I would like to create an implementation at runtime for an abstract class using Byte Buddy and I'm facing the issue, that a java.lang.AbstractMethodError
is being thrown when invoking a method from a created instance. I have an existing abstract
class like this (which I actually cannot modify and which actually contains more logic):
public abstract class Algorithm {
abstract int execute();
}
Using the following minimal sample, I would like my Algorithm
instance to return a constant value:
Class<?> type = new ByteBuddy()
.subclass(Algorithm.class)
.method(ElementMatchers.named("execute"))
.intercept(FixedValue.value(42))
.make()
.load(classLoader, ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
Algorithm instance = (Algorithm) type.newInstance();
System.out.println(myInstance.execute());
This however leads to the following exception:
Exception in thread "main" java.lang.AbstractMethodError: package.Algorithm.execute()I
(when I experimentally change Algorithm
to an interface
, everything works fine, but this does not solve my specific issue).