jeudi 22 janvier 2015

Reflection. Executing non-static method with parameters

I need to execute some method via reflection


method to execute



public void someMethod(int value1, int value2, String str, int value3)
{
try
{
// some code
}
catch (Exception e)
{
e.printStackTrace();
}
}


This is execution code



public static void execute()
{
try
{
String className = "some.class";
String classPath = "some.pack.name" + ":" + "some.class";
PathClassLoader loader = new dalvik.system.PathClassLoader(classPath, ClassLoader.getSystemClassLoader());

Class clazz = Class.forName(className, true, loader);
Method method = clazz.getDeclaredMethod("someMethod", Integer.class, Integer.class, String.class, Integer.class);
Object object = clazz.newInstance();

method.invoke(object, 1, 2, "str", 3);
}
catch (Throwable e) {e.printStackTrace();}
}


But I gets error



01-22 21:44:52.286: W/System.err(10877): java.lang.NoSuchMethodException: someMethod [class java.lang.Integer, class java.lang.Integer, class java.lang.String, class java.lang.Integer]
01-22 21:44:52.286: W/System.err(10877): at java.lang.Class.getConstructorOrMethod(Class.java:472)
01-22 21:44:52.286: W/System.err(10877): at java.lang.Class.getDeclaredMethod(Class.java:640)


What I'm doing wrong?






Aucun commentaire:

Enregistrer un commentaire