好的,在过去的几天里,我一直在努力学习如何在Java中使用反射。
到目前为止,我做得还不错,但是当我尝试在类的实例上调用方法时,我会陷入停顿状态。
} else if(commandName.equals("speak")) {
try {
Object stream = getField("stream").get(client);
Object aStream = getField("aStream_834").get(client);
Class<? extends Object> streamC = stream.getClass();
Class<? extends Object> aStreamC = aStream.getClass();
Class<? extends Object> textInputClass = textInput.getClass();
Method createFrame = streamC.getDeclaredMethod("createFrame", int.class);
createFrame.invoke(stream, 4); // this is line 32
streamC.getDeclaredMethod("writeWordBigEndian", int.class).invoke(stream, 0);
int offset = stream.getClass().getDeclaredField("currentOffset").getInt(stream);
streamC.getDeclaredMethod("method425", int.class).invoke(stream, 0);
streamC.getDeclaredMethod("method425", int.class).invoke(stream, 0);
aStreamC.getField("currentOffset").set(client, 0);
textInputClass.getDeclaredMethod("method526", String.class, aStreamC).invoke(textInput, "Hello", aStream);
int buf = aStreamC.getField("buffer").getInt(aStream);
streamC.getDeclaredMethod("method441", int.class, byte.class, int.class).invoke(stream, 0, buf, offset);
streamC.getDeclaredMethod("writeBytes", int.class).invoke(stream, (stream.getClass().getField("currentOffset").getInt(stream) - offset));
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
//e.printStackTrace();
e.getCause().printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
我的错误如下:
java.lang.NullPointerException
at vscapeClient.Stream.createFrame(Stream.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at in.programm.bot.Bot.handleCommand(Bot.java:32)
at in.programm.bot.Gui$1.actionPerformed(Gui.java:35)
“客户”来自这里:
public static Object client = null;
public static Object textInput = null;
public static String pkg = "vscapeClient/";
public static void main(String args[]) {
try {
Gui.main(null);
URL[] urls = { new URL("jar:file:vscape.jar!/") };
URLClassLoader classLoader = URLClassLoader.newInstance(urls);
Class<?> c = classLoader.loadClass(pkg.replaceAll("/", ".") + "client");
textInput = classLoader.loadClass(pkg.replaceAll("/", ".") + "TextInput");
String[] strings = {""};
client = c.newInstance();
client.getClass().getDeclaredMethod("main", strings.getClass()).invoke(client, (Object) strings);
Field[] fields = client.getClass().getDeclaredFields();
Method[] methods = client.getClass().getDeclaredMethods();
for(Field f : fields) {
f.setAccessible(true);
}
for(Method m : methods) {
m.setAccessible(true);
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
} catch (SecurityException e1) {
e1.printStackTrace();
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (InvocationTargetException e1) {
e1.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
这可能不重要,但这是Stream.java的第46行(this.buffer [(this .......)
public byte[] buffer;
public int currentOffset;
public ISAACRandomGen encryption;
public void createFrame(int i)
{
this.buffer[(this.currentOffset++)] = ((byte)(i + this.encryption.getNextKey()));
}