1. The first major function of the Java reflection API is to obtainProgramThe internal structure of the runtime. This is a very practical function for the checking tool and debugger of the program. Just a dozen linesCodeYou can traverse the internal structure of a Java class, including the constructor, declared domain, and defined method. This is a powerful capability. With the java. Lang. Class class object, you can obtain the constructor, domain, and method in the class through the methods in it. The corresponding methods are getconstructor, getfield, and getmethod. The three methods also have the corresponding getdeclaredxxx version. The difference is that the getdeclaredxxx version method only obtains the elements declared by the class itself, and does not consider inheritance. The constructor, field, and method classes represent the constructor, field, and method classes. The methods in these classes can obtain the metadata of the corresponding structure.
2. classes to be tested
Java code
-
- PackageCom. Home. Action. Test. mode. reflection;
-
-
- ImportJava. util. Map;
-
- Public ClassCounter {
-
- Public IntCount;
-
- PublicMap <string, Object> map;
-
-
- PublicCounter (IntCount ){
-
- This. Count = count;
- }
-
-
- Public VoidIncrease (IntStep ){
-
- Count = count + step;
-
- System. Out. println ("Count :"+ Count );
-
- }
-
- }
Package COM. home. action. test. mode. reflection; import Java. util. map; public class counter {public int count; Public Map <string, Object> map; public counter (INT count) {This. count = count;} public void increase (INT step) {COUNT = count + step; system. out. println ("count:" + count );}}
3. Test Method
Java code
-
- PackageCom. Home. Action. Test. mode. reflection;
-
-
- ImportJava. Lang. Reflect. constructor;
-
- ImportJava. Lang. Reflect. field;
- ImportJava. Lang. Reflect. invocationtargetexception;
-
- ImportJava. Lang. Reflect. method;
-
- ImportJava. Lang. Reflect. parameterizedtype;
-
- ImportJava. Lang. Reflect. type;
-
-
- /**
-
- * Test reflection of basic classes
-
- * @ Author Li
-
- */
- Public ClassTest {
-
- @ Suppresswarnings("Rawtypes")
-
- Public Static VoidMain (string [] ARGs ){
-
- Try{
-
- // Constructor
- Constructor <counter> contructor = counter.Class. Getconstructor (Int.Class);
-
- System. Out. println ("Contructorname :"+ Contructor. getname ());
-
-
- Counter = contructor. newinstance (10);
- Counter. Increase (10);
-
-
- // Basic Method
-
- Method [] Methods = counter.Class. Getmethods ();
-
- For(Method: Methods ){
- System. Out. println ("Method :"+ Method. getname ());
-
- }
-
-
- Method method = counter.Class. Getmethod ("Increase",Int.Class);
- Method. Invoke (counter,6);
-
-
- // Attribute Value
-
- Field [] fields = counter.Class. Getfields ();
-
- For(Field: fields ){
- System. Out. println ("Fieldname :"+ Field. getname ());
-
- }
-
-
- // Set public for the Count type. Otherwise, an exception message is displayed.
-
- Field field = counter.Class. Getfield ("Count");
- System. Out. println ("Countfield :"+ Field. getname ());
-
-
- // Process generics
-
- Field mapfield = counter.Class. Getdeclaredfield ("Map");
-
- Type type = mapfield. getgenerictype ();
- System. Out. println ("Maptype :"+ Type. tostring ());
-
-
- If(TypeInstanceofParameterizedtype ){
-
- Parameterizedtype paramerizedtype = (parameterizedtype) type;
-
- Type [] actualtypes = paramerizedtype. getactualtypearguments ();
-
- For(Type T: actualtypes ){
-
- If(TInstanceofClass ){
-
- Class clz = (class) T;
- System. Out. println ("Classtype :"+ Clz. getname ());
-
- }
-
- }
-
- }
-
- }Catch(Securityexception e ){
- E. printstacktrace ();
-
- }Catch(Nosuchmethodexception e ){
-
- E. printstacktrace ();
-
- }Catch(Illegalargumentexception e ){
-
- E. printstacktrace ();
-
- }Catch(Instantiationexception e ){
- E. printstacktrace ();
-
- }Catch(Illegalaccessexception e ){
-
- E. printstacktrace ();
-
- }Catch(Invocationtargetexception e ){
-
- E. printstacktrace ();
-
- }Catch(Nosuchfieldexception e ){
- E. printstacktrace ();
-
- }
-
- }
-
- }