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)

インターセプトされたオブジェクトを作成するヘルパー メソッド。生成されたインスタンスをより細かく制御するには、この静的メソッドの代わりに Enhancer の新しいインスタンスを使用します。

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

ドキュメントから、あなたの質問に対する答えはイエスのようです

于 2013-10-22T07:40:32.847 に答える