concept
- Reflection is the key to Java being considered as a dynamic language. Reflection allows programs to use the Reflection API to retrieve the internal information of any class during execution and to directly manipulate the internal properties and methods of any object.
- After the Class is loaded, an object of type Class (one Class object per Class) is generated in the method area of heap memory, which contains the complete structure information of the Class. We see the structure of the class through this object.
Code demo:
package ReflectionStudy;
// What is reflection
public class demo01 {
public static void main(String[] args) throws ClassNotFoundException {
// Get the Class object of the Class by reflection
Class c1 = Class.forName("ReflectionStudy.user");
System.out.println(c1);//class ReflectionStudy.user
// A Class has only one Class object in memory
// Once a Class is loaded, the entire structure of the Class is encapsulated in a Class object
Class c2 = Class.forName("ReflectionStudy.user");
Class c3 = Class.forName("ReflectionStudy.user");
Class c4 = Class.forName("ReflectionStudy.user");
Class c5 = Class.forName("ReflectionStudy.user");
System.out.println(c2.hashCode());/ / 460141958
System.out.println(c3.hashCode());/ / 460141958
System.out.println(c4.hashCode());/ / 460141958
System.out.println(c5.hashCode());/ / 460141958}}/ / entity class
class user{
private String name;
private int age;
private int id;
public user(a) {}public user(String name, int age, int id) {
this.name = name;
this.age = age;
this.id = id;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge(a) {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId(a) {
return id;
}
public void setId(int id) {
this.id = id; }}Copy the code
- Contrast between normal mode and reflection mode:
Class Class
- Method of obtaining class object:
package ReflectionStudy;
// Get the class object
public class demo02 {
public static void main(String[] args) throws ClassNotFoundException {
Person person = new Student();
System.out.println("This man is:"+person.name);// This person is: student
// Method 1: Obtain from objects
Class c1 = person.getClass();
System.out.println(c1.hashCode());/ / 460141958
// Method 2: obtain by forname
Class c2 = Class.forName("ReflectionStudy.Student");
System.out.println(c2.hashCode());/ / 460141958
// Method 3: Use the class name. Class
Class c3 = Student.class;
System.out.println(c3);//class ReflectionStudy.Student
// The basic built-in wrapper classes all have a Type attribute
Class c4 = Integer.TYPE;
System.out.println(c4);//int
// Get the parent type
Class c5 = c1.getSuperclass();
System.out.println(c5);//class ReflectionStudy.Person}}class Person{
public String name;
public Person(a) {}public Person(String name) {
this.name = name;
}
@Override
public String toString(a) {
return "Teacher{" +
"name='" + name + '\' ' +
'} '; }}class Student extends Person{
public Student(a) {
this.name="Students"; }}class Teacher extends Person{
public Teacher(a) {
this.name="Teacher"; }}Copy the code
- All types of classes
package ReflectionStudy;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
// All types of classes
public class demo03 {
public static void main(String[] args) {
Class c1 = Object.class;/ / class
Class c2 = Comparable.class;/ / interface
Class c3 = String[].class;// One dimensional array
Class c4 = int[][].class;// A two-dimensional array
Class c5 = Annotation.class;/ / comment
Class c6 = ElementType.class;/ / the enumeration
Class c7 = Integer.class;// Basic data type
Class c8 = void.class;//void
Class c9 = Class.class;//Class
System.out.println(c1);//class java.lang.Object
System.out.println(c2);//interface java.lang.Comparable
System.out.println(c3);//class [Ljava.lang.String;
System.out.println(c4);//class [[I
System.out.println(c5);//interface java.lang.annotation.Annotation
System.out.println(c6);//class java.lang.annotation.ElementType
System.out.println(c7);//class java.lang.Integer
System.out.println(c8);//void
System.out.println(c9);//class java.lang.Class
// As long as the element type is the same as the dimension, it is the same Class
int[] a = new int[5];
int[] b = new int[50];
System.out.println(a.getClass().hashCode());/ / 460141958
System.out.println(b.getClass().hashCode());/ / 460141958}}Copy the code
- Java memory analysis:
Code demo
package ReflectionStudy;
public class demo04 {
public static void main(String[] args) {
A a = newA(); System.out.println(A.m); }}class A{
static {
System.out.println("Static code block");
m = 30;
}
static int m = 100;
public A(a) {
System.out.println("Parametric structure"); }}Copy the code
Code running results:
- Get the runtime structure of the class
Direct code demo:
package ReflectionStudy;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
// Get the runtime structure of the class
public class demo05 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
Class c = Class.forName("ReflectionStudy.user");
System.out.println("============= get the name of the class ==================");
System.out.println("Class name obtained by the getName() method:" + c.getName());
System.out.println("Class name obtained by the getSimpleName() method:" + c.getSimpleName());
System.out.println("============= get the attributes of the class ==================");
Field[] fields = c.getFields();// Only public attributes of this class can be obtained
for (Field field : fields) {
System.out.println("Properties obtained by the getFields() method:" + fields);
}
fields = c.getDeclaredFields();
for (Field field : fields) {
System.out.println("Properties obtained by the getDeclaredFields() method: + fields);
}
System.out.println("============= gets the specified property of the class ===============");
Field field = c.getDeclaredField("name");
System.out.println("Get the specified property of the class" + field);
System.out.println("============= method for getting classes ==================");
Method[] methods = c.getMethods();// Find all public methods of this class and its parent
for (Method method : methods) {
System.out.println("Methods obtained by getMethods() : + method);
}
methods = c.getDeclaredMethods();// Find all methods of this class
for (Method method : methods) {
System.out.println("GetDeclaredMethods () method: + method);
}
System.out.println("============= get the specified method of the class ===============");
Method method = null;
method = c.getDeclaredMethod("getName".null);
System.out.println(method);
method = c.getDeclaredMethod("setName", String.class);
System.out.println(method);
System.out.println("============= get constructor ====================");
Constructor[] constructors = c.getConstructors();
for (Constructor constructor : constructors) {
System.out.println("GetConstructors () gets the constructor:"+constructor);
}
constructors = c.getDeclaredConstructors();
for (Constructor constructor : constructors) {
System.out.println("GetDeclaredConstructors () get the constructor:"+constructor);
}
System.out.println("============= get the specified constructor ===============");
Constructor declaredConstructor = c.getDeclaredConstructor(String.class, int.class, int.class);
System.out.println("Get the specified constructor :"+declaredConstructor); }}Copy the code
Running results:
= = = = = = = = = = = = = to get the name of the class = = = = = = = = = = = = = = = = = = getName () method to obtain the name of the class: ReflectionStudy. User getSimpleName () method to obtain the name of the class: User = = = = = = = = = = = = = for attributes of a class = = = = = = = = = = = = = = = = = = getDeclaredFields () method to obtain the properties of the: Ljava.lang.reflect.field;@74a14482 getDeclaredFields() Ljava.lang.reflect.field;@74a14482 getDeclaredFields() [Ljava. Lang. Reflect. Field; @ 74 a14482 = = = = = = = = = = = = = get specified attributes of a class = = = = = = = = = = = = = = = get specified attributes of a class private Java lang. String ReflectionStudy. User. Name = = = = = = = = = = = = = acquire the method of class = = = = = = = = = = = = = = = = = = getMethods () method to obtain the method: Public Java. Lang. String ReflectionStudy. User. GetName () getMethods () method to obtain the method: Public int ReflectionStudy. User. GetId () getMethods () method to obtain the method: Public void ReflectionStudy. User. Elegantly-named setName (Java. Lang. String) getMethods () method to obtain the method: Public void ReflectionStudy. User. SetId (int) getMethods () method to obtain the method: Public int ReflectionStudy. User. GetAge () getMethods () method to obtain the method: Public void ReflectionStudy. User. SetAge (int) getMethods () method to obtain the method: Public final void Java. Lang. Object. The wait () throws Java lang. InterruptedException getMethods () method to obtain the method: Public final void Java. Lang. Object. Wait (long, int) throws Java. Lang. InterruptedException getMethods () method to obtain the method: Public final native void Java. Lang. Object. Wait (long) throws Java. Lang. InterruptedException getMethods () method to obtain the method: Public Boolean java.lang.object. equals(java.lang.object) getMethods() Public Java. Lang. String Java. Lang. Object. The toString () getMethods () method to obtain the method: Public native int Java. Lang. Object. The hashCode () getMethods () method to obtain the method: Public final native Java. Lang. Class. Java lang. Object. The getClass () getMethods () method to obtain the method: Public final native void java.lang.object.notify () getMethods() Public final native void Java. Lang. Object. The notifyAll () getDeclaredMethods () method to obtain the method: Public Java. Lang. String ReflectionStudy. User. GetName () getDeclaredMethods () method to obtain the method: Public int ReflectionStudy. User. GetId () getDeclaredMethods () method to obtain the method: Public void ReflectionStudy. User. Elegantly-named setName (Java. Lang. String) getDeclaredMethods () method to obtain the method: Public void ReflectionStudy. User. SetId (int) getDeclaredMethods () method to obtain the method: Public int ReflectionStudy. User. GetAge () getDeclaredMethods () method to obtain the method: Public void ReflectionStudy. User. SetAge (int) = = = = = = = = = = = = = get class specified method = = = = = = = = = = = = = = = public Java lang. String ReflectionStudy.user.getName() public void ReflectionStudy.user.setName(java.lang.String) = = = = = = = = = = = = = for the constructor = = = = = = = = = = = = = = = = = = = = getConstructors () constructor: public ReflectionStudy. User getConstructors () () to obtain the constructor: Public reflectionstudy.user (java.lang.string,int,int) getDeclaredConstructors() Public reflectionstudy.user () getDeclaredConstructors() Public ReflectionStudy. User (Java. Lang. String, int, int) = = = = = = = = = = = = = to obtain the specified constructor = = = = = = = = = = = = = = = to obtain the specified constructor: the public ReflectionStudy.user(java.lang.String,int,int)Copy the code