编写Java程序 堆栈的接口_java - 错误:调用实现接口的通用方法时,java.lang.AbstractMethodError - 堆栈内存溢出...
我正在嘗試使用javassist以編程方式創(chuàng)建和編譯實(shí)現(xiàn)接口的類(lèi)(在運(yùn)行時(shí))。
每當(dāng)我調(diào)用該動(dòng)態(tài)類(lèi)的實(shí)例時(shí),都會(huì)收到以下錯(cuò)誤消息:
java.lang.AbstractMethodError: FooImpl.test()Ljava/lang/Object;
這是我的界面
public class FooBarInterface {
public T getEntity();
}
這是一個(gè)示例實(shí)體
public class FooEntity {
@Override
public String toString() {
return "Hello, Foo!";
}
}
這是我以編程方式實(shí)現(xiàn)接口的方式
public void test() {
ClassPool classPool = ClassPool.getDefault();
CtClass testInterface = classPool.get(FooBarInterface.class.getName());
CtClass fooImpl = classPool.makeClass("FooImpl");
fooImpl.addInterface(testInterface);
CtMethod testMethod = CtNewMethod.make(
"public com.test.FooEntity getEntity(){" +
"return new com.test.FooEntity();" +
"}",
canImpl
);
fooImpl.addMethod(testMethod);
fooImpl.writeFile();
TestInterface test =
(TestInterface) fooImpl.toClass().newInstance();
System.out.println(test.getEntity());
}
如果我將實(shí)現(xiàn)的方法的返回類(lèi)型更改為Object,則不會(huì)收到錯(cuò)誤,如下所示:
CtMethod testMethod = CtNewMethod.make(
"public Object getEntity(){" +
"return new com.test.FooEntity();" +
"}",
canImpl
);
然后我成功地打了hello, Foo! 。 我可以將返回類(lèi)型更改為Object,但是我想進(jìn)一步了解為什么使用Foo類(lèi)型返回會(huì)產(chǎn)生AbstractMethodError 。
總結(jié)
以上是生活随笔為你收集整理的编写Java程序 堆栈的接口_java - 错误:调用实现接口的通用方法时,java.lang.AbstractMethodError - 堆栈内存溢出...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql保存时乱码了_MySQL保存中
- 下一篇: c java开发对比_编程语言:Java