JAVA basic 2-Reflection (3): Retrieving internal class information
People will always get tired. When you feel tired, stick to it and stick to it.
In the last two articles, I will explain how to obtain information about the class perimeter. In this article, we will go deep into the class and look at the class constructor and internal variables, function. In comparison, this article is more important.
1. Get constructor correlation 1. There are several methods to obtain constructor of a class:
// Obtain the public-Type Constructor.
[] GetConstructors (); Constructor
GetConstructor (Class
... ParameterTypes); // obtain all types of Constructor
[] GetDeclaredConstructors (); Constructor
GetDeclaredConstructor (Class
... ParameterTypes)
In the four methods, getConstructors and getConstructor obtain constructors declared as public. The constructor declared as protected and private cannot be obtained. The getDeclaredConstructors function with the Declared (Declaration) function can obtain all Declared constructors, whether it is public, proctected or private. Therefore, these two functions are commonly used.
The following example shows how to use getDeclaredConstructors and getDeclaredConstructor.
(1) getDeclaredConstructors ()
Constructor
[] getDeclaredConstructors();
The getDeclaredConstructors () function obtains an array of Constructor;
First, construct a class Person:
public class Person { private int age; private String name; public Person(){ } private Person(int age, String name){ this.age = age; this.name = name; } private Person(Integer age, String name){ this.age = age; this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; }}In the Person class, currently we only care about constructors, So we declare three constructors, one with no parameters and two with parameters. We declare the constructors without parameters as public, and the other two are declared as private. as we have mentioned above, in the four methods for obtaining constructors, only the two getDeclaredXXX () Functions with Declared can obtain all functions in the class. The other two can only obtain functions declared as public. The following example is used to obtain the constructor of the Person class:
// 1. Enumeration Class
Clazz = Person. class; Constructor
[] Constructors = clazz. getDeclaredConstructors (); for (Constructor item: constructors) {Log. d (TAG, "enumeration constructor:" + item. toString ();} // 2. Obtain the Constructor of the specified Constructor according to the type.
Constructor = clazz. getDeclaredConstructor (Integer. class, String. class); Log. d (TAG, "constructor obtained by specifying the parameter:" + constructor. toString ());The result is as follows:
Let's take a look at the meanings of the two functions and then explain the above Code:
(1) getDeclaredConstructors ()
GetDeclaredConstructors () returns a list Of all constructors, including constructors declared as private, protected, and public.
Then we use... And print each constructor.
(2) getDeclaredConstructor (Class ... ParameterTypes)
This function obtains the specified constructor by specifying the constructor parameter list. Pay attention to the following two points:
ParameterTypes must be derived from objects of the Object type. The original data parameterTypes, such as Int, double, and float, cannot be matched exactly with the parameter declaration sequence, type, and number in the expected constructor, if one more, one less, or the type does not match, it cannot be found. This is worth noting. getDeclaredConstructor is not a fuzzy match but an exact match! So let's look back at our code based on these two points:
Constructor
constructor = clazz.getDeclaredConstructor(Integer.class, String.class);
Here, we specify the parameter list when matching the constructor. The first parameter of the constructor to be matched is of the Integer type, and the second parameter is of the String type.
We may doubt that we have two constructors that can match:
private Person(int age, String name){ this.age = age; this.name = name;}private Person(Integer age, String name){ this.age = age; this.name = name;}In the two constructors, the age type of the first constructor is the primitive int type, and the age parameter type of the second constructor is Integer.
Clazz. getDeclaredConstructor (Integer. class, String. class); which one should match?
Because this is a strict match, the private Person (Integer age, String name) function must be matched here. What if we want to match private Person (int age, String name? The method for getting the constructor is written as follows:
Constructor
constructor = clazz.getDeclaredConstructor(int.class, String.class);
It is important to note that not only the Class derived from the Object has a Class Object, but the original data type also has a Class Object.
2. Constructor methods when constructing an instance using the Constructor object to construct an instance, the newInstance method is mainly used:
public T newInstance(Object... args);
You can see that newInstance can pass in variable length parameters. It is worth noting that the type, sequence, and number of input parameters must all correspond to the current Constructor object one by one, otherwise, the following error will be reported:
The following is an example of how to use this function:
Similarly, this example is based on the Person class:
public class Person { private int age; private String name; public Person(){ } private Person(int age, String name){ this.age = age; this.name = name; } private Person(Integer age, String name){ this.age = age; this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; }}Note that the three constructors here, the accessors of a non-argument constructor are public, and the other two are all private!
Let's take a look at how to use newInstance to construct a Person instance:
Class
Clazz = Person. class; Constructor
Constructor = clazz. getDeclaredConstructor (Integer. class, String. class); constructor. setAccessible (true); // construct an instance: Person person1 = (Person) constructor. newInstance (new Integer (30), new String ("harvic"); Log. d (TAG, "constructed parameter:" + person1.getName () + "" + person1.getAge (); // construct instance two persons person2 = (Person) constructor. newInstance (50, "qijian"); Log. d (TAG, "constructed parameter:" + person2.getName () + "" + person2.getAge (); // construct instance 3 Person person3 = (Person) constructor. newInstance (); person3.setAge (30); person3.setName ("qijian"); Log. d (TAG, "the constructed parameter is:" + person3.getName () + "" + person3.getAge ());The result is as follows:
This code is divided into four parts:
Part 1: Get the Constructor object
Class
clazz = Person.class;Constructor
constructor = clazz.getDeclaredConstructor(Integer.class, String.class);constructor.setAccessible(true);
First, getDeclaredConstructor (Integer. class, String. class) is used to obtain the specified constructor. The first parameter is of the Integer type, and the second parameter is of the String type.
Therefore, the following constructor is matched:
private Person(Integer age, String name){ this.age = age; this.name = name;}The last sentence uses the Constructor function setAccessible. Its complete declaration is:
void setAccessible(boolean flag)
It indicates whether to set any function or field to accessible. If it is set to true, no matter whether the function or field is declared as private or public, it is accessible. The default value is false, that is, only the public type is accessible. If setAccessible (true) is not set, a prompt (Access denied) is displayed when you use the protected or private constructor to create an instance)