使用ASM的类验证程序(org.objectweb.asm.util.CheckClassAdapter)进行类验证时出现ClassNotFound错误

时间:2012-07-09 20:43:33

标签: bytecode java-bytecode-asm

所以这是一个字节码片段,我试图使用ASM的类Verifier进行验证:

public <init>(Ljava/io/InputStream;)V
    ALOAD 0
    ALOAD 1
    SIPUSH 2048
    NEW org/apache/xerces/impl/msg/XMLMessageFormatter
    DUP
    INVOKESPECIAL org/apache/xerces/impl/msg/XMLMessageFormatter.<init> ()V
    INVOKESTATIC java/util/Locale.getDefault ()Ljava/util/Locale;
    INVOKESPECIAL org/apache/xerces/impl/io/UTF8Reader.<init> (Ljava/io/InputStream;ILorg/apache/xerces/util/MessageFormatter;Ljava/util/Locale;)V
    RETURN
    MAXSTACK = 5
    MAXLOCALS = 2

这是更大的类org / apache / xerces / impl / io / UTF8Reader的一部分。现在,当我通过ASM提供的类验证程序运行此类的字节码时,我得到以下结果:

org.objectweb.asm.tree.analysis.AnalyzerException: **Error at instruction 7: java.lang.ClassNotFoundException: org.apache.xerces.util.MessageFormatter**
    at org.objectweb.asm.tree.analysis.Analyzer.analyze(Unknown Source)
    at org.objectweb.asm.util.CheckClassAdapter.verify(Unknown Source)
    at org.objectweb.asm.util.CheckClassAdapter.verify(Unknown Source)
    at org.objectweb.asm.util.CheckClassAdapter.main(Unknown Source)
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: org.apache.xerces.util.MessageFormatter
    at org.objectweb.asm.tree.analysis.SimpleVerifier.getClass(Unknown Source)
    at org.objectweb.asm.tree.analysis.SimpleVerifier.isAssignableFrom(Unknown Source)
    at org.objectweb.asm.tree.analysis.SimpleVerifier.isSubTypeOf(Unknown Source)
    at org.objectweb.asm.tree.analysis.BasicVerifier.naryOperation(Unknown Source)
    at org.objectweb.asm.tree.analysis.BasicVerifier.naryOperation(Unknown Source)
    at org.objectweb.asm.tree.analysis.Frame.execute(Unknown Source)
    ... 4 more
<init>(Ljava/io/InputStream;)V
00000 UTF8Reader InputStream  :  :     ALOAD 0
00001 UTF8Reader InputStream  : UTF8Reader  :     ALOAD 1
00002 UTF8Reader InputStream  : UTF8Reader InputStream  :     SIPUSH 2048
00003 UTF8Reader InputStream  : UTF8Reader InputStream I  :     NEW org/apache/xerces/impl/msg/XMLMessageFormatter
00004 UTF8Reader InputStream  : UTF8Reader InputStream I XMLMessageFormatter  :     DUP
00005 UTF8Reader InputStream  : UTF8Reader InputStream I XMLMessageFormatter XMLMessageFormatter  :     INVOKESPECIAL org/apache/xerces/impl/msg/XMLMessageFormatter.<init> ()V
00006 UTF8Reader InputStream  : UTF8Reader InputStream I XMLMessageFormatter  :     INVOKESTATIC java/util/Locale.getDefault ()Ljava/util/Locale;
00007 UTF8Reader InputStream  : UTF8Reader InputStream I XMLMessageFormatter Locale  :     INVOKESPECIAL org/apache/xerces/impl/io/UTF8Reader.<init> (Ljava/io/InputStream;ILorg/apache/xerces/util/MessageFormatter;Ljava/util/Locale;)V
00008 ?        :     RETURN

我对最后一点非常困惑...为什么它会在第7行为org / apache / xerces / util / MessageFormatter对象发出ClassNotFound异常?

感谢。

1 个答案:

答案 0 :(得分:1)

ASM的验证工具会根据JVM规范检查每个堆栈和变量槽的有效性。其中一项检查是所有变量和堆栈槽中的值在合并点处具有相同的类型。因此,此实现加载类以查找常见的超类型。