ClassReflectionDemo
Copy the code code as follows:
package Reflection;
@Deprecated
public class ReflectionDemo {
private String pri_field;
public String pub_field;
public ReflectionDemo(){}
public ReflectionDemo(String name){}
private ReflectionDemo(String name,int int1){}
public void ReflectMethod(){}
public void ReflectMethod(String str){}
private void ReflectMethod(int int1){}
class innerclss_Refection{}
}
Test classReflectionDemoTest
Copy the code code as follows:
package Reflection;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ReflectionDemoTest {
public static void main(String[] args) {
//Create a class object and use generic modification to avoid forced transfer
Class cla=ReflectionDemo.class;
//Method to get all public fields
Field[] field=cla.getFields();
for(Field f:field){
System.out.println("Method to get all public fields:"+f.toString());
}
//Method to obtain a specified public domain
try {
Field field1=cla.getField("pub_field");
System.out.println("Method to get the specified public field:"+field1.toString());
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Method to obtain all domains (including private domains), Declare
Field[] field2=cla.getDeclaredFields();
for(Field f:field2){
System.out.println("Method to obtain all fields (including private fields):"+f.toString());
}
//Method to get the specified domain (including private domain)
try {
Field field3=cla.getDeclaredField("pri_field");
System.out.println("Method to get the specified field (including private field):"+field3.toString());
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Get all public methods (including parent classes)
Method[] method=cla.getMethods();
for(Method m:method){
System.out.println("Get all public methods:"+m.toString());
}
try {
//Get the specified parameterless method
//The second parameter indicates the parameter type, and the parameter NUll indicates the parameterless method.
Method method1=cla.getMethod("ReflectMethod", null);
System.out.println("Get the method of public method without parameters:"+method1.toString());
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Method to get all methods of this class (excluding parent class)
Method[] method2=cla.getDeclaredMethods();
for(Method m:method2){
System.out.println("Get all methods (including parent classes):"+m.toString());
}
//Get the method of the specified method of this class (excluding parent class)
//The first parameter is the method name, the second parameter is the method return type, and NULL is the method without parameters.
try {
Method method3=cla.getDeclaredMethod("ReflectMethod",int.class);
System.out.println("Get the method of the specified method of this class (excluding parent class):"+method3);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Get the public constructor
//Get the public constructor without parameters
try {
Constructor cs=cla.getConstructor(null);
System.out.println("Get the parameterless constructor:"+cs);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Get the public constructor with parameters
//If there is a constructor with multiple parameters, there are multiple constructors. Pay attention to the order.
try {
Constructor cs=cla.getConstructor(String.class);
System.out.println("Get the parameterized constructor:"+cs);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Get all construction methods
try {
Constructor[] cs=cla.getDeclaredConstructors();
for(Constructor c:cs){
System.out.println("Get all construction methods:"+c);
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Get the package name
Package pack=cla.getPackage();
System.out.println("Get the name of the current package:"+pack);
//Get comments
Annotation[] ann=cla.getAnnotations();
for(Annotation an:ann){
System.out.println(an.toString());
}
//Get the parent class
Class supercla=cla.getSuperclass();
System.out.println(supercla);
//Get inner class
Class[] innercla=cla.getDeclaredClasses();
for(Class cl:innercla){
System.out.println(cl);
}
}
}
Summary: Steps to obtain information about Class objects:
1. Create a Class object. Method 1 Class cla=Class.forName(“PATH”) Method 2 CLass cla=Instance.getclass(); Method 3 Class cla=Class.class
2. Use Field, Method, Constructor, Package, and Annotation to obtain relevant information by calling the cla.getXXXX method.
3. The methods of obtaining parent classes and inner classes are slightly different.
4. When using cla.getMethods, all public methods of the class and the parent class are called.
5. When using Declared, all methods of the class, including private methods, are called. The same applies to domains and constructors.
6. When the reflection class has no public constructor and cannot use the newInstance method directly, use
Method cs=cla.getMethod("getInstance",null);//Get the method
Calendar calendar=(Calendar) cs.invoke(cla, null);//Execute the obtained method, parameter 1 is the object of the execution method, parameter 2 represents the parameters of the method
Date df=calendar.getTime();
System.out.println(df.toString());
to get.