hallo,
ich habe eine klasse mit einer funktion die nicht public ist. und versuche diese per reflection aufzurufen.
und die klasse dazu, die per reflection darauf zugreifen soll:
ich wäre froh, wenn mir jemand sagen könnte, was ich falsch mache...
denn die funktion gibts definitiv und dennoch bekomme ich eine NoSuchMethodException?!
ich habe eine klasse mit einer funktion die nicht public ist. und versuche diese per reflection aufzurufen.
Java:
public class SecretClass {
private int visibleIndex = 0;
void setVisibleIndex(int newVisibleIndex) {
System.out.println("setting visible index to "+newVisibleIndex);
this.visibleIndex = newVisibleIndex;
}
int getVisibleIndex() {
return visibleIndex;
}
}
Java:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Hacking {
/**
* this function hacks up secret code ;o)
*/
public static void secretClass() {
Class<?> secretClass = SecretClass.class;
Object instance = new SecretClass();
Method[] methods = secretClass.getDeclaredMethods();
System.out.println("declared functions in class "
+ secretClass.getSimpleName());
for (int i = 0; i < methods.length; i++) {
try {
System.out.println(String.format("%s %s(%s);",
methods[i].getReturnType(), methods[i].getName(),
methods[i].getParameterTypes()[0]));
} catch (Exception e) {
System.out.println(String.format("%s %s();",
methods[i].getReturnType(), methods[i].getName()));
}
}
Method method;
try {
method = secretClass.getMethod("setVisibleIndex", int.class);
method.setAccessible(true);
System.out.println(method.invoke(instance, 12) + "\n");
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Hacking.secretClass();
}
}
ich wäre froh, wenn mir jemand sagen könnte, was ich falsch mache...
denn die funktion gibts definitiv und dennoch bekomme ich eine NoSuchMethodException?!