Summary of common methods for Java reflection to get class details

  • 2020-04-01 01:36:23
  • OfStack

Class ReflectionDemo


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{}  
} 

The test class ReflectionDemoTest



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 class objects, using generic modifiers to avoid strong rotation
        Class cla=ReflectionDemo.class; 
        //Method to get the entire public domain
        Field[] field=cla.getFields(); 
        for(Field f:field){ 
            System.out.println(" Method to get the entire public domain :"+f.toString()); 
        } 
        //Gets a method for a specified public domain
        try { 
            Field field1=cla.getField("pub_field"); 
            System.out.println(" Gets a method for 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(); 
        } 
        //Method to get all fields (including private fields),Declare
        Field[] field2=cla.getDeclaredFields(); 
        for(Field f:field2){ 
            System.out.println(" Method to get all fields ( Including private domain ):"+f.toString()); 
        } 
        //Method to get the specified domain (including private domain)
        try { 
            Field field3=cla.getDeclaredField("pri_field"); 
            System.out.println(" Gets a method for the specified field ( 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(); 
        } 

        //Method to get all public methods (including parent class)
        Method[] method=cla.getMethods(); 
        for(Method m:method){ 
            System.out.println(" Method to get all public methods :"+m.toString()); 
        } 
        try { 
            //Gets the specified parameter - free method
            //The second parameter represents the parameter type and the NUll parameter represents the no-parameter method
            Method method1=cla.getMethod("ReflectMethod", null); 
            System.out.println(" Method to get a public method without arguments :"+method1.toString()); 
        } catch (SecurityException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } catch (NoSuchMethodException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 

        //Methods to get all methods of the class (not including the parent class)
        Method[] method2=cla.getDeclaredMethods(); 
        for(Method m:method2){ 
            System.out.println(" Method to get all methods ( With the parent class ):"+m.toString()); 
        } 

        //Method to get the specified method of the class (without parent class)
        //The first parameter is the method name, the second parameter is the method return type, and NULL is the no-argument method
        try { 
            Method method3=cla.getDeclaredMethod("ReflectMethod",int.class); 
            System.out.println(" Gets the method for the class specified method ( Does not contain the parent class ):"+method3); 
        } catch (SecurityException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } catch (NoSuchMethodException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 

        //Gets the public constructor
        //Gets an argument - free public constructor
        try { 
            Constructor cs=cla.getConstructor(null); 
            System.out.println(" Gets the parameter - free constructor :"+cs); 
        } catch (SecurityException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } catch (NoSuchMethodException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 

        //Gets the argument public constructor
        //If more than one parameter of the constructor method, the constructor method is more than one, pay attention to the order
        try { 
            Constructor cs=cla.getConstructor(String.class); 
            System.out.println(" Gets the argument constructor :"+cs); 
        } catch (SecurityException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } catch (NoSuchMethodException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 

        //Gets all constructors

        try { 
            Constructor[] cs=cla.getDeclaredConstructors(); 
            for(Constructor c:cs){ 
            System.out.println(" Gets all constructors :"+c); 
            } 
        } catch (SecurityException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace();    
    } 
        //To get the package name
        Package pack=cla.getPackage(); 
        System.out.println(" Gets the name of the current package :"+pack); 

        //Get comments
        Annotation[] ann=cla.getAnnotations(); 
        for(Annotation an:ann){ 
            System.out.println(an.toString()); 
        }    
//Access to the parent class
  Class supercla=cla.getSuperclass();
  System.out.println(supercla);
  //Get inner class
  Class[] innercla=cla.getDeclaredClasses();
  for(Class cl:innercla){
   System.out.println(cl);

  }

} 
}    

Summary: steps to get the information of the Class object:

1. Create the Class object method 1 Class cla= class.forname (" PATH ") method 2 Class cla= instance. Getclass (); Method 3 Class cla= Class. Class

2. Use Field(Field),Method(Method),Constructor(Constructor), Package(Package), Annotation(Annotation) to get relevant information by calling cla.getxxxx Method

3. Parent and inner classes are obtained in slightly different ways

4. When using cla.getmethods, the class and all the public methods of the parent class are called

5. When all methods of the class are called using Declared, including private methods, this is also true in domain, constructor methods

6. When the reflection class has no public constructor method, it cannot directly use the newInstance method

                Cs = cla Method getMethod (" getInstance ", null); // get method
                Calendar Calendar = (Calendar) cs. Invoke (cla, null); // execute the method obtained, parameter 1 is the object to execute the method, parameter 2 is the parameter of the method
                The Date df = calendar getTime ();
                System. The out. Println (df) toString ());

In order to get.


Related articles: