Understanding Reflection is great for learning the Java framework, as the core of the spring framework is implemented using Java reflection, and it can be helpful to do some Java-level operations.
First, the use of class classes
1, everything is the object, (of course, the basic data type, static members are not object-oriented (belonging to the Class)), so we create every class is also an object, That is, the class itself is an instance object of the Java.lang.Class class, but these objects do not need to be new, because the Java.lang.Class class is constructed in a private way 2. Any class is an instance object of class, and this instance object is represented in three ways : (we create a new student Class)①class C1 = student.class;//actually tells us that any class has an implied static member variable class (used when the class name is known) ②class c2 = Stu.getclass ()///The object of the class is known to pass the GetClass method (when the object is known) ③class C3 = Class.forName ("Full Name of Class");//There will be a ClassNotFoundException exceptionThe official website explains: C1,C2 represents the class type of the Student class (), everything is object, the class is also an object, is an instance object of class, this object we become the class type of the Class ( It's a little messy, but it can be understood slowly. Here's one thing to note when we execute System.out.println (C1==C2); statement, the result returns True, which is why? The reason is that both C1 and C2 represent the class type of the student class, and a class may be an instance object of the class. It is entirely possible to create an instance of the class from the class type of the class by creating an instance of the student through C1 or C2. Student stu = (Student) c1.newinstance (),//The precondition is that there must be an argument-free constructor, because the statement will call its parameterless construction method. The statement throws an exception. second, dynamic loading class1, compile-time load class is static load class,new Creation object is static load class, at compile time need to load all the available classes, if there is no use, then the entire file can not be compiled 2. Runtime load class is dynamic load Class Class C = Class.forName ("Full name of the class"), not only represents the type of the class, but also represents the dynamic load class, compile will not error, load at runtime, using interface standard can be more convenient to dynamically load the implementation of the class. The functional classes try to use dynamic loading instead of static loading. many software such as qq,360 online upgrade, do not need to recompile the file, just dynamic loading new things Iii. Obtaining method information1, the basic data type, the VOID keyword has the class type
-
1Class C1 =int.class;//class type of int2Class C2 =string.class;//The class type of the string class, which can be understood as the String.class bytecode file generated by the compilation,3 //Of course, this is not an official statement.4Class C3 =Double.class;5Class C4 =double.class;6Class c5 =void.class;
2. Basic API Operation of Class
1 /**2 * Print class information, including member functions of the class, member variables3 * @paramobj information about the class to which the object belongs4 */5 publicstaticvoid printclassmessage (Object obj) {6 //to get information about a class, you first get the class type of the class7Class C = Obj.getclass ();//is the object of the subclass that is passed, and C is the class type of the child class8 //gets the name of the class9System.out.println ("Tired name is:" +c.getname ());Ten One /* A * Method Class, object of methods - * A member method is an object - * The GetMethods () method gets all the public functions, including those inherited by the parent class . the * Getdeclaredmethods () gets more of the methods that the class declares itself, without asking access rights - */ -Method[] ms = C.getmethods ();//c.getdeclaredmethods (); - for(inti = 0; i < ms.length; i++){ + //gets the class type of the return value type of the method -Class Retruntype =Ms[i].getreturntype (); +System.out.print (Retruntype.getname () + ""); A //get the name of the method atSystem.out.print (Ms[i].getname () + "("); - //gets the parameter type---> Gets the class type of the type of the parameter list -class[] Paratypes =ms[i].getparametertypes (); - for(Class class1:paratypes) { -System.out.print (Class1.getname () + ","); - } inSystem.out.println (")"); - } to}
Class API There are many other methods, you can get interface, package, annotation and many other information, specific use please refer to the Help manual, this article is not explained in detail. It is important to note that if you want the information for a class, you first get the class type of the class.
iv. Getting member variable constructor information
1 /**2 * Member variables are also objects, objects that are Java.lang.reflect.Field this class3 * Field class encapsulates operations on member variables4 * The GetFields () method gets information about the member variables of all public5 * The Getdeclarefields () method gets information about the member variables declared by the class itself6 */7field[] fs =c.getdeclaredfields ();8 for(Field field:fs) {9 //the class type that gets the type of the member variableTenClass FieldType =Field.gettype (); OneString TypeName =fieldtype.getname (); A //get the name of the member variable -String FieldName =field.getname (); -System.out.print (typename+ "" +fieldName); the } - - - /** + * Constructors are also objects - * Information in the java.lang.Constructor that encapsulates the constructor function + * The GetConstructor () method gets all the public constructors A * Getdeclaredconstructors Get all the constructors at */ -Constructor[] cs =c.getdeclaredconstructors (); - for(Constructor constructor:cs) { -System.out.print (Constructor.getname () + "("); - //gets the argument list of the constructor---the class type of the parameter Leibiao -class[] Paramtypes =constructor.getparametertypes (); in for(Class class1:paramtypes) { -System.out.print (Class1.getname () + ","); to } +System.out.println (")"); -}
V. Basic operation of Method reflection
1. How to get a methodThe name of the method and the parameter list of the method can only determine a method Method m = C.getdeclaredmethod ("Method name", variable parameter list (parameter type. Class))2, method of reflection OperationM.invoke (object, parameter list)method returns null if there is no return value, returns the object type if there is a return value, and then forces the type to be converted to the return value type of the original function vi. Understanding the nature of set generics through reflection
1 ArrayList list1 =newarraylist (); 2 arraylist<string> list2 =newarraylist<string>(); 3 4 Class c1 = list1.getclass (); 5 Class c2 = list2.getclass (); 6 7 System.out.println (C1==C2); // The result is true, why??
Result analysis: because the reflected operation is a compile-time operation, that is, the operation of the runtime, C1==C2 returns True, stating that the generic type of the collection after compilation is de-generalized. then we can understand that the Java collection of generics, is used to prevent the error type element input, such as in List2 we add a int,add (10) will compile the error, then this generic can only be in the compilation phase valid, through the compilation phase, generics will not existed. Can be verified, we bypass the compilation, with reflection dynamic in the List2 add an int can be successful, just at this time because List2 store a number of different types of data (string type, and int), you can not use For-each to traverse, Throws a type conversion error exception classcastexception
from for Notes (Wiz)
Reflection---Java advanced development must understand