我正在使用CGLib来增强A类。
public class A {
public void printName(){
System.out.println("guy");
}
}我有B类,它扩展了A类。
public class B extends A{
public void printName(){
System.out.println("someone else!");
}
}当我增强它时,我如何告诉CGLib实例化B而不是A?
public A getEnhancedClass( boolean trueIfIWantBInsteadOfA ){
e.setSuperclass( A.class ); // cannot change this
e.setCallback( createDummyInterceptor() );// an interceptor that just invokesSuper
/// ... missing code here
return (A) e.create()
}下面的代码应该会输出"someone print!“
getEnhancedClass( true ).printName();发布于 2013-12-01 20:30:45
Cglib创建Enhancer#setSuperclass中给定的arument的子类。如果你子类化A,代理甚至不会知道B的存在。也许你想创建一个懒惰的代理?然后,您可能希望查看LazyLoader或Dispatcher回调。
发布于 2013-10-22 15:20:56
也许你需要截取构造函数并返回一个B的实例,而不是调用A构造函数。
https://stackoverflow.com/questions/15928921
复制相似问题