In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to achieve Java reflection Library", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve Java reflection Library" article.
In Java and Android, we often use reflection for compatibility purposes. The reflection provided natively by Java is troublesome and inconvenient to use. For example, we want to call the static method get of UserManager, using the following native implementation
Try {
Final Method m = UserManager.class.getMethod ("get", Context.class)
M.setAccessible (true)
M.invoke (null, this)
} catch (NoSuchMethodException e) {
E.printStackTrace ()
} catch (IllegalAccessException e) {
E.printStackTrace ()
} catch (InvocationTargetException e) {
E.printStackTrace ()
}
It's easy to achieve. One of these
You need to determine the method name and parameters to get the corresponding Method object
Set the assessible of the Method object to true
Call the invoke method, passing in the corresponding parameters
Catch a series of exceptions that may be thrown
So can reflection be simpler, of course, and it will be much simpler.
This is what this article wants to introduce, jOOR (Java Object Oriented Reflection), which is a simple encapsulation of the java.lang.reflect package, which makes it more direct and easy to use.
With jOOR, the above code can be shortened to one line.
Reflect.on (UserManager.class) .call ("get", getApplicationContext ())
Dependence
JOOR has no dependencies.
API introduction
Reflect
Reflect.on wraps a class or object to indicate reflection on the class or object, and the value of the class can be either Class or the full class name (including package name information)
Reflect.create is used to call the constructor of the previous class. There are two overloads, one with parameters and one without parameters.
Reflect.call method call, passing in method name and parameters. If there is a return value, you need to call get.
The value obtained by Reflect.get (returned by field and method) is related to type conversion. It is often used in combination with call and field.
Reflect.field gets the value of the attribute. You need to call get to get the value.
Related to Reflect.set setting properties.
ReflectException
The introduction of ReflectException not only avoids too many exceptions to catch, but also reduces the amount of vertical code, making the code concise. ReflectException thrown, the following exception may have occurred.
ClassNotFoundException
IllegalAccessException
IllegalArgumentException
InstantiationException
InvocationTargetException
NoSuchMethodException
NoSuchFieldException
SecurityException
In addition, ReflectException is a unchecked exception, which does not need to be explicitly caught syntactically, but you also need to decide whether or not to explicitly catch the exception according to the actual situation.
Use the example
Create an instance
String string = Reflect.on (String.class) .create ("Hello World") .get ()
Access attributes (public,protected,package,private is available)
one
Char pathSeparatorChar = Reflect.on (File.class) .create ("/ sdcard/droidyue.com") .field ("pathSeparatorChar") .get ()
Modify the property (the final property can also be modified)
one
String setValue = Reflect.on (File.class) .create ("/ sdcard/drodiyue.com") .set ("path", "fakepath") .get ("path")
Call method (public,protected,package,private can be used)
ArrayList arrayList = new ArrayList ()
ArrayList.add ("Hello")
ArrayList.add ("World")
Int value = Reflect.on (arrayList) .call ("hugeCapacity", 12) .get ()
Realization principle
Reflect actually encapsulates the native java reflect, shielding the irrelevant details.
Taking the fields method as an example, its internal implementation shows that the reflection-related code provided natively by java is called.
Public Map fields () {
Map result = new LinkedHashMap ()
Class type = type ()
Do {
For (Field field: type.getDeclaredFields ()) {
If (! isClass ^ Modifier.isStatic (field.getModifiers () {
String name = field.getName ()
If (! result.containsKey (name))
Result.put (name, field (name))
}
}
Type = type.getSuperclass ()
} while (type! = null)
Return result
}
The above is about the content of this article on "how to achieve Java reflection Library". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
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.