1

作为我使用名为 JIST/SWANS 的工具运行的一些模拟的一部分,我遇到了一些奇怪的错误。这个模拟器是为 Java 1.4 编写的,我正在尝试将它移植到 1.5。

我要做的是用 1.5 SDK 编译原始代码。问题是模拟器使用bcel重写字节码,以便 JVM 可以用于模拟。当我在新的 SDK 下编译代码时,我得到下面给出的错误。有人可以指出我正确的方向来解决这个问题吗?我知道 1.4 和 1.5 生成的字节码有些不同,但我不知道从哪里开始寻找。

java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.ArrayList.remove(ArrayList.java:390)
    at org.apache.bcel.verifier.structurals.OperandStack.pop(OperandStack.java:135)
    at org.apache.bcel.verifier.structurals.ExecutionVisitor.visitPUTFIELD(ExecutionVisitor.java:1048)
    at org.apache.bcel.generic.PUTFIELD.accept(PUTFIELD.java:78)
    at jist.runtime.RewriterFlow.execute(RewriterFlow.java:235)
    at jist.runtime.RewriterFlow.doFlow(RewriterFlow.java:187)
    at jist.runtime.RewriterTraversalContinuableMethods.doMethod(Rewriter.java:3059)
    at jist.runtime.ClassTraversal.processMethodGen(ClassTraversal.java:136)
    at jist.runtime.ClassTraversal.processClassGen(ClassTraversal.java:96)
    at jist.runtime.ClassTraversal.processClass(ClassTraversal.java:63)
    at jist.runtime.Rewriter.rewriteClass(Rewriter.java:621)
    at jist.runtime.Rewriter.findClass(Rewriter.java:410)
    at jist.runtime.Rewriter.loadClass(Rewriter.java:367)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
    at java.lang.Class.getDeclaredMethod(Class.java:1935)
    at jist.swans.app.AppJava.findMain(AppJava.java:86)
    at jist.swans.app.AppJava.<init>(AppJava.java:61)
    at driver.aodvtest.createNode(aodvtest.java:192)
    at driver.aodvtest.createSim(aodvtest.java:235)
    at driver.aodvtest.main(aodvtest.java:277)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at jist.runtime.Bootstrap$JavaMain.startSimulation(Bootstrap.java:163)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at jist.runtime.Controller.processEvent(Controller.java:650)
    at jist.runtime.Controller.eventLoop(Controller.java:428)
    at jist.runtime.Controller.run(Controller.java:457)
    at java.lang.Thread.run(Thread.java:619)

java.lang.NullPointerException
    at driver.aodvtest.createNode(aodvtest.java:198)
    at driver.aodvtest.createSim(aodvtest.java:235)
    at driver.aodvtest.main(aodvtest.java:277)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at jist.runtime.Bootstrap$JavaMain.startSimulation(Bootstrap.java:163)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at jist.runtime.Controller.processEvent(Controller.java:650)
    at jist.runtime.Controller.eventLoop(Controller.java:428)
    at jist.runtime.Controller.run(Controller.java:457)
    at java.lang.Thread.run(Thread.java:619)

更新: 我从抛出异常的地方缩小到这一行:

private Method findMain(Class<?> c) throws NoSuchMethodException {
  return c.getDeclaredMethod("main", new Class<?>[] { String[].class });
}

类中有一个main方法正在传递给这个函数,所以我不知道它为什么返回一个null.

更新 2:

有一个功能:

createNode(..., ..., ..., ..., MyClient.class, ..., ...)

它将 传递MyClient.class给在上面发布的方法中使用 this 的函数findMain。使用调试器,我可以看到declaredMethodsnull明显getDeclaredMethods调用正在消亡。该类MyClient通过以下方式定义为内部静态类:

public static class MyClient {
   public static void main(String[] args[]) {

      ...

   }
}

我不确定这是否与declaredMethods存在有关,null所以我尝试将该类提取到一个单独的类中,但没有运气。

更新 3:

好的缩小范围。以下即使在主类中也会引发异常:

System.out.println(MyClient.class.getDeclaredMethods());
4

1 回答 1

1

也许下载BCEL的源代码并在该方法的第135行放置一个断点

org.apache.bcel.verifier.structurals.OperandStack.pop(OperandStack.java:135)

会告诉你更多。显然,数组索引操作存在问题。

据我所知,BCEL 不再处于开发阶段,所以如果存在错误,如果您报告它可能很难修复它。

ASM是一个更现代的字节码生成库,正在开发中。您可能已经知道这一点;但是如果你有那个模拟器的源代码并且它没有过多地使用 BCEL,你也许可以使用 ASM 重写它。当然,像这样的东西通常是矫枉过正的。

于 2010-11-30T05:45:50.350 回答