Personal understanding of Java Reflection: Reflection is a set of methods for understanding classes and using classes ;
The Java basics know how to instantiate a class, and also know how to invoke a variable or method through an instance;
But many times the class is not written by ourselves, so we do not know the class very well, and this time we can understand or invoke the class itself by reflection.
Let's say there's a class named A.
Class a{
}
To understand this class we can use it to create variables, call functions, but when you do not know the class, then about the function itself is not the start, how to do.
All classes are objects of class classes C1=a.class So what we come to is an equivalent to the class itself, such as C1, the official name is class type (class type), through C1 can do a series of operations on class A;
In depth, it is appropriate to have a class type called a bytecode file;
Because reflection exists only in the runtime , that is, at run time, the call has been compiledclass, such as Class C=a.class, can be understood as the call to a already compiled class file A.class, so all parameters related to reflection must be class type, corresponding to each compiled file, such as the use of reflection call annotations are Xx.class, and the life cycle of annotations must be runtime, so that these annotations exist at run time
0. Three types of reflection initialization
Class C1=a.class;
A a=new a ();
Class C2=a.getclass ();
try {
Class c3=class.forname ("A");
} catch (ClassNotFoundException e) {
e.printstacktrace ();
}
1. Create an instance;
Pass
A a2= (a) c1.newinstance ();
The instance A2 of a is created, and the same instantiation function as Class A is implemented through class type C1
Then we get the surrogate C1 of Class A, so that we can do a series of operations on the class itself, such as the function inside the class, the return value of the function and the parameter value.
2. Methods
as follows, 3 functions are defined in Class A, and by C1.getmethod we get the
Method Set (method[] methods);
method returns the Value class type (class retype);
Method function value class type (class[] paratypes);
So that the function can be called further
Import java.lang.reflect.InvocationTargetException;
Import Java.lang.reflect.Method;
/** * Created by xuh-h on 2017/7/19. */public class SimpleTest {public static void main (string[] args) throws InvocationTargetException, Illegalaccessexc
eption {Class c1=a.class;
try {method M1=c1.getmethod ("Print", Int.class, Int.class);
Method M2=c1.getmethod ("Print", New Class[]{string.class,string.class});
M1.invoke (New A (), 1,2); Method[] Methods=c1.getdeclaredmethods (); function class type array for (methods Method:methods) {System.out.println ("method:" +
Method.getname ());
Class Retype = Method.getreturntype ();//function returns the value class type System.out.println ("Return:" +retype.getname ());
class[] Paratypes = Method.getparametertypes ();//function parameter class type array for (class Paratype:paratypes
) {System.out.println ("PARAMETER:" +paratype.getname ()); }
}
} catch (Nosuchmethodexception e) {e.printstacktrace ();
Class a{public void print (int a,int b) {System.out.println (a+b);
public void print (String a,string b) {System.out.println (a.touppercase () + "" +b.tolowercase ());
Public String print2 (int a,int b) {return "" +A*B; }
}
The result of the operation is to get the return value type and the parameter type of all functions in Class A, or to call the function in A;
3
method:print
return:void
PARAMETER:java.lang.String
PARAMETER:java.lang.String method
: Print
return:void
parameter:int
parameter:int
method:print2
RETURN:java.lang.String
Parameter:int
Parameter:int
So we see that when you do not know the class, or do not need to understand, just call can be easily achieved using reflection;
A class type is the class itself that can perform various operations
Another function is to implement dynamic loading,
Like what
Class.forName (Args[0])
The required classes can be loaded dynamically after compilation, at run time
Also like JDBC connection with MySQL,
Class.forName ("Driver")
The reflection is used