reflection

What is reflection

Reflection encapsulates the components of a class into other objects, and that’s how reflection works

  • Benefits:
    1. These objects can be manipulated while the program is running.
    2. Can be decoupled, improve the scalability of the program.

How to obtain the Class object

  1. Class.forname (” full Class name “) : Loads the bytecode file into memory and returns a Class object
    • The class name is defined in the configuration file. Read the file and load the class
  2. Class: obtained from the class attribute of the class name
    • Mostly used for passing parameters
  3. Object. GetClass (): The getClass() method is defined in the Object class.
    • A method of obtaining bytecode for an object

Conclusion: The same bytecode file (*.class) is loaded only once during a program run, and the class object is the same regardless of which way it is retrieved.

Class object function:

  • Access function:

    1. Get member variables

      Field[] getFields() : getFields(String name) : getDeclaredFields() : getDeclaredFields() Field getDeclaredField(String name) : Obtains the specified member variableCopy the code
    2. Get constructor

      Constructor<? >[] Constructor() Constructor<T>[] Constructor(class <? >... parameterTypes) Constructor<? >[] getDeclaredConstructor() Constructor<T> >... parameterTypes)Copy the code
    3. Get member method

      Method[] getMethods() Method[] getMethod(String name, class <? >... ParameterTypes) Method[] getDeclaredMethods() Method[] getDeclaredMethod(String name, class <? >... parameterTypes)Copy the code
  • Field: member variable

    • Operation:
      1. Set the value
        • void set(Object obj,Object value)
      2. Get the value
        • get(Object obj)
      3. Ignoring security checks for access modifiers (Violent reflection)
        • setAccessible(true)
  • (4) Constructor

    • Create an object
      • T newInstance (Object… initargs)
      • If you use the empty parameter constructor to create an object, the operation can be simplified: the newInstance method of the Class object
  • Method: Method object

    • Execution method
      • The Object invoke (Object obj, Object… args)
    • Obtain method name:
      • String getName: Gets the method name

annotations

Concept:

Annotations are also called metadata. A proxy level specification that is at the same level as classes, interfaces, and enumerations. It can be declared in front of packages, classes, fields, methods, local variables, method parameters, etc., and used to describe and comment these elements. (In short, it is used to describe a program. But for computers)

Description:

  • That program
  • Use annotation: @ annotation name

Classification of functions:

  1. Document: Generate documentation from metadata identified in code
  2. Code analysis: Code analysis by identifying metadata in the code
  3. Compile checking: Enables the compiler to perform basic compile checking through metadata identified in the code

Predefined annotations in the JDK

  • Override: checks whether the method marked by this annotation inherits from a parent class or interface
  • @deprecated: The content of the annotation, indicating that it is Deprecated
  • @suppresswarnings: SuppressWarnings (generally pass parameter “all”)

Custom annotations

  • Format:

    Yuan notes

    Public @interface Annotation name {}

  • Essence: The essence of an Annotation is an interface that inherits the Annotation interface by default

    Public interface name annotation extends Java. Lang. The annotation. The annotation {}

  • Properties: Abstract methods in an interface

    Requirements:

    1. Property return value type
      1. Basic data types
      2. String
      3. The enumeration
      4. annotations
      5. An array of the above types
    2. Defines attributes that need to be assigned when used
      1. If the default keyword is used to define the default initialization value of an attribute, the attribute assignment can be omitted when annotations are used.
      2. If only one attribute needs to be assigned and the attribute name is value, the value can be omitted and the value can be defined directly
      3. Array assignments are wrapped with {values. If there is only one value in the array, {} is omitted
  • Meta-annotation: a annotation used to describe the annotation

    • @target: Describes where the annotation can be used
    • @Retention: Describes the phase in which annotations are retained
    • Documented: Describe whether an annotation is extracted into an API document
    • Inherited: Describes whether an annotation is Inherited by a child class

Parsing annotations in the program: Gets the values of the attributes in the annotations

  1. Get the position object defined by the annotation (Class,method)

  2. Gets the specified annotation

    GetAnnotation (Class) // essentially generates a subclass implementation object for the annotation interface in memory

  3. Call the abstract method in the annotation to get the configured property values