1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import java.lang.reflect.*;
import java.util.Date;
public class Reflection
{
public static void main(String [ ] args){
System.out.println(call( "hashCode", new Date()));
}
public static Object call( String methodName, Object thisObject, Object... args){
Object back = null;
try
{
Class[] classArray = new Class[args.length];
for(int i = 0; i<args.length ; i++){
if(args[i]!=null) classArray[i] = args[i].getClass();
}
Method myMethode = thisObject.getClass().getMethod(methodName, classArray);
back = myMethode.invoke(thisObject, args);
} catch(Exception e) {
e.printStackTrace();
}
return back;
}
} |
Partager