Climbing the ladder: annotation and reflection compaction

Learning resources are organized from: Station B "Crazy God Talk"

Annotation

Use from JDK5.0

  • The role of annotations

    • It is not the program itself, but the program can be explained (similar to comments, used for explanation)
    • Can be read by other programs (compiler, etc.)
  • Annotation format

    • Annotation exists with the name of @note and can have parameters
  • General application scenarios

    • It can be attached to package, class, method, field, etc., to add additional auxiliary information to these elements, and this information can be obtained through the reflection mechanism

Meta annotation

  • The function of meta-annotation is to annotate other annotations. Java defines 4 standard meta-annotation types, which are used to provide explanations for other annotation types;

  • These types and the classes they support can be found in the java.lang.annotation package

    • @Target: used to describe the scope of the annotation (describe where the annotation can be used)
    • @Retention: Indicates at what level the annotation information needs to be saved and describes the life cycle of the annotation
      • (SOURCE < CLASS < RUNTIME )
    • @Document: Explain that the annotation will be included in the javadoc
    • @Inherited: indicates that the subclass can inherit the annotation in the parent class, the annotation inheritance

Custom annotation

  • Use @interface declaration annotation, public @interface name{}
  • Automatically inherit the java.lang.annotation.Annotation interface
  • Each method actually declares a configuration parameter, the method name is the parameter name, and the return value type is the parameter type (the return value can only be the basic type, Class, String, enum)
  • The default value of the parameter can be declared by default
  • If there is only one parameter member, it is generally named value
  • The annotation element must have a value. When we define the annotation element, we often use the empty string 0 as the default value.
    Insert picture description here

Reflection

Reflection overview

  • Reflection is the key to java being regarded as a dynamic language. The reflection mechanism allows the program to catch the Reflection Api during execution to obtain any internal information of the class, and to directly manipulate the internal properties and methods of any object.
  • After loading the class, an object of type Class is generated in the method area of ​​the heap memory . This object contains the complete structure information of the class, so the structure of the class can be seen through this object. This object is like a mirror, so it is called reflection.

You can read the private modified classes and methods.

Functions provided by the java reflection mechanism:

  • Determine the class of any object at runtime
  • Construct an object of any class at runtime
  • Judge the member variables and methods of any class at runtime
  • Get generic information at runtime
  • Call member variables and methods of any object at runtime
  • Processing annotations at runtime
  • Generate dynamic proxy

Advantages of Java reflection:

  • It can realize dynamic compilation and creation of objects, reflecting great flexibility;

Disadvantages of Java reflection:

  • Has an impact on performance. Using reflection is basically an interpretation operation, and this type of operation is always slower than performing the same operation directly.

Class class and get Class instance

  • Class itself is also a class
  • Class objects can only be created by the system
  • A loaded class will only have one Class instance in the JVM
  • A Class object corresponds to a .class file loaded into the JVM
  • Through Class, all the loaded structures in a class can be completely obtained
  • The Class class is the root of Reflection. For any class you want to dynamically load and run, you need to obtain the corresponding Class object first.

Common methods of Class

Method name Description
static Class.forName(String name) Get the Class object of the specified class name
Object newInstance() Call the parameterless constructor to create an instance of the Class object
getName() Returns the name of the entity (class, interface, array class or void) represented by this Class object
Class getSuperClass() Return the Class object of the parent class of the current Class object
Class[] getInterfaces() Get the Class array of the interface of the current Class object
ClassLoader getClassLoader() Class loader that returns this class
Constructor[] getConstructors() Get the constructor array of the current Class object
Method getMothed(String name,Class …T) Return a Method object whose formal parameter type is paramType
Field[] getDeclaredFields() Get the field array of the current Class object

Method to obtain the Class class:

  • Get directly according to the class name
Class clazz = Person.class;
  • Obtained by the example:
Class calzz = sutdent.getClass();
  • According to the path of the class:
Class clazz = Class.forName("com.ssx.People");
  • The built-in basic data types can be used directly.
  • Can be obtained through ClassLoader

Full Type Class

Insert picture description here

Class loading and ClassLoader

When the program actively uses a class that has not been loaded into memory, the system will initialize the class through the following three steps
Insert picture description here

Looking back with reference to JVM will be clearer.

Boot class loader: rt.jar

Insert picture description here
Class loading demo
Insert picture description hereInsert picture description here

Get generics by reflection

  • Java uses the mechanism of generic erasure to introduce generics. Generics in Java are only used by the compiler javac to ensure data security and avoid forced type conversion issues, but once the compilation is complete, all types related to introspection Erase all
  • In order to manipulate these types through reflection, Java has added ParameterizedType, GenericArrayType, TypeVariable, and WildcardType to represent types that cannot be classified into the Class class but have the same name as the original type.
  • ParameterizedType: a parameterized type, such as: Collection
  • GenericArrayType: An array type whose element type is a parameterized type or type variable
  • TypeVariable: is the public parent interface of various types of variables
  • WildcardType: Represents a wildcard type expression

Insert picture description here

Get annotations by reflection

Annotation annotation = clazz.getAnnotations();//获取全部注解
clazz.getAnnotation(anno.class);
annotation.value()

summary

Guess you like

Origin blog.csdn.net/qq845484236/article/details/107924393