Dashboard > People > codesmell > 2008 > Juni > 16 > protected Constructors
  codesmell Log In   View a printable version of the current page.  
  protected Constructors
Added by Uwe Schaefer, last edited by Uwe Schaefer on Jun 16, 2008
Labels: 

Did you ever try to create an instance of a class with a protected constructor from outside its package?
I ran into this need when working with JPA-Entities of which i wanted to know initial field values. (yeah, i know. kind of black magic...)

It is actually easier, than getting protected Fields:

public static <T> T newInstance(final Class<T> c)
    {
        try
        {
            Constructor<T> constructor = c.getDeclaredConstructor(new Class[] {});
            if (constructor != null)
            {
                constructor.setAccessible(true);
                return constructor.newInstance((Object[]) null);
            }
            else
            {
                throw new IllegalArgumentException("Cannot find empty Contructor for class '" + c.getName() + "'");
            }
        }
        catch (SecurityException e)
        {
            throw ExceptionHelper.wrap(e);
        }
        catch (IllegalArgumentException e)
        {
            throw ExceptionHelper.wrap(e);
        }
        catch (InstantiationException e)
        {
            throw ExceptionHelper.wrap(e);
        }
        catch (IllegalAccessException e)
        {
            throw ExceptionHelper.wrap(e);
        }
        catch (InvocationTargetException e)
        {
            throw ExceptionHelper.wrap(e);
        }
        catch (NoSuchMethodException e)
        {
            throw ExceptionHelper.wrap(e);
        }
    }
Juni 2008
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30          

Quartz fits
Another Richtext-editor in Wicket called NicEdit

This site is powered by a free Atlassian Confluence Personal Server License. Evaluate Confluence for your organisation or read more about Confluence licensing here.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.9 Build:#527 Sep 07, 2006) - Bug/feature request - Contact Administrators