This is the 18th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

What is reflection

Both class and interface data types in Java are of class type. For each class loaded, the JVM creates an instance of class type for it and associates it with it. Each Class instance of the JVM points to a data type (Class or interface). A Class instance contains complete information about that Class. That is, every time a Class is loaded, a Class example is loaded. We get the class instance of that class and then get the method information that the class holds is called reflection.

Getting a Class instance

Three ways to get an instance of a class

  • String.class
  • object.getClass()
  • Class.forName()

There is only one instance of class in the JVM, so you can use == directly

Access the class field

Get field information from Class instance:

  • GetField (name): Get a public field(including the parent class)
  • GetDeclaredField (name): Gets a field of the current class (excluding the parent class)
  • GetFields (): Get all public fields (including parent classes)
  • GetDeclaredFields (): Get all fields of the current class (excluding the parent class)

The Field object contains all the information about a Field:

  • getName()
  • getType()
  • getModifiers()

Get (Object obj) get the value of the Object field! set(Object, Object)

You can tell from the code; A reflected field is a utility class that needs to be applied to the desired object and has no properties of its own

Access to the Field via reflection requires rules set through the SecurityManager.

Non-public fields are accessed by setting setAccessible(true).

Method

Get Method information from Class instance:

  • getMethod(name, Class…) Gets the class after a public method(including its parent class) that represents the method’s parameter types
  • getDeclaredMethod(name, Class…) Gets a method of the current class (excluding the parent class)
  • GetMethods (): Get all public methods (including parent classes)
  • GetDeclaredMethods (): Get all methods of the current class (excluding the parent class)

The Method object contains all information about a Method:

  • getName()
  • getReturnType()
  • getParameterTypes()
  • getModifiers()

Call the Method:

  • Object invoke(Object obj, Object… args)

Access non-public methods by setting setAccessible(true). Reflection calls to Method also obey the rules of polymorphism.

Constructor

Class.newinstance () can only call public parameterless constructors, but arguments cannot be used this way; instead, use a Constructor object

Get Constructor information from the Class instance:

  • getConstructor(Class…) Get a public Constructor
  • getDeclaredConstructor(Class…) : Gets a Constructor
  • GetConstructors (): Get all public constructors
  • GetDeclaredConstructors (): Get all Constructor

Create an instance object using the Constructor instance:

  • newInstance(Object… parameters)

Access non-public constructors by setting setAccessible(true).

Get inheritance

Get the Class of the parent Class:

Class getSuperclass()

  • The parent of Object is NULL
  • Interface’s parent class is NULL

Get the interface directly implemented by the current class:

  • Class[] getInterfaces()
  • Indirectly implemented interfaces are not included
  • A class with no interface returns an empty array
  • Interface Returns the inherited interface

To judge whether an upward transition is true:

  • bool isAssignableFrom(Class)