1. Reflection
Reflection: The JAVA reflection mechanism is in the running state. For any class, you can know all the properties and methods of this class; for any object, you can call any of its methods and properties; this The function of dynamically obtaining information and dynamically calling object methods is called the reflection mechanism of the Java language.
If you want to dissect a class, you must first obtain the bytecode file object of the class. The dissection uses the methods in the Class class. Therefore, you must first obtain the Class type object corresponding to each bytecode file. The operation of reflection is actually obtained through the Class object:
*a, java.lang.reflect.Field: Provides information about a single field of a class or interface, as well as dynamic access rights to it. The reflected field may be a class (static) field or an instance field. Member variables of the operating class.
*b, java.lang.reflect.Constructor: The constructor of the operation class.
*c, java.lang.reflect.Method: Method of operating class.
Create a Person object as an instance before learning the basics of reflection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | package com.jalja.org.base.relfect;
public class Person {
private String name;
int age;
public String address;
public Person() {
}
private Person(String name) {
this.name = name;
}
Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public void show() {
System.out.println( "show" );
}
public void method(String s) {
System.out.println( "method " + s);
}
public String getString(String s, int i) {
return s + "---" + i;
}
private void function () {
System.out.println( "function" );
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", address=" + address
+ "]" ;
}
}
|
Copy after login
2. Get the Class object of the class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public static void getClassObject() throws ClassNotFoundException{
Person person1= new Person();
Person person2= new Person();
Class c1=person1.getClass();
Class c2=person2.getClass();
System.out.println(person1==person2);
System.out.println(c1==c2);
Class c3=Person. class ;
System.out.println(c1==c3);
Class c4=Class.forName( "com.jalja.org.base.relfect.Person" );
System.out.println(c1==c4);
}
|
Copy after login
3. java.lang.reflect.Constructor: object and use the Constructor class.
1. Get the Constructor object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public static void getConstructorTest() throws Exception{
Class c4=Class.forName( "com.jalja.org.base.relfect.Person" );
Constructor [] cs=c4.getConstructors();
Constructor[] cs2 =c4.getDeclaredConstructors();
Constructor cs3=c4.getConstructor();
Constructor cs4=c4.getConstructor(String. class ,int. class ,String. class );
Constructor cs5=c4.getDeclaredConstructor(String. class );
}
|
Copy after login
2. Create an instance of the class represented by the Class object through the Constructor object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public static void createObject() throws Exception{
Class c4=Class.forName( "com.jalja.org.base.relfect.Person" );
Constructor cs3=c4.getConstructor();
Object obj=cs3.newInstance();
Constructor cs4=c4.getConstructor(String. class ,int. class ,String. class );
Object obj1=cs4.newInstance( "jalja" ,21, "北京" );
System.out.println(obj1);
Constructor cs5=c4.getDeclaredConstructor(String. class );
cs5.setAccessible(true);
Object obj2=cs5.newInstance( "张三丰" );
System.out.println(obj2);
}
|
Copy after login
4. java.lang.reflect.Field
1. Get the Field object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public static void getFieldTest() throws Exception{
Class cs=Class.forName( "com.jalja.org.base.relfect.Person" );
Field [] fs=cs.getFields();
Field [] fs1=cs.getDeclaredFields();
Field fs2=cs.getField( "address" );
Field fs3=cs.getDeclaredField( "name" );
System.out.println(fs3);
}
|
Copy after login
2. Through the Field object Assign a value to the specified class attribute
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public static void createVarValue() throws Exception{
Class cs=Class.forName( "com.jalja.org.base.relfect.Person" );
Object obj=cs.getConstructor().newInstance();
Field addressField=cs.getField( "address" );
addressField.set(obj, "北京" );
System.out.println(obj);
Field nameField=cs.getDeclaredField( "name" );
nameField.setAccessible(true);
nameField.set(obj, "张三丰" );
System.out.println(obj);
}
|
Copy after login
5. java.lang.reflect.Method
1. Get the Method object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public static void getMethodTest() throws Exception{
Class cs=Class.forName( "com.jalja.org.base.relfect.Person" );
Method [] m1=cs.getMethods();
Method [] m2=cs.getDeclaredMethods();
Method m3=cs.getMethod( "show" );
Method m4=cs.getMethod( "method" ,String. class );
Method m5=cs.getDeclaredMethod( "function" );
System.out.println(m5);
}
|
Copy after login
2. Call the method of the specified class through the Method object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public static void createMethod() throws Exception{
Class cs=Class.forName( "com.jalja.org.base.relfect.Person" );
Object obj=cs.getConstructor().newInstance();
Method m3=cs.getMethod( "show" );
m3.invoke(obj);
Method m4=cs.getMethod( "method" ,String. class );
m4.invoke(obj, "北京" );
Method m6=cs.getMethod( "getString" ,String. class ,int. class );
Object str=m6.invoke(obj, "北京" ,200);
System.out.println(str);
Method m5=cs.getDeclaredMethod( "function" );
m5.setAccessible(true);
m5.invoke(obj);
}
|
Copy after login