In Java and Android, we often use reflection to achieve some compatible purposes. Java native reflection is troublesome and inconvenient to use. For example, we want to tune the static method get Usermanager, using the native implementation as follows
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 no trouble to achieve it. This requires that the method name and parameters be determined to get the assessible of the methods object set to True invoke the Invoke method, and pass in the corresponding parameter to catch a series of exceptions which may be thrown out
So the reflection can be simple, of course, but also a lot 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, making it more direct and convenient for us to use.
With Joor, the above code can be shortened to one line.
Reflect.on (Usermanager.class). Call ("Get", Getapplicationcontext ());
reliance onJoor is not dependent. Using Joor, you need to add the two files (Reflect.java, Reflectexception.java) to the project.
API Introduction
reflectReflect.on packages A class or object that represents reflection on this class or object, the value of the class can be made of classes, or it can be the complete class name (containing the package name information) Reflect.create The constructor method used to invoke the previous class, there are two overloads, one with parameters, one without parameters Reflect.call method calls, passing in method names and parameters, and if there is a return value that requires a call to get reflect.get fetch (field and method return) value, type conversion, often used in combination with call and field Reflect.field Gets the property value correlation, needs to call get to obtain the value Reflect.set the setting property correlation.
reflectexception
The introduction of reflectexception avoids our catch too many exceptions, but also reduces the amount of vertical code, making the code a lot simpler. Reflectexception thrown, the following exception may have occurred. ClassNotFoundException illegalaccessexception illegalargumentexception instantiationexception InvocationTargetException nosuchmethodexception nosuchfieldexception SecurityException
In addition, Reflectexception belongs to the unchecked exception, the syntax does not need to be explicitly captured, but also need to be based on the actual situation, consider whether to explicitly catch the exception. using the sample Create an instance
String string = Reflect.on (String.class). Create ("Hello World")-get ();
access Properties (public,protected,package,private)
Char Pathseparatorchar = Reflect.on (File.class). Create ("/sdcard/droidyue.com"). Field ("Pathseparatorchar"). get ();
Modify Properties (final property can also be modified)
String SetValue = Reflect.on (File.class). Create ("/sdcard/drodiyue.com"). Set ("Path", "Fakepath"). Get ("path");
Call Method (Public,protected,package,private)
ArrayList ArrayList = new ArrayList ();
Arraylist.add ("Hello");
Arraylist.add ("World");
int value = Reflect.on (arrayList). Call ("Hugecapacity").
Implementation Principle
Reflect actually encapsulates the native Java reflect and masks extraneous details.
Take the fields method as an example, its internal implementation can be seen to invoke the Java native provided reflection-related code.
Public map<string, reflect> fields () {
map<string, reflect> result = new linkedhashmap<string, Reflect> ();
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;
}
Library AddressJoor
These are the above, hope Joor can be helpful to the development of our daily.