Java reflection, java
Package com. reflect;
Import java. lang. reflect. Method;
Public class ReflectUitl {
Public static Method getMethodDemo (Class clazz, String methodName,
Final Class [] classes ){
Method method = null;
// MethodName: method name, classes method type parameter data
Try {
Method = clazz. getDeclaredMethod (methodName, classes );
} Catch (NoSuchMethodException e ){
Try {
Clazz. getMethod (methodName, classes );
} Catch (NoSuchMethodException e1 ){
If (clazz. getSuperclass () = null ){
Return method;
} Else {
Method = getMethodDemo (clazz. getSuperclass (), methodName,
Classes );
}
}
}
Return method;
}
Public static Object invoke (Object obj, String methodname, Class [] classes,
Object [] object ){
Method method = getMethodDemo (obj. getClass (), methodname, classes );
Method. setAccessible (true );
Object inObj = null;
Try {
InObj = method. invoke (obj, object );
} Catch (Exception e ){
System. out. println (e. getMessage ());
}
Return inObj;
}
Public static Object invoke (final Object obj, final String methodName ){
Return invoke (obj, methodName, new Class [] {}, new Object [] {});
}
Public static Object invoke (final Object obj, final String methodName,
Final Class [] classes ){
Return invoke (obj, methodName, classes, new Object [] {});
}
Public static void main (String [] args) throws Exception {
ReflectUitl. invoke (new Demo2 (), "printlnB ");
}
// Demo2 class, customized.
}