Java CGLib是否修改类的所有实例
如果我实现了一些MethodInterceptor,如下所示:Java CGLib是否修改类的所有实例,java,cglib,Java,Cglib,如果我实现了一些MethodInterceptor,如下所示: public class HashCodeAlwaysZeroMethodInterceptor implements MethodInterceptor { public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { if ("hashCode
public class HashCodeAlwaysZeroMethodInterceptor implements MethodInterceptor {
public Object intercept(Object object, Method method, Object[] args,
MethodProxy methodProxy) throws Throwable {
if ("hashCode".equals(method.getName())) {
return 0;
}
return methodProxy.invokeSuper(object, args);
}
}
Object proxy = Enhancer.create(
Object.class,
new HashCodeAlwaysZeroMethodInterceptor());
对象的每个实例现在都增强了吗?IE:如果我做了以下事情:
class foo { foo(){} }
foo myfoo = new foo();
int hash = myfoo.hashCode();
System.io.println(hash); //Prints 0
它真的会从API文档打印0吗
创造
public static Factory create(java.lang.Class type,
Callback callback)
用于创建截获对象的帮助器方法。为了更好地控制
对于生成的实例,请使用增强器的新实例而不是此实例
静态方法
从文件上看,您的问题的答案似乎是肯定的我仍然看不清楚。create()似乎返回一个工厂,可用于实例化增强对象。。。这意味着您必须自己显式地实例化它们
Parameters:
type - class to extend or interface to implement
callback - the callback to use for all methods