官方介绍
jOOR - Fluent Reflection in Java jOOR is a very simple fluent API that gives access to your Java Class structures in a more intuitive way. The JDK’s reflection APIs are hard and verbose to use. Other languages have much simpler constructs to access type meta information at runtime. Let us make Java reflection better. http://www.jooq.org/products
大意为:
jOOR提供了一种更为直观的方式来构建JDK原生的反射调用,因为JDK提供的反射API使用起来较冗长(它对包java.lang.reflect进行了简单封装,使得反射更加方便)。
gradle配置
compile'org.jooq:joor:0.9.6'
举例
先看一个简单例子,对比JDK的反射API和jOOR的方法调用对比。
JDK的反射API:
/**
* 反射读取系统SystemProperties的属性值
* @param key
* @param defaultValue
* @return
*/
public static String getValue(String key, String defaultValue) {
String value = "";
try {
Class<?> cls = Class.forName("android.os.SystemProperties");
Method getMethod = cls.getDeclaredMethod("get", String.class, String.class);
getMethod.setAccessible(true);
return (String) getMethod.invoke(null, key, defaultValue);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return value;
}
jOOR的反射调用:
/**
* 反射读取系统SystemProperties的属性值
* @param key
* @param defaultValue
* @return
*/
public static String getValueEx(String key, String defaultValue) {
String value;
try {
value = Reflect.on("android.os.SystemProperties")
.call("get", key, defaultValue).get();
} catch (ReflectException e) {
e.printStackTrace();
value = "";
}
return value