2

I want to create class object from name, call constructor and create new instance. But I don't know how to send parameters to constructor. My base class is:

    public carDao(ConnectionSource connectionSource, Class<Car> dataClass) throws SQLException 
{
    super(connectionSource, dataClass);
}

adn what i want do do:

    Class myClass = Class.forName("carDao");
    Constructor intConstructor= myClass.getConstructor();
    Object o = intConstructor.newInstance();

what should I write in getConstructor()?

4
  • 1
    Does this answer you question - stackoverflow.com/questions/6094575/… Commented Aug 15, 2013 at 11:02
  • 3
    You should first explain why you want to use reflection, because for 99% of the problems, it's the wrong solution. And respect the Java naming conventions, and put your classes in a package. Commented Aug 15, 2013 at 11:02
  • DAO antipattern, you should not invoke singletons. Commented Aug 15, 2013 at 11:07
  • 1
    @RomanC Where do you see a singleton? Commented Aug 15, 2013 at 11:08

4 Answers 4

7

You need to pass classes for your constructor

For example if your constructor has a String parameter

  Class myClass = Class.forName("carDao");
  Constructor<?> cons = myClass.getConstructor(String.class);
  Object o = cons.newInstance("MyString");

In your case it will be:

  myClass.getConstructor(ConnectionSource.class, Class.class);

Since getConstructor method declaration is this:

 //@param parameterTypes the parameter array
 public Constructor<T> getConstructor(Class<?>... parameterTypes)
    throws NoSuchMethodException, SecurityException {
Sign up to request clarification or add additional context in comments.

Comments

3

This should work:

public static <T> T newInstance(final String className,final Object... args) 
        throws ClassNotFoundException, 
        NoSuchMethodException, 
        InstantiationException, 
        IllegalAccessException, 
        IllegalArgumentException, 
        InvocationTargetException {
  // Derive the parameter types from the parameters themselves.
  Class[] types = new Class[args.length];
  for ( int i = 0; i < types.length; i++ ) {
    types[i] = args[i].getClass();
  }
  return (T) Class.forName(className).getConstructor(types).newInstance(args);
}

Comments

1

You need to pass types or arguments in getConstructor to get correct constructor. Try maybe

myClass.getConstructor(ConnectionSource.class,Class.class);

and

intConstructor.newInstance(connectionSourceInstance, classInstance);

Comments

0

You should give the Class objects to the getConstructor method, like this:

Class myClass = Class.forName("carDao");
Constructor intConstructor= myClass.getConstructor(ConnectionSource.class, Class.class);
Object o = intConstructor.newInstance(connectionSource, dataClass);

For more information, refers to the documentation of the getConstructor method:

public Constructor<T> getConstructor(Class<?>... parameterTypes)
                              throws NoSuchMethodException,
                                     SecurityException

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.