Here I required to create a instance of BImpl
but BImpl
requires to access functionality by an interface A
. For this purpose, the class implements this interface A
. How Can I wire a delegation of these interface methods of BImpl
at runtime ? The idea is that BImpl
can use A
's methods.
In my case A
is known and the AImpl
instance is created at run-time.
public static void main(String[] args) {
B b = (B) Enhancer.create(BImpl.class, new MyInterceptor());
System.out.println(b.cellValue());
}
interface A {
String value();
}
class AImpl implements A {
@Override
public String value() {
return "MyA";
}
}
interface B {
String cellValue();
}
abstract class BImpl implements B, A {
@Override
public String cellValue() {
return value() + "MyBCell";
}
}
class MyInterceptor implements MethodInterceptor {
@Override
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy proxy) throws Throwable {
System.out.println(method.getName());
if ("value".equals(method.getName()))
return method.invoke(obj, args);
else
return proxy.invokeSuper(obj, args);
}
}