Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 超级类不可分配给asm在运行时实现的类_Java_Runtime_Java Bytecode Asm_Dynamic Class Creation - Fatal编程技术网

Java 超级类不可分配给asm在运行时实现的类

Java 超级类不可分配给asm在运行时实现的类,java,runtime,java-bytecode-asm,dynamic-class-creation,Java,Runtime,Java Bytecode Asm,Dynamic Class Creation,我有一个包含内部接口的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

我有一个包含内部接口的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'

之所以我确信我的字节码和类加载方法是正确的,是因为如果我在不扩展任何其他类的情况下定义了实现类,它将很好地工作

似乎ASM类加载器不是
X
类加载器的子类,因此有两个不同的类(即使它们是相同的),这是不能相互铸造的

尝试使用:

Method m = ClassLoader.getDeclaredMethods("defineClass", String.class, byte[].class, int.class, int.class);
m.setAccessible(true);
m.invoke(X.class.getClassLoader(), ...);

您能否发布如何定义和实例化XImpl实例?看起来像是典型的类加载issue@qwwdfsad嗨,我已经编辑了我的问题。谢谢你的回复!显然我有一些内部问题,你的回答给了我一个很好的方向。你能告诉我这个问题吗?我很感兴趣,还有什么会导致这个错误呢?我有自己的类加载器实现,它覆盖/扩展了一些功能。当您处理Java类路径和进行动态绑定时,事情开始变得非常有趣。。
Method m = ClassLoader.getDeclaredMethods("defineClass", String.class, byte[].class, int.class, int.class);
m.setAccessible(true);
m.invoke(X.class.getClassLoader(), ...);