Commit 71f821e4 authored by Administrator's avatar Administrator

双段反射支持更多接口

parent cc4e32ab
...@@ -19,6 +19,10 @@ public class FreeReflection { ...@@ -19,6 +19,10 @@ public class FreeReflection {
private static Method getDeclaredConstructorMethod = null; private static Method getDeclaredConstructorMethod = null;
private static Method getDeclaredConstructorsMethod = null;
private static Method getDeclaredMethodsMethod = null;
private static Method getDeclaredFieldsMethod = null; private static Method getDeclaredFieldsMethod = null;
static { static {
...@@ -32,6 +36,12 @@ public class FreeReflection { ...@@ -32,6 +36,12 @@ public class FreeReflection {
getFiledMethod = Class.class.getDeclaredMethod("getDeclaredField", String.class); getFiledMethod = Class.class.getDeclaredMethod("getDeclaredField", String.class);
getDeclaredConstructorMethod = Class.class.getDeclaredMethod("getDeclaredConstructor", Class[].class); getDeclaredConstructorMethod = Class.class.getDeclaredMethod("getDeclaredConstructor", Class[].class);
getDeclaredFieldsMethod = Class.class.getDeclaredMethod("getDeclaredFields"); getDeclaredFieldsMethod = Class.class.getDeclaredMethod("getDeclaredFields");
//java.lang.Class.getDeclaredConstructors
getDeclaredConstructorsMethod = Class.class.getDeclaredMethod("getDeclaredConstructors");
getDeclaredMethodsMethod = Class.class.getDeclaredMethod("getDeclaredMethods");
} catch (Throwable t) { } catch (Throwable t) {
throw new RuntimeException(t); throw new RuntimeException(t);
} }
...@@ -52,6 +62,16 @@ public class FreeReflection { ...@@ -52,6 +62,16 @@ public class FreeReflection {
return clazz.getDeclaredMethod(methodName, parameterTypes); return clazz.getDeclaredMethod(methodName, parameterTypes);
} }
public static Method[] getDeclaredMethods(Class<?> clazz) throws SecurityException {
if (use()) {
try {
return (Method[]) getDeclaredMethodsMethod.invoke(clazz);
} catch (Throwable e) {
//ignore
}
}
return clazz.getDeclaredMethods();
}
public static Constructor<?> getDeclaredConstructor(Class<?> clazz, Class<?>... parameterTypes) throws NoSuchMethodException { public static Constructor<?> getDeclaredConstructor(Class<?> clazz, Class<?>... parameterTypes) throws NoSuchMethodException {
if (use()) { if (use()) {
...@@ -64,6 +84,19 @@ public class FreeReflection { ...@@ -64,6 +84,19 @@ public class FreeReflection {
return clazz.getDeclaredConstructor(parameterTypes); return clazz.getDeclaredConstructor(parameterTypes);
} }
public static Constructor<?>[] getDeclaredConstructors(Class<?> clazz) throws SecurityException {
if (use()) {
try {
return (Constructor<?>[]) getDeclaredConstructorsMethod.invoke(clazz);
} catch (Throwable e) {
//ignore
e.printStackTrace();
}
}
return clazz.getDeclaredConstructors();
}
public static Field getDeclaredField(Class<?> clazz, String field) throws NoSuchFieldException { public static Field getDeclaredField(Class<?> clazz, String field) throws NoSuchFieldException {
if (use()) { if (use()) {
try { try {
......
...@@ -425,7 +425,8 @@ public final class RposedHelpers { ...@@ -425,7 +425,8 @@ public final class RposedHelpers {
Class<?> clz = clazz; Class<?> clz = clazz;
boolean considerPrivateMethods = true; boolean considerPrivateMethods = true;
do { do {
for (Method method : clz.getDeclaredMethods()) { for (Method method : FreeReflection.getDeclaredMethods(clz)) {
//for (Method method : clz.getDeclaredMethods()) {
// don't consider private methods of superclasses // don't consider private methods of superclasses
if (!considerPrivateMethods && Modifier.isPrivate(method.getModifiers())) if (!considerPrivateMethods && Modifier.isPrivate(method.getModifiers()))
continue; continue;
...@@ -664,7 +665,8 @@ public final class RposedHelpers { ...@@ -664,7 +665,8 @@ public final class RposedHelpers {
} }
Constructor<?> bestMatch = null; Constructor<?> bestMatch = null;
Constructor<?>[] constructors = clazz.getDeclaredConstructors(); // Constructor<?>[] constructors = clazz.getDeclaredConstructors();
Constructor<?>[] constructors = FreeReflection.getDeclaredConstructors(clazz);
for (Constructor<?> constructor : constructors) { for (Constructor<?> constructor : constructors) {
// compare name and parameters // compare name and parameters
if (ClassUtils.isAssignable(parameterTypes, constructor.getParameterTypes(), true)) { if (ClassUtils.isAssignable(parameterTypes, constructor.getParameterTypes(), true)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment