检测类文件时出错(asm.ClassWriter.getCommonSuperClass)

时间:2012-07-02 11:25:52

标签: java classloader instrumentation

在仪器上获得错误

java.lang.RuntimeException: java.lang.ClassNotFoundException: Deposit
    at org.objectweb.asm.ClassWriter.getCommonSuperClass(Unknown Source)
    at org.objectweb.asm.ClassWriter.a(Unknown Source)
    at org.objectweb.asm.Frame.a(Unknown Source)
    at org.objectweb.asm.Frame.a(Unknown Source)
    at org.objectweb.asm.MethodWriter.visitMaxs(Unknown Source)
    at com.jConSequence.instrumentor.methodProber.AdddataBaseDetailsInstructions$AdddataBaseDetailsMethodInstructions.visitMaxs(AdddataBaseDetailsInstructions.java:131)
    at org.objectweb.asm.ClassReader.accept(Unknown Source)
    at org.objectweb.asm.ClassReader.accept(Unknown Source)
    at com.jConSequence.instrumentor.PrintLoadedClasses.print(PrintLoadedClasses.java:31)
    at com.jConSequence.tooGUI.RevEnggToolGUI.jButton1ActionPerformed(RevEnggToolGUI.java:487)
    at com.jConSequence.tooGUI.RevEnggToolGUI.access$0(RevEnggToolGUI.java:471)
    at com.jConSequence.tooGUI.RevEnggToolGUI$1.actionPerformed(RevEnggToolGUI.java:127)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

在检测课程时

D:\workspaces\workspace\bankManagement\bin\Account.class
D:\workspaces\workspace\bankManagement\bin\ATM.class
D:\workspaces\workspace\bankManagement\bin\ATMCaseStudy.class
D:\workspaces\workspace\bankManagement\bin\BalanceInquiry.class
D:\workspaces\workspace\bankManagement\bin\BankDatabase.class
D:\workspaces\workspace\bankManagement\bin\CashDispenser.class
D:\workspaces\workspace\bankManagement\bin\Deposit.class
D:\workspaces\workspace\bankManagement\bin\DepositSlot.class
D:\workspaces\workspace\bankManagement\bin\Keypad.class
D:\workspaces\workspace\bankManagement\bin\Screen.class
D:\workspaces\workspace\bankManagement\bin\Transaction.class

我正在使用apache commonsIO将上述类作为字节流读取。

然后使用asm库检测它们。

最后将它们转换为类文件。

正是在仪器的这一点上我得到了这个错误任何人都可以解释原因和可能的解决方案。

2 个答案:

答案 0 :(得分:7)

发生此错误是因为ASM正在生成堆栈映射帧,对于某些字节码,堆栈映射帧必须包含两个类的公共超类。默认情况下,ASM通过通过Class.forName 加载类来实现此功能,然后使用反射实现正确的算法。据推测,ASM库无法从其类加载器加载您的Deposit类。

为避免错误,您需要使用SKIP_FRAMES(注意:Java 7使用的类文件版本51.0需要堆栈映射帧,因此如果您生成Java 7字节码,这是一个非选项),或者您需要继承ClassWriter并覆盖getCommonSuperClass。根据您的代码所做的事情,也许您可​​以使用字符串比较对代码进行硬编码,或者您可以采用ASM实现并将其重写为使用您指定的ClassLoader。

答案 1 :(得分:0)

我也遇到了这个问题。在尝试编写自己的 ClassWriter 并在互联网上搜索后,我终于找到了一个非常适合我的实现:SafeClassWriter.java

希望它会有所帮助。