1. Learn about class
PackageCom.inspire.reflection.Class_api;ImportJava.lang.reflect.Constructor;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Method; Public classClassutils { Public Static voidMain (string[] args) {String s= "ABC"; Printclassfieldmessage (s); Printclassmethodmessage (s); } /*** Print class information, member method *@paramobj information about the class to which the object belongs*/ Public Static voidprintclassmethodmessage (Object obj) {//Gets the information for the class , gets the class type of the classClass C1=obj.getclass ();//The object of which child class is passed, and C is the class type of the subclass//gets the name of the classSystem.out.println ("Class name is:" +c1.getname ()); //Get Method /*Method class, the object of the methods a member method is a method object The GetMethods method obtains all the public methods, including the parent class inherited from the Getdeclaredmetho The DS method obtains all the methods that are defined by the class itself. Do not ask for access rights*/method[] Methods=c1.getmethods ();//method[] Method=c1.getdeclaredmethods (); for(inti = 0; I <methods.length; i++) { //gets the class type of the return value type of the method (for example, returns a string type, String.class is returned)Class returntype=Methods[i].getreturntype (); System.out.print (Returntype.getname ()+" "); //gets the name of the methodSystem.out.print (Methods[i].getname () + "("); //gets the class type of the parameter list type obtained by the parameter typeClass[] Paramtype=methods[i].getparametertypes (); for(Class c:paramtype) {System.out.print (C.getname ()+","); } System.out.println (")"); } } /*** Print class information, member variables *@paramobj*/ Public Static voidprintclassfieldmessage (Object obj) {//Get member Variable /*** The member variable is also an object * Java.lang.reflect.Field * Field encapsulates the operation on member variables * The GetFields method obtains all the public's Variable information * Getdeclaredfields () Gets the member information of the class's own declaration (public/private)*/Class C1=obj.getclass ();//The object of which child class is passed, and C is the class type of the subclass//field[] Fields=c1.getfields ();Field[] Fields=C1.getdeclaredfields (); for(Field f:fields) {//gets the class type of the member variable typeClass fieldtype=F.gettype (); //get the name of the member variableString typename=F.getname (); System.out.println (TypeName+" "+FieldType); } } /*** Print information about the constructor of an object *@paramobj*/ Public Static voidprintclassconmessage (Object obj) {Class C=Obj.getclass (); /*The constructor is also the information that encapsulates the constructor in the object Java.lang.Constructor Getdeclaredconstructor gets the constructor of its own declaration*/constructor[] Constructors=c.getdeclaredconstructors (); //constructor[] Constructors=c.getconstructors (); for(Constructor constructor:constructors) {System.out.println (Constructor.getname ()+"("); //get the argument list of the constructorClass[] Paramtype=constructor.getparametertypes (); for(Class c1:paramtype) {System.out.print (C1.getname ()+","); } System.out.println (")"); } }}
2. Get the class object for a class
(1). Define a class
Public classStudent {Private intID; PrivateString name; Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } Private intAge ;}
(2). Get Class object
Public classClassDemo1 { Public Static voidMain (string[] args) {//How is an instance object of student represented? //Student student=new Student (); /*student is also an instance object, which is an instance object of class. How to express? Any class is an instance object of class, and there are three ways to represent it*/ //Mode one: Any class has an implied static member classClass c1=student.class; //Method Two: The object of this class is known, through the GetClass methodStudent student=NewStudent (); Class C2=Student.getclass (); /*C1,C2 represents the class type of the Student class (class type) Everything is the object class is also the object, is the Class class instance object This object we call this class type /c8>*/ //C1,C2 represent the class type of the student class, and a class can only be an instance object of classSystem.out.println (C1==C2);//true//Way Three: Try{Class C3=class.forname ("Com.inspire.reflection.Class_object. Student "); System.out.println (C2==C3);//true}Catch(ClassNotFoundException e) {e.printstacktrace (); } /*with the class type of the class, we can create an object of that class from the class type. That is, create an object from C1,C2,C3*/ Try{Student Student1=(Student) c1.newinstance (); //Calling MethodsStudent1.getage (); } Catch(instantiationexception e) {e.printstacktrace (); } Catch(illegalaccessexception e) {e.printstacktrace (); } }}
3. Calling methods by reflection
Public class Printer { publicvoid PrintA () { System.out.println ("No parameter Method ...") ); } Public void PrintA (int A,int b) { System.out.println (a+ ":" +B); }}
Public classMethoddemo { Public Static voidMain (string[] args) {Printer Printer=NewPrinter (); Class C=Printer.getclass (); Try { //Get MethodMethod method= C.getdeclaredmethod ("PrintA",int.class,int.class);//ways to get your own claims//C.getmethod ("PrintA", Int.class, Int.class);//methods to get public//reflection operation of the methodMethod.invoke (printer,1,3);//put back the return value of the methodSystem.out.println ("========================="); Method method1=c.getdeclaredmethod ("PrintA"); Method1.invoke (printer); } Catch(Exception e) {e.printstacktrace (); } }}
4. Understanding generics through reflection
generics in Java are prevented from being entered incorrectly. Valid only at compile time. The generics of the collection after compilation are de-generalized
Public Static voidMain (string[] args) {ArrayList<String> list1=NewArraylist<string>(); List1.add ("Hello"); ArrayList List2=NewArraylist<string>(); //System.out.println (C1==C2);//The result is true, stating that the generics of the compiled collection are de-generalizedClass c2=List1.getclass (); Try{Method Method=c2.getmethod ("Add", Object.class); //Note here: Although the above definition List1 is a string type, it is possible to add data of type intMethod.invoke (list1,100); System.out.println (List1.size ()); System.out.println (list1);//cannot traverse with foreach, type is different}Catch(Exception e) {e.printstacktrace (); } }
Spring Base Reflection (Reflection)