2017-03-02 6 views
2

ある注釈で注釈を付けられたすべてのメソッドが特定のクラスに委任されるpremain()があります。一般的に、私は次のようになります。デバッガを使用してByteBuddy MethodDelegationがJavaエージェントで動作しない

public static void premain(final String agentArguments, final Instrumentation instrumentation) { 

    CountingInterception ci = new CountingInterception(); 

    new AgentBuilder.Default() 
    .type(ElementMatchers.isAnnotatedWith(com.codahale.metrics.annotation.Counted.class)) 
     .transform((builder, type, classLoader, module) -> 
     builder.method(ElementMatchers.any()) 
       .intercept(MethodDelegation.to(ci)) 
    ).installOn(instrumentation); 
} 

は、この部分が処理されていることを示しているが、注釈付きメソッドが呼び出された場合、何も起こりません。

CountingInterceptionは、任意のヒントについては、この

public class CountingInterception { 

    @RuntimeType 
    public Object intercept(@DefaultCall final Callable<?> zuper, @Origin final Method method, @AllArguments final Object... args) throws Exception { 

    String name = method.getAnnotation(Counted.class).name(); 
    if (name != null) { 
     // do something 
    } 

    return zuper.call(); 
    } 
} 

おかげのように見えます! ByteBuddy 1.6.9

答えて

1

私はあなたは、Java 8のデフォルトのメソッド呼び出しとは異なる何かをしようとしていることを前提とを使用して

。スーパーメソッドを呼び出す@SuperCallを使用しましたか?

私はあなたにお勧めします: 1.何もしないようにインターセプターを減らしてください。 MethodDelegationSuperMethodCallで連結するインターセプタを作成します。 2.コンソールにエラーを書き込むためにAgentBuilder.Listenerを登録します。

デフォルトのメソッド実装を提供するクラスにのみインターセプタを適用できるため、Byte Buddyはメソッドをバインドできません。私がやりたいことを達成するために

+0

感謝を。 – micfra

1

は、以下の変更が行われました。

premainで:

CountingInterception ci = new CountingInterception(); 

new AgentBuilder.Default() 
    .type(declaresMethod(isAnnotatedWith(Counted.class))) 
     .transform((builder, type, classLoader, module) -> builder 
     .method(isAnnotatedWith(Counted.class)) 
       .intercept(MethodDelegation.to(ci).andThen(SuperMethodCall.INSTANCE)) 
    ).installOn(instrumentation); 

とCountingInterceptionで

:あなたのヒントのための

public void interceptor(@Origin final Method method) throws Exception { 

    String name = method.getAnnotation(Counted.class).name(); 
    if (name != null) { 
     // do something 
    } 

} 
関連する問題