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--
In view of what reflection can do, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
In Java, if you want to manipulate the methods and properties of an object at runtime, one of the effective means is reflection. This is also the most common tool used by the framework because the framework does not know which classes in the system will be used at compile time.
The type information of objects in Java is represented by Class objects at run time, Class objects are instantiated with class loading, and the implementation of reflection revolves around Class objects.
There are many ways to get the Class object of a class, including:
Class C1 = Object.class; Class c2 = Class.forName ("java.lang.Object"); Class c3 = new Object () .getClass ()
What can reflexes do?
1. Instantiate object
Objects can be instantiated directly by the new keyword or by reflection, for example:
Class.newInstance () class.getConstructor (Class... ParameterTypes) .newInstance (Object... Initargs)
Why do you need reflection for object instantiation?
There are scenarios where objects cannot be instantiated directly using the new keyword. For example, Bean managed by the Spring container can only load the class by its fully qualified name, and then reflect the instantiation.
In scenarios where you do not want to use the new keyword, the goal is to simplify programming and make the code beautiful. You may often see similar usage, such as:
Public static T parseObject (String text, Class clazz) {return parseObject (text, clazz, new Feature [0]);} / * simply copy a new type of object * / public static D map (S source, Class destinationClass) {return mapper.map (source, destinationClass);}
two。 Filter the appropriate class
In actual development, there is often such a need to do so-and-so if the class has the characteristics of so-and-so. When Spring is scanned, we use filters to finely control the generation of bean, including:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Judge whether to implement an interface or inherit a special parent class according to isInstance (Object obj)
It is determined whether or not to be annotated according to isAnnotationPresent (Class annotationClass).
3. Method call
Some scenarios cannot or are not suitable to directly call methods. For example, when we deal with HTTP requests, we need to map from URI to method calls. If we can enumerate all the mapping relationships between URL to object methods, that's fine, but countless if conditions are obviously not a wise choice.
Usually we get the resource object first, and then reflect the method of the calling object.
Method.invoke (Object obj, Object... Args)
How do I get the object's method object, the Method object? the Class class provides the following implementation:
Method [] getMethods (); Method [] getDeclaredMethods (); Method getMethod (String name, Class...) ParameterTypes); Method getDeclaredMethod (String name, Class...) ParameterTypes)
These methods can be divided into two categories:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
The Declared in the method signature is looked up in all the methods of the current class, but does not traverse the parent class.
Those without Declared iterate through all the parent classes, but only look for the public method.
Recommended utility class: the org.apache.commons.lang3.reflect.MethodUtils class contains convenient operations to traverse all parent class lookup methods, current class lookup public methods, or reflection execution methods.
4. Attribute operation
Class.getFields (), Class.getField (String), Class.getDeclaredFields (), Class.getDeclaredField (String)
The naming rule is the same as the method, and it is recommended to use the tool class: org.apache.commons.lang3.FieldUtils for reading or assigning values.
There are several points to note for reflection assignment:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
If it is not of type public, the access permission must be set through the Field.setAccessible (true) method before setting the field, otherwise an exception will be thrown: IllegalAccessException.
If the field is of type static, the obj object is ignored when assigned through the set method, because the static field belongs to the class.
If the field is of type final, whether public or private, when assigning a value with the set method, it can only be called correctly after the access permission is set by setAccessible, otherwise an exception will be reported: IllegalAccessException. However, when you assign a set method to a final field, although the method is called normally, it does not change the value of the fianl field.
If the field is of type final static, an IllegalAccessException exception is always thrown when the set method assignment is made.
The answer to the question about what reflection can do is shared here. 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.