2017-02-08 8 views
2

私はメソッドの委譲を実装しようとしているし、すべてのセッターを委任することによってクラスのダーティフィールドを追跡するフィールドを追加しようとしています。私のクラスのインスタンスは、次のようになります。メソッドの委任とフィールドの追加

 T t = new ByteBuddy() 
      .subclass (typeClass) 
      .defineField ("dirtyFields", List.class, Visibility.PUBLIC) 
    .method(ElementMatchers.isSetter()) 
     .intercept(MethodDelegation.to(SetterInterceptor.class)) 
      .make() 
      .load (SetterInterceptor.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) 
      .getLoaded() 
      .newInstance(); 

    List<String> l = new ArrayList<String>(); 
    t.getClass().getField("dirtyFields").set(t, l); 
    l.add("foobar"); 

    return t; 

と私のインターセプタは、次のようになります。

@RuntimeType 
    public static Object intercept(@SuperCall Callable<?> superMethod, @Origin Method method, @Super(proxyType = TargetType.class) Object delegate) throws Exception { 
    System.out.println ("---- Intercept"); 
    markDirty (delegate, Character.toLowerCase (method.getName().charAt(3)) + method.getName().substring(4)); 
    return superMethod.call(); 
    } 

しかし、私はセッターがコールされていることを「オブジェクト」を取得することができないんだ、とデリゲートそのオブジェクトを参照していないようです。この機能を提供するために、私が何かする必要があるか、私が注入できるものがあるかどうか疑問に思っています。

ありがとうございます!

+0

注意を。むしろ、すべてのインスタンスが実装し、このインタフェース上でプロキシをベースにする 'CanBeDirty'インターフェースを持っています。 –

答えて

1

私が迎撃に@Thisを注入することによって、自分の質問に答えた:あなたはかなり高価であるターゲットクラスの `Super` @プロキシを使用しないでください

public static Object intercept(@This Object thiz, @SuperCall Callable<?> superMethod, @Origin Method method, @Super(proxyType = TargetType.class) Object delegate) throws Exception {