2

如果我实现了一些 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());

现在是否增强了 Object 的每个实例?IE:如果我做类似的事情:

class foo { foo(){} }
foo myfoo = new foo();
int hash = myfoo.hashCode();
System.io.println(hash); //Prints 0

它真的会打印0吗?

4

1 回答 1

0

来自 API 文档

创造

public static Factory create(java.lang.Class type,
                             Callback callback)

Helper 方法来创建一个被拦截的对象。为了更好地控制生成的实例,请使用 Enhancer 的新实例而不是此静态方法。

Parameters:
    type - class to extend or interface to implement
    callback - the callback to use for all methods

从文档看来,您的问题的答案是肯定的

于 2013-10-22T07:40:32.847 回答