Look at the Github practice code here


Dynamic languages

Is a language that can change its structure at run time, meaning that code can change its structure at run time depending on certain conditions.

Static language

A language with an unchanging runtime structure as opposed to a dynamic language. Such as Java, C, C ++ Java is not a dynamic language, but has a certain dynamic, can be applied to the reflection mechanism of similar dynamic language features. This dynamic nature makes programming more flexible.

java Reflection

  • Reflection has made Java the key to a dynamic language. Reflection allows programs to use the Reflection API during execution to retrieve information about classes and directly manipulate objects’ internal properties and methods.
  • After the Class is loaded, an object of type Class (a Class has only one Class object) is created in the method area of the heap, which contains information about the entire Class. We can see the structure of the class through this object. This way of getting the structure of a class from its objects is called reflection.

Normal mode: import the required “package class” name –> Instantiate through new –> get the instantiated object

Reflection: Instantiate the object ->getClass() method ->get the full “package class” name

Functionality provided by the Java reflection mechanism

  1. Determine the class of any object at run time;
  2. Construct an object of any class at runtime;
  3. Determine which member variables and methods any class has at run time.
  4. Get generic information at run time;
  5. Call any object member variable and method at run time;
  6. Annotations are processed at runtime;
  7. Generate dynamic proxy;
  8. .

Advantages and disadvantages of Java reflection

Advantages: Dynamic object creation and compilation can be achieved, showing great flexibility. Disadvantages: Impact on performance. Using reflection is basically an interpretation operation where we can tell the JVM what we want to do and meet our requirements. The eel always performs the same operation directly.

  1. Java.lang. Class: Represents a Class
  2. Java. Lang. Reflect. Method: on behalf of the class of methods
  3. Java.lang.reflect. Field: Represents a member variable of the class
  4. Java. Lang. Reflect. Constructor: on behalf of the Constructor of a class
  5. .



Class Class

Object to obtain information about a class’s properties, methods and constructors, and which interfaces a class implements. For each Class, the JRE reserves an immutable object of type Class. A Class object contains a particular structure (Class/interface/enum/annotation/primitive type/void / []) of the relevant information.

  1. Class itself is also a Class;
  2. Class objects can only be created by the system;
  3. A loaded Class has only one Class instance in the JVM;
  4. A Class object corresponds to a. Class file loaded into the JVM;
  5. Each instance of a Class remembers that it was generated in that Class instance;
  6. Class provides a complete view of all loaded structures in a Class.
  7. The Class Class is the root of Reflection, and for any Class that you want to dynamically load and run, you have to get the corresponding Class object first.

Common methods of the Class Class

methods function
static CalssforName(String name) Returns a Class object with the specified Class name name
Object newInstance() Call the default constructor, which returns an instance of the Class object
getName() Returns the name of the entity (Class, interface, array Class, void) represented by this Class object
Calss getSuperCalss() Returns the Class object of the parent Class of the current Calss object
Calss[] getinterfaces() Gets the interface of the current Calss object
ClassLoader getCalssLoader() Returns the classloader for the class
Constructor[] getConstructors() Returns an array containing some Comstructor objects
Method getMothed(String name,Class… T) Returns a Method object whose parameter type is paramType
Field[] getDeclaredFields() Returns an array of Field objects

Has the type of a Class object

  1. Class: External classes, members (member inner classes, static inner classes), local inner classes, anonymous inner classes
  2. Interface: the interface
  3. [] : array
  4. Enum: enumeration
  5. Annotation: @interface
  6. Primitive Type: Primitive data type
  7. void

Java memory analysis

The process of class loading

When the main program uses a class that has not already been loaded into memory, the system initializes it through the following three steps.

  • Load: Reads the class file into memory and creates a java.lang. class object for it. This process is done by the class loader
  • Link: Merges the binary data of the class into the JRE
  • Initialize: The JVM is responsible for initializing a class

Class loading and the understanding of ClassLoader

  1. Load: Load the bytecode content of the class file into memory, convert this static data into a runtime data structure for the method area, and generate a java.lang.class object that represents this class.
  2. Linking: The process of merging the binaries of Java classes into the JVM’s running state
  • Validation: To ensure that the loaded class information complies with the JVM specification and that there are no security issues
  • Preparation: The formal allocation of memory for a class variable (static) and the setting of its default initial values. This memory will be allocated in the method area.
  • Resolution: The process of replacing symbolic references (constant names) in the virtual machine constant pool with direct references (addresses).
  1. Initialize the
  • The process of executing the constructor () method. The class constructor () method is generated by combining the assignment of all class variables in the class that are automatically collected at compile time with statements in the static code quickie. (Class constructors are constructors for class information, not constructors for class objects)
  • When initializing a class, if the parent class is not initialized, the initialization of the parent class must be triggered first
  • The virtual machine ensures that a class’s () methods are locked and synchronized correctly in a multithreaded environment.

Class initialization

  1. Method area (special heap) : Load the data of the class
  • A static variable
  • A static method
  • Constant pool
  • code
  • .
  1. Heap: Generates a Class object corresponding to a Class
  2. Stack: The main () method is executed to generate the object, which takes the value of Class and initializes it

  1. Load into memory and generate a Class object corresponding to the Class
  2. Link, after the link properties have default values
  3. Initialize, call (){} method to assign value

Conditions under which initialization occurs

  1. Active reference to a class (class initialization must occur)
  • When the virtual machine starts, initialize the class in which the main() method resides
  • New An object of a class
  • Call static members of the class (except final constants) and static methods
  • Make reflection calls to classes using the Java, lang.Reflect package methods
  • When initializing a class, if the parent class is not initialized, the parent class is initialized first
  1. A passive reference to a class (class initialization does not occur)
  • When accessing a static field, only classes that actually declare the field are initialized. For example, when referring to a static variable of a parent class through a subclass, it does not cause the subclass to be initialized
  • Defining a class reference through an array does not trigger initialization of the class
  • Referring to constants does not trigger initialization of this class (constants are coarsedly in the constant pool of the calling class during the connection phase)

The role of the class loader

  1. The class loader loads the bytecode content of the class file into memory, converts this static data into the runtime data structure of the method area, and then generates a java.lang. class object representing this class in the heap pair as an access point to the class data in the method area.
  2. Class caching: The standard javaSE class loader can look up classes on demand, but once a class is loaded into the class loader, it keeps loading (caching) for a while. However, the JVM garbage collection mechanism can wave these Class objects.

.java>>>.class>>> Classloader >>> Bytecode validator >>> interpreter >>> operating system platform

Class loaders are used to load classes into memory. The JVM specification defines loaders for classes of the following types

Bootstrap class loader: written in C++, is the JVM’s own class loader, responsible for the Java platform core library, used to load the core class library. The loader is not available directly

Extension class loader: responsible for jar packages in jre, lib, ext or -d java.ext.dirs specified directory jar packaging such as working library

System class loader: Is responsible for wrapping classes and JARS in a java-classpath or -djava.class. path directory into a working library. It is the most common loader.





What can a Class object do

  1. Create Class objects: Call newInstance () on the Class object
  • A class must have a no-parameter constructor
  • Class with sufficient constructor access
  • An operation can be instantiated only after the constructor of the class is explicitly called at operation time and arguments are passed in
  1. The steps are as follows:
  • GetDeclaredConstructor (Class… ParameterTypes gets the constructor of the specified parameter type for this class
  • Pass an array of objects to the constructor parameters that contain the parameters required by the constructor
  • Instantiate objects by Constructor

setAccessible

  1. Method and Field and Constructor objects both have setAccessible () methods
  2. SetAccessible Enables and disables access to security checks
  3. Objects with a true argument that are only reflected should be used without Java language access checks
  • Improve the efficiency of reflection. Set to true if reflection must be used in code that needs to be called frequently
  • Make private members that would otherwise not be accessible accessible
  1. A value of false indicates that the reflected object should perform Java access checking

Reflection operation type

  1. Java uses generic erasure to introduce generics. Generics in Java are only used by the compiler Javac to ensure data security and avoid casting problems, but once the compilation is complete, all types related to generics are erased
  2. To manipulate these types by reflection, Java adds ParameterizedType, GenericArrayType, TypeVariable, and WildCardType to represent types that cannot be grouped into a Class Class but are named the same as the original type
  3. ParameterrizedType: represents a parameterized type, such as Collectio
  4. GenericArrayType: Indicates the array type of an element whose type is a parameterized type or a type variable
  5. TypeVariable: is a common class parent excuse for variables of various types
  6. WildcardType: represents a WildcardType expression

Reflection operation annotation

ORM:Object Reflectionship Mapping –> Object mapping

  • Classes correspond to table structures
  • Properties correspond to fields
  • Objects correspond to records

You can use annotations and reflection to complete the mapping of class and table structures.



Github project code