In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What is the reflection mechanism of Java and how to obtain reflection? in order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
First of all, we have to know what is reflex?
After the class is loaded, an object of type Class is generated in the method area of heap memory. A class has only one Class object, which contains the complete structural information of the class. We can see the structure of the class through this object. This object is like a mirror, through which you can see the structure of the class, so we visually call it reflection, or dynamic (runtime) access to class information and call class methods.
Second (to get to the bottom of it) if you know what it is, you still need to know what "ingredients" make up reflection?
If you want to implement reflection, you need to use some classes, namely class,Constructor,Field,Method.
2.1 acquisition and use of Class objects
If you eat, pick up the bowls and chopsticks first, and if you reflect, you have to find Class first.
There are three ways to find Class
1 instantiate the object to get the Class object of the instance
Person person = new Student ()
Class s=person.getClass ()
2 get the Class object of the class through the fully qualified name of the class
Class S1 = Class.forName ("com.ma.reflect.Student")
3 through the class name .class
Class S2=Student.class
2.2 when you get the bowls and chopsticks, you have to serve the rice, and when you get the Class, you have to operate it and get the attributes.
Code case
Package com.ma.reflect;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;public class reflectTest6 {public static void main (String [] args) throws ClassNotFoundException, NoSuchFieldException {/ / reflection Class c=Class.forName ("com.ma.reflect.User"); System.out.println ("get package + class" + c.getName ()) / / get package + class System.out.println ("get class name" + c.getSimpleName ()); / / get class name / / get class attribute System.out.println ("-"); Field [] fields=c.getFields () / to the public class for (Field field: fields) {System.out.println ("get the Public attribute of the class" + field);} System.out.println ("-"); / / get all the class attributes Field [] f1=c.getDeclaredFields () For (Field field: F1) {System.out.println ("get all the properties of the class" + field);} System.out.println ("-"); / / get the value of the specified property Field name=c.getDeclaredField ("age"); System.out.println ("get the type of the specified attribute" + name) System.out.println ("-"); / / get the value of the specified method Method [] methods=c.getMethods (); System.out.println ("get all public methods"); for (Method method: methods) {System.out.println ("public class has" + method) } Method [] methods1=c.getDeclaredMethods (); System.out.println ("get all public methods"); for (Method method: methods1) {System.out.println ("all methods =" + method);} System.out.println ("-") System.out.println ("get all public constructors"); Constructor [] constructors=c.getConstructors (); for (Constructor constructor: constructors) {System.out.println ("public constructor" + constructor);} System.out.println ("get all constructors"); Constructor [] constructors1=c.getDeclaredConstructors () For (Constructor constructor: constructors1) {System.out.println ("all constructors" + constructor);}
After eating, I also want to learn to cook and find Class, but I want to get an instance of the Class object. Class user=Class.forName ("com.ma.reflect.User"); / / get Class object / / Construction object User user1= (User) user.newInstance (); / / No-parameter method System.out.println (user1); / / create parameter method Constructor constructor=user.getDeclaredConstructor (String.class,int.class,int.class) through constructor; User user2= (User) constructor.newInstance ("myt", 28213); System.out.println (user2)
2.4 call the method Class user=Class.forName ("com.ma.reflect.User") of the instance object of Class; / / get the Class object User user3= (User) user.newInstance (); / / pass reflection to get a method Method setName=user.getDeclaredMethod ("setName", String.class); / / invoke activate (object, parameter) setName.invoke (user3, "myt"); System.out.println (user3.getName ())
2.5 modify the private property of the class, because it is private, so you need to turn off the security monitoring of the program.
/ / call the method through reflection
User user4= (User) user.newInstance (); Field name=user.getDeclaredField ("name"); / / get attributes / / * modify permissions to modify private properties by turning off the program's security monitoring name.setAccessible (true); name.set (user4, "tym"); / / the properties of private cannot directly call and modify System.out.println (user4.getName ())
The performance of three reflections 3. 1 if you take different paths, you have to compare the comparison of different paths (performance comparison)
Common method, reflection, off the reflection of security detection
Public class reflectTest8 {/ / ordinary method call public static void test () {User user=new User (); Long startTime=System.currentTimeMillis (); for (int I = 0; I < 100000000; iSum +) {user.getName ();} Long endTime=System.currentTimeMillis (); System.out.println ("Common method" + (endTime-startTime) + "ms") } / / reflection method calls public static void test1 () throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {User user=new User (); Class c=Class.forName ("com.ma.reflect.User"); Method getName=c.getDeclaredMethod ("getName", null); Long startTime=System.currentTimeMillis (); for (int I = 0; I < 100000000; iTunes +) {getName.invoke (user,null) } Long endTime=System.currentTimeMillis (); System.out.println ("reflection method" + (endTime-startTime) + "ms");} / / reflection method off detection calls public static void test2 () throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {User user=new User (); Class c=Class.forName ("com.ma.reflect.User"); Method getName=c.getDeclaredMethod ("getName", null) GetName.setAccessible (true); Long startTime=System.currentTimeMillis (); for (int I = 0; I < 100000000; iTunes +) {getName.invoke (user,null);} Long endTime=System.currentTimeMillis (); System.out.println ("reflection method off Detection" + (endTime-startTime) + "ms") } public static void main (String [] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {test (); test1 (); test2 ();}}
Advantages and disadvantages of four reflections 4.1 advantages of reflection
Reflection improves the flexibility and expansibility of the Java program, reduces the coupling, and improves the adaptive ability. It allows programs to create and control objects of any class without having to hard-code the target class in advance
4.2 shortcomings of reflection
Because it is a JVM operation, there will be a degradation in performance.
It is easy to cause some confusion to the program source code.
The use of five reflexes
It is impossible to know which classes the object or class might belong to at compile time, and the program only relies on run-time information to discover the real information of the object and class.
This is the answer to the question about what is the reflection mechanism of Java and how to obtain reflection. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.