I am trying to use javassist to programatically create and compile a class (at runtime) that implements an interface.
I get the following Error whenever I invoke an instance of that dynamic class:
java.lang.AbstractMethodError: FooImpl.test()Ljava/lang/Object;
Here's my interface
public class FooBarInterface<T> {
public T getEntity();
}
Here's a sample Entity
public class FooEntity {
@Override
public String toString() {
return "Hello, Foo!";
}
}
Here's how I programatically implement the interface
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<FooEntity> test =
(TestInterface<FooEntity>) fooImpl.toClass().newInstance();
System.out.println(test.getEntity());
}
If I changed the return type of the implemented method to Object, then I don't get the Error, like this:
CtMethod testMethod = CtNewMethod.make(
"public Object getEntity(){" +
"return new com.test.FooEntity();" +
"}",
canImpl
);
Then I successfully get the hello, Foo!
. I am OK with changing the return type to Object, but I'd like to understand more why returning with type Foo produces AbstractMethodError
.