Summary of the concept, principle and usage of java reflection mechanism

This paper describes the concept, principle and usage of java reflection mechanism with examples. Share with you for your reference, as follows:

What is the reflection mechanism

Reflection mechanism is to know all the properties and methods of any class in the running state; For any object, you can call any of its methods and properties; This kind of dynamically acquired information and the function of dynamically calling object methods are called the reflection mechanism of Java language.

What can reflection mechanism do

The reflection mechanism mainly provides the following functions:

① Judge the class of any object at run time; ② Construct the object of any class at runtime; ③ Judge the member variables and methods of any class at run time; ④ Call the method of any object at run time; ⑤ Generate dynamic proxy.

API for reflection mechanism

Interface

Person class

Get class: 3 methods

Operation results:

Get all methods: getmethods()

Operation results:

Get all implemented interfaces: getinterfaces()

Operation results:

Get parent class: getsuperclass()

Operation results:

Get all constructors: getconstructors()

Operation results:

Get all attributes: getdeclaraedfields();

Operation results:

It can be seen that the attribute modifiers are: private, data type: string, name: ID / name

Create instance: newinstance()

The difference between getdeclaredfields and getfields

Getdeclaredfields () obtains all declared fields of a class, including public, private and protected, but excluding the declared fields of the parent class. Getfields () gets all the public fields of a class, including the parent class.

Small example

Operation results:

Actual combat 1: the method of obtaining object instances and operating objects through reflection

Operation results:

Practice 2: obtain the object field attribute through reflection and assign a value

Operation results:

The program crashed because the ID attribute is private and its value cannot be modified.

improvement:

Add idfield setAccessible( true );

The complete code is:

Operation results:

Practice 3: comprehensive training, reflection operation attributes and methods

Operation results:

Practice 4: static attributes and static method calls

Define util class

Complete small example:

Operation results:

When the parameter is of int type and integer type, the reflection acquisition method is different

① When the parameter is of type int

When obtaining the method, you need to use: int.class. Cannot use integer class. Will report an error.

② When the parameter is of type integer

When obtaining methods, you need to use: integer class。 Cannot use int.class Will report an error.

Create object instance

Person class

Create instance practice

Operation results

summary

Class class provides four public methods for obtaining the constructor of a class.

Constructor getconstructor (class [] params) returns a specific constructor with public attribute according to the constructor parameters. Constructor getconstructors() returns an array of constructors with public attribute. Constructor getdeclaraedconstructor (class [] params) returns an array of constructors with public attribute according to the constructor parameters, Return a specific constructor (regardless of public and non-public attributes). Constructor getdeclaraedconstructors() returns all constructor arrays in the class (regardless of public and non-public attributes)

Four methods of obtaining members

Method getmethod (string name, class [] parameters) returns a specific method with public attribute according to the method name and parameters. Method [] getmethods() returns an array of methods with public attribute. Method getdeclaredmethod (string name, class [] parameters) returns an array of methods with public attribute according to the method name and parameters, Return a specific method (regardless of public and non-public attributes) method [] getdeclaraedmethods() returns all method arrays in the class (regardless of public and non-public attributes)

Four methods to obtain member properties

Field getfield (string name) returns a specific member variable with public attribute according to the variable name. Field [] getfields() returns an array of member variables with public attribute. Field getdeclaredfield (string name) returns an array of member variables according to the variable name, Return a member variable (regardless of public and non-public attributes) field [] getdelcaredfield() returns an array composed of all member variables (regardless of public and non-public attributes)

Readers interested in more Java related content can view the special topics of this site: introduction and advanced tutorial of java object-oriented programming, tutorial of Java data structure and algorithm, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>