Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the purpose of reflection in JDK

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the role of reflection in JDK". In daily operation, I believe that many people have doubts about the role of reflection in JDK. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the question of "what is the effect of reflection in JDK?" Next, please follow the editor to study!

In the running state, we can restore all the information of the class according to the "partial known information of the class".

Some of the known information about the

Class name

Objects of the class

All the information about the class

Attribute

Method

Inheritance relationship

Annotation comments

Construct class code sample User based on class name public class User implements Serializable {private static final long serialVersionUID = 1510634274152200118L; private int id; private String passWord; public User () {System.out.println ("Create user... Public User (int id, String passWord) {this.id = id; this.passWord = passWord;} public int getId () {return id;} public void setId (int id) {this.id = id;} public String getPassWord () {return passWord;} public void setPassWord (String passWord) {this.passWord = passWord @ Override public String toString () {return "User [id=" + id + ", passWord=" + passWord + "]";}}

Construct the code for the class based on the class name:

@ Testpublic void testReflection () throws Exception {Class clazz = Class.forName ("test.User"); User user = (User) clazz.newInstance (); System.out.println ("user =" + user);}

Output:

Create user... User = User [id=0, passWord=null] get class object @ Testpublic void testClazz () throws Exception {Class clazz1 = Class.forName ("test.User"); Class clazz2 = User.class; Class clazz3 = new User () .getClass (); System.out.println ("clazz1:" + clazz1); System.out.println ("clazz2:" + clazz2); System.out.println ("clazz3:" + clazz3);}

Output:

Create user... Clazz1: class test.Userclazz2: class test.Userclazz3: API of class test.Userclass

Constructor function

Member method

Member variable

Other information about the class (such as comments, package names, class names, inheritance relationships, etc.)

Constructor / / get public constructor public Constructor getConstructor (Class [] parameterTypes) / / get all public constructor public Constructor [] getConstructors () / / get "parameter is parameterTypes" and is the constructor declared by the class itself, including public, protected, and private methods. Public Constructor getDeclaredConstructor (Class [] parameterTypes) / / gets all the constructors declared by the class itself, including the public, protected, and private methods. Public Constructor [] getDeclaredConstructors () / / if the class is "an inner class in the constructor of another class," call getEnclosingConstructor () is the constructor of the class; if it doesn't exist, return null. Public Constructor getEnclosingConstructor ()

Test:

Testpublic void testConstructor () throws Exception {Class clazz = Class.forName ("test.User"); Constructor constructor = clazz.getDeclaredConstructor (null); Object object1 = constructor.newInstance (); System.out.println (object1); Constructor constructor2 = clazz.getDeclaredConstructor (new Class [] {int.class, String.class}); Object object2 = constructor2.newInstance (1, "123456"); System.out.println (object2);}

Output:

Create user... User [id=0, passWord=null] User [id=1, passWord=123456]

You can call the default constructor or you can call the

Clazz.getDeclaredConstructor (new Class [] {int.class, String.class})

To call the parameter constructor of User

Public User (int id, String passWord) {this.id = id; this.passWord = passWord } member method / / get the public function of "name is name, argument is parameterTypes" (including all public functions inherited from base class and implemented from interface) public Method getMethod (String name, Class [] parameterTypes) / / get all public functions (including all public functions inherited from base class and implemented from interface) public Method [] getMethods () / get "name is name, parameter is parameterTypes" And are functions declared by the class itself, including public, protected, and private methods. Public Method getDeclaredMethod (String name, Class [] parameterTypes) / / gets all the functions declared by the class itself, including public, protected, and private methods. Public Method [] getDeclaredMethods () / / if the class is "the inner class of a method in another class," call getEnclosingMethod () the same method as the class; if it doesn't exist, return null. Public Method getEnclosingMethod ()

You can determine whether there is a method in the class, or you can call any method in the class (including private methods).

@ Testpublic void testMethod () throws Exception {Class clazz = Class.forName ("test.User"); Method [] declaredMethods = clazz.getDeclaredMethods (); for (Method method: declaredMethods) {System.out.println (method);} System.out.println (); Method printInfo = clazz.getDeclaredMethod ("printInfo", new Class [] {}); User user = (User) clazz.newInstance (); printInfo.invoke (user, null);}

Output:

Create user... Public java.lang.String test.User.toString () public int test.User.getId () public void test.User.printInfo () public void test.User.setId (int) public void test.User.setPassWord (java.lang.String) public java.lang.String test.User.getPassWord () User [id=0 PassWord=null] member variables / / get member variables of public with "name is name" (including all public member variables inherited from the base class and implemented from the interface) public Field getField (String name) / / get all public member variables (including all public member variables inherited from the base class and implemented from the interface) public Field [] getFields () / / get "name is name" And are member variables declared by the class itself, including public, protected, and private member variables. Public Field getDeclaredField (String name) / / gets all the member variables declared by the class itself, including public, protected, and private member variables. Public Field [] getDeclaredFields () @ Testpublic void testField () throws Exception {Class clazz = Class.forName ("test.User"); Field [] fields = clazz.getDeclaredFields (); for (Field field: fields) {System.out.println (field);} / / create and modify a private variable id User user = (User) clazz.newInstance (); Field field = clazz.getDeclaredField ("id") through reflection Field.setAccessible (true); field.set (user, 123); user.printInfo ();}

Output:

Private static final long test.User.serialVersionUIDprivate int test.User.idprivate java.lang.String test.User.passWordCreate user... User [id=123, passWord=null]

We can see the definition of id in User:

Private int id

In the test case, you can get the private id variable in the object, modify the content directly, and finally output the id=123.

Other information comments on the class / / get comments of the "annotationClass" type of the class (including all public member variables inherited from the base class and implemented from the interface) public Annotation getAnnotation (Class annotationClass) / / get all comments of the class (including all public member variables inherited from the base class and implemented from the interface) public Annotation [] getAnnotations () / / get all comments declared by the class itself (including public, Protected and private member variables) public Annotation [] getDeclaredAnnotations () "parent class" and "interface" related API// get all implemented interfaces public Type [] getGenericInterfaces () / / get parent class public Type getGenericSuperclass () here The study on "what is the role of reflection in JDK" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report