反射方法
for (Method methodSub : mxxManager.getClass().getDeclaredMethods()) {
if ("xxx".equalsIgnoreCase(methodSub.getName())) {
Class<?>[] types = methodSub.getParameterTypes();
if (types != null && types.length > 0) {
if ("int".equalsIgnoreCase(types[0].getName())) {
xxxMethod = methodSub;
}
}
}
}
xxxMethod.invoke(mxxManager, 123);
动态代理实现接口反射,并实例化
Class<?> myInterface = null;
Class<?>[] interfaceList = mXXXManager.getClass().getDeclaredClasses();
for (Class<?> methodSub : interfaceList) {
if ("android.net.xxxx$xxxx".equalsIgnoreCase(methodSub.getName())) {
myInterface = methodSub;
}
}
Object instance = Proxy.newProxyInstance(myInterface.getClassLoader(), new Class<?>[]{myInterface}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//Handle the invocations
if(method.getName().equals("xxx")){
Log.d(TAG, "invoke: test");
}
else {
return -1;
}
return -1;
}
});