I use ByteBuddy and I have this code:
public class A extends B {
public A(String a) {
super(a);
}
public String getValue() {
return "HARDCODED VALUE";
}
}
public abstract class B {
private final String message;
protected B(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
My current generation code is:
Constructor<T> declaredConstructor;
try {
declaredConstructor = A.class.getDeclaredConstructor(String.class);
} catch (NoSuchMethodException e) {
//fail with exception..
}
new ByteBuddy()
.subclass(A.class, Default.IMITATE_SUPER_CLASS)
.name(A.class.getCanonicalName() + "$Generated")
.defineConstructor(Visibility.PUBLIC)
.intercept(MethodCall.invoke(declaredConstructor).with("message"))
.make()
.load(tClass.getClassLoader(),ClassLoadingStrategy.Default.WRAPPER)
.getLoaded()
.newInstance();
I want to get instance of class A, and also I want to make some actions in constructor after invoke super(), like this:
public A(){
super("message");
// do something special..
}
I tried implement with MethodDelegation.to(DefaultConstructorInterceptor.class), but I didn't succeed.