【发布时间】:2015-05-11 17:59:18
【问题描述】:
我有一个包含内部接口的 java 类,并计划在运行时使用 asm 字节码注入来实现实现类。
package my.example;
public class X{
public static interface XInt{
public void getX();
}
//the following class that implement the interface will be defined during runtime.
//I put it here just to show how the implementation will look like
public static XImpl extend Y implements XInt{
public void getX(){//implementation code}
}
}
我确定我的 asm 代码是正确的,但是在定义类并调用 Class.forName("my.example.X$XImpl") 之后,我收到以下错误:
> Bad <init> method call
Exception Details:
Location:
my/example/X$XImpl.<init>()V: invokespecial
Reason:
Type 'my/other/package/Y' is not assignable to 'my/example/X$XImpl'
我的猜测是 Y 类在运行时还不可用?我不知道.. 任何帮助将不胜感激!
编辑: 我加载XImpl的方式很简单:
defineClass("my.example.X$XImpl", getXImplByte());
Class.forName("my.example.X$XImpl"); //fails here
我之所以确定我的字节码和类加载方法是正确的,因为如果我定义实现类而不扩展任何其他类,它会正常工作
【问题讨论】:
-
能否请您发布您如何定义和实例化 XImpl 实例?看起来像典型的类加载问题
-
@qwwdfsad 嗨,我已经编辑了我的问题。感谢您的回复
标签: java runtime java-bytecode-asm dynamic-class-creation