Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java CGLib是否修改类的所有实例_Java_Cglib - Fatal编程技术网

Java CGLib是否修改类的所有实例

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

如果我实现了一些MethodInterceptor,如下所示:

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