如何避免“常量池中的非法类型”使用“ldc_w”在茉莉花?
我正在编写一个生成 Jasmin 代码的编译器 想要调用一个以 Class 作为参数的方法。
public class CTest
{
public static void main(String[] args)
throws Exception
{
java.lang.reflect.Array.newInstance(CTest.class, 0);
}
}
所以在Jasmin中,我认为应该是:
.class public CTest2
.super java/lang/Object
.method public static main([Ljava/lang/String;)V
.limit stack 2
.limit locals 1
ldc_w CTest2
iconst_0
invokestatic java/lang/reflect/Array/newInstance(Ljava/lang/Class;I)Ljava/lang/Object;
pop
return
.end method
当我组装它并运行它时,我得到:
Exception in thread "main" java.lang.VerifyError: (class: CTest2, method: main signature: ([Ljava/lang/String; )V) 常量池中的非法类型
查看 CTest.class(Java 版本)的反汇编代码 和带有“javap -c -verbose”的 CTest2.class(Jasmin 版本) 它们似乎都以相同的方式设置常量池:
const #2 = class #16; // CTest
const #16 = Asciz CTest;
0: ldc_w #2; //class CTest
const #14 = Asciz CTest2;
const #17 = class #14; // CTest2
0: ldc_w #17; //class CTest2
我已经修复了 Jasmin 中的两个错误,但我看不出它是什么 将类放入“ldc_w”常量池时出错 它将类放入常量池中以供其他指令使用,例如 正确地“new”和“anewarray”。
我尝试使用 ASM 中的 TraceClassVisitor 查看 .class 文件, 但它不会转储常量池。
我下一步可以尝试什么想法?
I'm writing a compiler that generates Jasmin code and
want to invoke a method that takes a Class as a parameter.
public class CTest
{
public static void main(String[] args)
throws Exception
{
java.lang.reflect.Array.newInstance(CTest.class, 0);
}
}
So in Jasmin, I think that should be:
.class public CTest2
.super java/lang/Object
.method public static main([Ljava/lang/String;)V
.limit stack 2
.limit locals 1
ldc_w CTest2
iconst_0
invokestatic java/lang/reflect/Array/newInstance(Ljava/lang/Class;I)Ljava/lang/Object;
pop
return
.end method
When I assemble it and run it I get:
Exception in thread "main" java.lang.VerifyError: (class: CTest2, method: main signature: ([Ljava/lang/String;)V) Illegal type in constant pool
Looking at the disassembled code for both CTest.class (the Java version)
and CTest2.class (the Jasmin version) with "javap -c -verbose"
they both appear to set up the constant pool the same way:
const #2 = class #16; // CTest
const #16 = Asciz CTest;
0: ldc_w #2; //class CTest
const #14 = Asciz CTest2;
const #17 = class #14; // CTest2
0: ldc_w #17; //class CTest2
I've fixed two bugs in Jasmin already, but I can't see what it's
doing wrong when putting the class in the constant pool for "ldc_w"
it puts classes in the constant pool for other instructions, like
"new" and "anewarray" correctly.
I've tried looking at the .class files with TraceClassVisitor in ASM,
but it doesn't dump the constant pool.
Any ideas what I can try next?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须确保该类的版本号至少为 49(请参阅此 ASM Javadoc 页面)。
You have to ensure that the version number of the class is at least 49 (see the visitLdcInsn on this ASM Javadoc page).