concept

What is Reflection?

The ability of a program to access, detect, and modify its own state or behavior.

What is Java reflection?

Java reflection refers to the fact that in the running state of a Java program, all properties and methods of any class can be obtained. For a given object, you can call any of its properties and methods. This method of dynamically retrieving the contents of a class and dynamically calling an object is called reflection.

The Java reflection mechanism provides the following functions

  1. Determine the class to which any object belongs at run time.
  2. Constructs an object of any class at run time.
  3. Determine which member variables and methods any class has at run time.
  4. Call a method of any object at run time.

Java’s reflection mechanism is mainly used to analyze class capabilities, and we can obtain information about the members and members of each type of program or assembly at run time. Common object types in programs are identified at compile time, and Java’s reflection mechanism creates objects dynamically at run time and calls their properties. So the core of reflection is that it dynamically loads classes or calls methods to access properties at runtime, and it doesn’t need to know in advance who the running object is.

Related classes of reflection mechanisms

The name of the class use
Class Class An entity that represents a class and represents classes and interfaces in a running Java application
The Field class Member variables representing the class (also known as attributes of the class)
Method the class A method that represents a class
Constructor class Represents the constructor of a class

In reflection, to get a Class or call a method of a Class, we first need to get the Class object of that Class.

Baidu encyclopedia

For a bytecode file. Class, although we know nothing about the bytecode file on the surface, the file itself records a lot of information. When Java loads a.class bytecode file, the JVM generates a Java.lang. class object representing the.class bytecode file, from which much of the basic information about the class can be obtained. This is the reflection mechanism. So in order to do reflection, you must first understand the Class Class.

Gets the API of the Class object

** First, use the class.forname static method. ** You can use this method to get Class objects when you know the full pathname of the Class.

Class clz = Class.forName("java.lang.String");
Copy the code

Second, use the.class method.

Class clz = String.class;
Copy the code

Third, use the getClass() method of the class object.

String str = new String("Hello");
Class clz = str.getClass();
Copy the code

Class Class

Class represents the entity of a Class and represents the Class and interface in a running Java application. There are many useful methods provided in this class, and they are briefly classified here.

API

Get class-related methods
methods use
asSubclass(Class clazz) Converts the object of the passed class to an object representing its subclasses
Cast Converts an object to an object that represents a class or interface
getClassLoader() Get the loader for the class
getClasses() Returns an array containing objects for all public and interface classes in that class
getDeclaredClasses() Returns an array containing objects for all classes and interface classes in that class
forName(String className) Returns the object of the class based on the class name
getName() Gets the full path name of the class
newInstance() Create an instance of the class
getPackage() Get the package for the class
getSimpleName() Gets the name of the class
getSuperclass() Gets the name of the parent from which the current class inherits
getInterfaces() Gets the class or interface that the current class implements
Gets property-related methods in a class
methods use
getField(String name) Gets a public property object
getFields() Get all public property objects
getDeclaredField(String name) Get a property object
getDeclaredFields() Get all property objects
Gets the annotation related methods in the class
methods use
getAnnotation(Class annotationClass) Returns the public annotation object in this class that matches the parameter type
getAnnotations() Returns all public annotation objects for the class
getDeclaredAnnotation(Class annotationClass) Returns all annotation objects in the class that match the parameter type
getDeclaredAnnotations() Returns all the annotation objects for the class
Gets constructor-specific methods in a class
methods use
getConstructor(Class… <? > parameterTypes) Gets the public constructor of the class that matches the parameter type
getConstructors() Gets all public constructors for the class
getDeclaredConstructor(Class… <? > parameterTypes) Gets the constructor of the class that matches the parameter type
getDeclaredConstructors() Gets all constructors for the class
Gets methods that are related to methods in a class
methods use
getMethod(String name, Class… <? > parameterTypes) Gets a public method of that class
getMethods() Gets all public methods of the class
getDeclaredMethod(String name, Class… <? > parameterTypes) Gets a method of that class
getDeclaredMethods() Get all methods of the class
Other important methods in the class
methods use
isAnnotation() Returns true for annotation type
isAnnotationPresent(Class<? extends Annotation> annotationClass) Returns true if the annotation type is specified
isAnonymousClass() Returns true if it is an anonymous class
isArray() Returns true if it is an array class
isEnum() Returns true if it is an enumerated class
isInstance(Object obj) Returns true if obj is an instance of this class
isInterface() Return true for interface classes
isLocalClass() Returns true if it is a local class
isMemberClass() Returns true if it is an inner class

The Field class

Field represents the member variables of the class (also known as attributes of the class).

methods use
equals(Object obj) Property equal to obj returns true
get(Object obj) Get the corresponding attribute value in obj
set(Object obj, Object value) Set the corresponding property value in obj

Method the class

Method represents the Method of the class.

methods use
invoke(Object obj, Object… args) Pass an object and its parameters to call the corresponding method of the object

Constructor class

Constructor represents the Constructor of a class.

methods use
newInstance(Object… initargs) Creates an object of the class based on the parameters passed