Java reflection of the underlying technology

 Today I explain to you the basic technology of Java reflection, if wrong or bad can be a lot of talk made, I will make the appropriate changes, first thank you for the comments in advance! ! !

  What is reflection?

   It is a reflection bytecode class files can be reflected in the contents of information fields, methods, and other construction method, the object method invocation can also create, we call reflection technique.

  How to Obtain the Class object by reflection?

   There are three ways, 1. get a class object with Class

                            2. Get the class object by getClass () method

                        3. Get the class object through Class.forName ( "full class name")

 

   Three methods Class object obtained by reflection: Case

Class c1 = ReflectionBasic.class;
Class c2 = new ReflectionBasic().getClass();
Class c3 = Class.forName("reflection.getClassObject.ReflectionBasic");

System.out.println(c1);
System.out.println(c2);
System.out.println(c3);

 operation result:

 

 

 Note: the Class.forName () can be acquired in addition to its Class object can also be acquired class object is automatically loaded into memory to virtual machines JVM, the results can be seen in the following cases static static code block of a generic code output C4 and C5 of the object, but only the output of the static C5, which illustrates the use of the Class.forName () class object may be loaded into memory to virtual machines JVM.

  Case:

 // the output OtherTest reflecting object 
 Class OtherTest C4 = <?>. Class ; 
System.out.println ( "The output target is C4 ===================== ========== "+ C4); 
Class C5 = the Class.forName (<?>" reflection.getClassObject.OtherTest " ); 
System.out.println ( " The output target is C5 ==== =========================== "+ c4);
{OtherTest class 
static {
System.out.println ( "You okay !!! babies !!!");
}
}

   operation result:

 

 

  Obtaining information reflecting the class

  There are many types of information reflected in Bags java.lang.reflect

  Class information of the reflected

  Interface type:

public interface IReflection {
}

  father:

public class Reflection {
}

  Main categories:

public  class ReflectionInfo the extends the Reflection the implements IReflection {
     public  static  void main (String [] args) {
         // create an object class 
        Class C1 = ReflectionInfo <?>. class ;
         // Get Class ======== reflected ReflectionInfo ======================== modifier 
        int modifiers = c1.getModifiers (); // get is an integer then we need to convert the 
        System. Out.println (modifiers); 
        String sModifiers = Modifier.toString (modifiers); // converted information obtained 
        System.out.println (sModifiers); 
        System.out.println ("It is to obtain the above reflection ReflectionInfo class ================================ modifier" );
         // Get reflected ReflectionInfo ================================ full class name of the class 
        String name = c1.getName (); 
        System.out. the println (name); 
        // Get ================================ simple reflection ReflectionInfo class class name 
        String simpleName = c1.getSimpleName (); 
        System.out.println (SimpleName); 
        System.out.println ( "upper class acquisition ===================== reflection ReflectionInfo =========== full class name and the simple class name " );
         // get ======================= reflection ReflectionInfo class ========= the full name of the parent class 
        class the superclass for = <?> c1.getSuperclass (); 
        System.out.println (superclass.getSuperclass ()); 
        // Get Class ============================== reflected ReflectionInfo simple parent class name == 
        System.out.println (superclass.getSimpleName ()); 
        System.out.println ( "upper class acquisition ================= reflection ReflectionInfo =============== full name of the parent class and simplicity parent class name " );
         // get ================= reflection ReflectionInfo class =============== interfaces, because we need the interface may be a plurality of outputs out by traversing 
        Class [] = the interfaces <?> c1.getInterfaces ();
         IF (interfaces.length> 0 ) {
             for ( int I = 0; I <interfaces.length; I ++ ) {
                 // Get class ======================== reflected ReflectionInfo ======== complete interface name
                System.out.println (the interfaces [I]);
                 // Get Class =============================== reflected ReflectionInfo simple interface name = 
                System.out.println (the interfaces [I] .getSimpleName ()); 
            } 
        } 
    } 
}

operation result:

 

 

Access by reflection technology field (public and private sub-modified)

Error Case: get the value of private property by getFile () method, will complain

public  class AccessFiled {
     public  static  void main (String [] args) throws a NoSuchFieldException, IllegalAccessException, InstantiationException {
         // create Class objects 
        Class Company c1 = <?>. class ;
         // Return the field of public public 
        Field, Field, = c1.getField ( "name" );
         // create an object instance by the reflection technique, called by default constructor with no arguments Oh 
        Object obj = c1.newInstance (); // this method is equivalent to a new company object 
        field.set (obj, "unknown company" );   // set the name field value 
        System.out.println (obj); 
        System.out.println (Field.get (obj));// output name field value 

        // Return public fields public 
        Field, Fields c1.getField = ( "ID" );
         // create an object instance by the reflection technique, called by default constructor with no arguments oh 
        field.set (obj, "00001 ");   // set the field name value 
        System.out.println (Field.get (obj)); // output name field value 

    } 
} 
class Company {
     public String name;
     Private  int ID; 
}

operation result:

 

Proper case: use getDeclaredField () method to get private modified id value, this can get any modifiers modified field

   

public  class AccessFiled {
     public  static  void main (String [] args) throws a NoSuchFieldException, IllegalAccessException, InstantiationException {
         // create Class objects 
        Class Company c1 = <?>. class ;
         // Return the field of public public 
        Field, Field, = c1.getField ( "name" );
         // create an object instance by the reflection technique, called by default constructor with no arguments Oh 
        Object obj = c1.newInstance (); // this method is equivalent to a new company object 
        field.set (obj, "unknown company" );   // set the name field value 
        System.out.println (obj); 
        System.out.println (Field.get (obj));// output name field value 

        // Return public fields public 
        Field, Fields c1.getDeclaredField = ( "ID" );
         // create an object instance by the reflection technique, called by default constructor with no arguments oh 
        field.set (obj, "00001 ");   // set the field name value 
        System.out.println (Field.get (obj)); // output name field value 

    } 
} 
class Company {
     public String name;
     Private  int ID; 
}

operation result:

 

Calling method reflected by the technique

public  class callobject {
     public  static  void main (String [] args) throws a NoSuchMethodException, IllegalAccessException, an InstantiationException is, a InvocationTargetException {
         // Create Object Class 
        Class C1 = Company <?>. class ;
         // reflective Shou () method 
        Method method = c1. getMethod ( "Show", null ); // in front of the argument is the name of the method. The latter method is the return value type
         // by reflection to create instances 
        Object obj = c1.newInstance (); 
        System.out.println ( "no parameters ===========" + method.invoke (obj , null ));
        System.out.println (obj);
        Shous Method, = c1.getMethod (. "Shows", String class ); 
        shous.invoke (obj, "unknown Company" );
         // reflective Shows () method with (Type parameter list) 
        System.out.println ( "have parameters =========== "+ obj) ;; 
    } 
} 
class Company {
     public String name;
     Private  int ID; 

    public  void Show () { 
        System.out.println ( " Show method ===== " ); 
    } 
    public  void Shows (String name) { 
        System.out.println ( " Shows ====== method " );
         the this .name = name;
    }



    @Override
    public String toString() {
        return "Company{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }
}

operation result:

 

 

  Java reflection to explain the basis is completed, they do not know or do not understand, I can give a message in the comments section, I will promptly reply to everyone.

Guess you like

Origin www.cnblogs.com/mumu555/p/12528443.html