As we all know, Java reflection provides Class.getConstructors
method, but how is the order of the Constructor<?>[]
array?
For example, I create Person class and there are 3 construct method:
public Person()
{
}
public Person(int age)
{
this.age = age;
}
public Person(int age, String name)
{
this.age = age;
this.name = name;
}
And by debug, I find the order of constructor array like this:
0 = public xxx.Person(int)
1 = public xxx.Person(int, java.lang.String)
2 = public xxx.Person()
getConstructors() Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object. Annotation[] getDeclaredAnnotations()
Reflection of Java MethodsMethod[] methods = obj. getDeclaredMethod(); Here, the getDeclaredMethod() returns all the methods present inside the class.
We can use newInstance() method on the constructor object to instantiate a new instance of the class. Since we use reflection when we don't have the classes information at compile time, we can assign it to Object and then further use reflection to access it's fields and invoke it's methods.
getMethod() returns a Method object that reflects the specified public member method of the class or interface represented by this Class object. The name parameter is a String specifying the simple name of the desired method.
When I look at the Class#getDeclaredConstructors()-method I noticed the following line:
"The elements in the array returned are not sorted and are not in any particular order."
At the Class#getConstructors()-method this isn't mentioned though.. Even though it isn't mentioned, my assumption is that the same applies to that method, which your code kind of proves.
I'm not sure what you want to accomplish (perhaps you could add you goal to your question so we might be able to help your further), but I don't think it's possible to get the Constructors in the same order using reflection.
EDIT: With the constructors in your example, you can call either of the three like this:
Class<Person> pClass = Person.class;
pClass.getDeclaredConstructor().newInstance(); // default constructor
pClass.getDeclaredConstructor(int.class).newInstance(50); // constructor with int parameter
pClass.getDeclaredConstructor(int.class, String.class).newInstance(50, "test"); // constructor with int and String parameters
As constructor order does not matter for java classes there is not specification for getConstructors method order. I think testing you can figured out the default order, but I will not use it as can change any time. If you want to get an specific constructor check parameters or if the order does matter for you process the .java file as a simple text file.
Java documentions.
Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object. An array of length 0 is returned if the class has no public constructors, or if the class is an array class, or if the class reflects a primitive type or void. Note that while this method returns an array of Constructor objects (that is an array of constructors from this class), the return type of this method is Constructor[] and not Constructor[] as might be expected. This less informative return type is necessary since after being returned from this method, the array could be modified to hold Constructor objects for different classes, which would violate the type guarantees of Constructor[]. Returns:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With