I. Overview of reflection
Java’s reflection mechanism allows you to know all the properties and methods of any class at runtime. You can call any of its methods and properties for any object. This ability to dynamically retrieve information and invoke methods is called the Reflection mechanism of the Java language. Reflection encapsulates the components of a class into an object from which instances of the class can be created. = =
Second, the use of reflection
2.1 Obtaining class Objects
There are two ways to get a Class object: class.class and class.forname (). .class applies to classes that are already known at compile time.
- .class
Class cl = String.class;
System.out.println(cl);
Copy the code
Output:
class java.lang.String
Copy the code
- Class.forName()
try { Class<? > aClass = Class.forName("java.lang.String"); System.out.println(aClass.getName()); } catch (ClassNotFoundException e) {system.out.println (" find String class "); }Copy the code
Output:
java.lang.String
Copy the code
2.2 Obtaining a class member
Student test class:
public class Student { private String name; private Integer age; public Student(String name, Integer age) { this.name = name; this.age = age; System.out.println("name: " + name + " age: " + age); } public Student(String name) { this.name = name; System.out.println("name: " + name); } public Student() { } private Student(Integer age) { this.age = age; System.out.println("age: " + age); } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}Copy the code
2.2.1 Getting the constructor of the class
Get all constructors:
public static void main(String[] args) throws ClassNotFoundException { Class<? > aClass = Class.forName("com.example.demo.thread.Student"); Constructor<? >[] declaredConstructors = aClass.getDeclaredConstructors(); for (Constructor constructor : DeclaredConstructors) {// Get the constructors permission modifier system.out. print(" Permission modifier: "+ Modifier. ToString (constructive.getModiFIERS ()) + "); / / get a constructor parameter of type Class [] parameterTypes = constructor. GetParameterTypes (); for (int i = 0; i < parameterTypes.length; i++) { System.out.print(parameterTypes[i].getSimpleName() + " "); } System.out.println(" "); }}Copy the code
Output:
Permission modifier: private Parameter type: Integer Permission modifier: public Parameter type: public Parameter type: String Permission modifier: public parameter type: String IntegerCopy the code
Gets a specific constructor:
Get the parameterless constructor:
try { Constructor<? > constructor = aClass.getConstructor(); System.out.println(constructor); } catch (NoSuchMethodException e) { e.printStackTrace(); }Copy the code
Output:
public com.example.demo.thread.Student()
Copy the code
Get the parameterized constructor:
Class[] p = {String.class, Integer.class}; try { Constructor<? > constructor = aClass.getConstructor(p); System.out.print(Modifier. ToString (constructive.getModiFIERS ()) + "); Class[] parametertypes = constructor.getParameterTypes(); for (int j = 0; j < parametertypes.length; j++) { System.out.print(parametertypes[j].getSimpleName() + " "); } } catch (NoSuchMethodException e) { e.printStackTrace(); }Copy the code
Output:
Public parameter :String IntegerCopy the code
Call the constructor:
Class[] p = {String.class, Integer.class}; try { Constructor<? > constructor = aClass.getConstructor(p); Object instance = constructor. NewInstance (" zhang Shan ", 22); System.out.println(instance); } catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) { e.printStackTrace(); }Copy the code
Output:
Name: zhang age: 22 com. Example. Demo. Thread. Student @ 4 d7e1886Copy the code
Call the private constructor:
Class[] p1 = {Integer.class}; try { Constructor<? > constructor = aClass.getDeclaredConstructor(p1); constructor.setAccessible(true); Student instance = (Student) constructor.newInstance(20); System.out.println(instance.getAge()); } catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) { e.printStackTrace(); }Copy the code
Output:
20
Copy the code
2.2.2 Method of obtaining a class
try {
Method method = aClass.getDeclaredMethod("setAge", Integer.class);
Object arg1s[] = {10};
Student student1 = Student.class.newInstance();
method.invoke(student1, arg1s);
System.out.println(student1.getAge());
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
Copy the code
Output:
10
Copy the code
2.2.3 Obtaining member variables of a class
try {
Field field = aClass.getDeclaredField("age");
Student student = new Student("zs", 21);
try {
field.setAccessible(true);
field.set(student, 20);
System.out.println(student.getAge());
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
Copy the code
Output:
20
Copy the code