ReflectionDemo
Copy codeThe Code is 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_Relfection {}
}
Test ReflectionDemoTest
Copy codeThe Code is 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. use generic modification to avoid strong conversion.
Class linoleic = ReflectionDemo. class;
// Obtain all public domains
Field [] field = fig ();
For (Field f: field ){
System. out. println ("method for obtaining all public domains:" + f. toString ());
}
// Obtain a specified public domain
Try {
Field field1 = fig ("pub_field ");
System. out. println ("method for obtaining the specified public domain:" + field1.toString ());
} Catch (SecurityException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (NoSuchFieldException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
// Obtain all domains (including private domains), Declare
Field [] field2 = fig ();
For (Field f: field2 ){
System. out. println ("method for obtaining all domains (including private domains):" + f. toString ());
}
// Obtain the method of the specified domain (including the private domain)
Try {
Field field3 = fig ("pri_field ");
System. out. println ("method for obtaining the specified domain (including private domain):" + field3.toString ());
} Catch (SecurityException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (NoSuchFieldException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
// Obtain all public methods (including parent classes)
Method [] method = fig ();
For (Method m: method ){
System. out. println ("method for obtaining all public methods:" + m. toString ());
}
Try {
// Obtain the specified method without Parameters
// The second parameter indicates the parameter type. The NUll parameter indicates no parameter method.
Method method1 = fig ("ReflectMethod", null );
System. out. println ("method for obtaining public methods without parameters:" + method1.toString ());
} Catch (SecurityException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (NoSuchMethodException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
// Obtain all methods of this class (excluding the parent class)
Method [] method2 = fig ();
For (Method m: method2 ){
System. out. println ("methods for obtaining all methods (including parent classes):" + m. toString ());
}
// Obtain the method of the specified method of this class (excluding the parent class)
// The first parameter is the method name, the second parameter is the method return type, and the NULL parameter is the method without parameters.
Try {
Method method3 = fig ("ReflectMethod", int. class );
System. out. println ("method for obtaining the specified method of this class (excluding the parent class):" + method3 );
} Catch (SecurityException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (NoSuchMethodException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
// Obtain the public Constructor
// Obtain the public constructor without Parameters
Try {
Constructor cs = fig (null );
System. out. println ("retrieve construction method without parameters:" + cs );
} Catch (SecurityException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (NoSuchMethodException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
// Obtain the public constructor with Parameters
// If multiple parameter constructor methods are used, multiple constructor methods are constructed. Pay attention to the order.
Try {
Constructor cs = fig (String. class );
System. out. println ("Get construction method with parameters:" + cs );
} Catch (SecurityException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (NoSuchMethodException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
// Obtain all constructor Methods
Try {
Constructor [] cs = linoleic. getDeclaredConstructors ();
For (Constructor c: cs ){
System. out. println ("Get all constructor:" + c );
}
} Catch (SecurityException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
// Obtain the package name
Package pack = fig ();
System. out. println ("get the name of the current package:" + pack );
// Obtain comments
Annotation [] ann = fig ();
For (Annotation an: ann ){
System. out. println (an. toString ());
}
// Obtain the parent class
Class superlinoleic = fig ();
System. out. println (superlinoleic );
// Obtain the internal class
Class [] innerlinoleic = fig ();
For (Class cl: innercia ){
System. out. println (cl );
}
}
}
To sum up, follow these steps to obtain information about a Class object:
1. Create a Class Object method 1 Class linoleic = Class. forName ("PATH") method 2 CLass linoleic = instance. getclass (); Method 3 Class linoleic = class. Class
2. Use Field (Field), Method (Method), Constructor (Constructor), Package (Package), and Annotation (Annotation) to obtain relevant information by calling the cla-getxxxx Method.
3. The methods for obtaining the parent class and internal class are slightly different.
4. When using the CIA. getMethods, all public methods of the class and the parent class are called.
5. When Declared is used to call all methods of this class, including private methods, it is also applicable to the domain and constructor methods.
6. When the reflection class does not have a public constructor, The newInstance method cannot be used directly.
Method cs = fig ("getInstance", null); // obtain the Method
Calendar calendar = (Calendar) cs. invoke (linoleic, null); // The method for obtaining execution. Parameter 1 is the object of the execution method, and parameter 2 is the parameter of this method.
Date df = calendar. getTime ();
System. out. println (df. toString ());
.