Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reflection: How to find a constructor that takes a list of objects?

I have a class that takes a List in the constructor;

public class MyClass {

       private List<Structure> structures;

       public MyClass(List<Structure> structures) {
          this.structures = structures;
       }
}

that I need to instantiate via reflection. How do I define the call to class.getConstructor() to find this?

Regards

like image 407
user497087 Avatar asked Jun 09 '11 08:06

user497087


1 Answers

This should work:

Constructor<MyClass> constructor = MyClass.class.getConstructor(List.class);

or

Constructor constructor = MyClass.class.getConstructor(new Class[]{List.class});

for Java 1.4.x or less

like image 75
Andreas Dolk Avatar answered Nov 15 '22 03:11

Andreas Dolk