java-based (Reflection-- reflection)

 Reflection

  - means that can be loaded to run, Discovery, completely unknown during use compiled classes.
  - programs running state, you can only load a dynamic class name, for any loaded classes, are able to know all the properties and methods of this class;
     For any object, we are able to call any of its methods and properties;
    Class c = Class.forName("com.bjsxt.test.User");
  - After loading the class finished, the heap memory, generates a Class object type (only one of a class Class object), the object contains information about the structure of the entire class.
  We can see the structure of the class through this object. This object is like a mirror, see the structure of the class through this mirror, so we called the image: reflection.

Class class

  java.lang.Class very special class that represents the java type (class / interface / enum / annotation / primitive type / void) itself.
    - Object Class class contains the structure of a loaded class. A class that is loaded corresponds to a Class object.
    - When a class is loaded, or when defineClass loader (class loader) of () is invoked JVM, JVM will automatically generate a Class object.
  Class class is the source of Reflection.
    - for any class that you want to dynamically load, run, only to obtain the corresponding Class object
  Class object of acquisition:
    Use getClass ()
    Use Class.forName () (the most commonly used)
    .Class use of grammar

The role of reflection:

  • dynamic class loading, dynamic access class information (attribute, method, constructor)
  • dynamically construct objects
  • Dynamic call any of the methods of classes and objects, constructors
  • Dynamic calls and handling properties
  • Get generic information
  • Processing notes
 

Generic reflection operation (the Generic)

  • Java uses a generic mechanism to erase the introduction of generics. Generics in Java just to the compiler javac use, ensure data security and eliminating the mandatory conversion of the type of trouble.
   However, once compiled, and all of the relevant generic type all erased. • To operate by reflecting these types to meet the needs of the actual development, Java on added
    ParameterizedType, GenericArrayType, TypeVariable WildcardType and types to represent the type that can not be normalized, but in class Class
   Is another type of primitive types and par.
  • ParameterizedType: shows a parameterized type such as Collection <String>
  • GenericArrayType: represents an element type is an array type parameterized type or types of variables
  • TypeVariable: the interface is the common parent of all types of variables
  • WildcardType: represents a wildcard type expression, for example, extends Number, super Integer [wildcard is a word: is the "wildcard"]???
 

Reflection operation Annotations (Annotation)

  By reflection API: getAnnotations, getAnnotation obtain relevant information notes

// All valid annotation class obtained 
            the Annotation [] = Annotations clazz.getAnnotations ();
             for (the Annotation A: Annotations) { 
                the System. OUT .println (A); 
            } 
            // get the specified class annotation 
            SxtTable st = (SxtTable ) clazz.getAnnotation (SxtTable. class ); 
            . the System OUT .println (st.value ()); 
            
            // get the annotation attribute class 
            Field, F = clazz.getDeclaredField ( " studentName " ); 
            sxtField sxtField = f.getAnnotation ( . SxtField class ); 
            System.out.println(sxtField.columnName()+"--"+sxtField.type()+"--"+sxtField.length());

 

Reflection performance issues

• setAccessible
  - enable and disable the access switch security checks, the value of true indicates that the object is reflected in the use of the Java language access checks should be canceled.
    Value of false indicates that the object should be reflected in the Java language access checks embodiment. Not be able to access to true to false can not access.
  - No security check, you can improve the speed of reflection.
• consider using: cglib / javaassist bytecode manipulation </
 

Guess you like

Origin www.cnblogs.com/skyline1/p/11327363.html