2017-12-10 9 views
-1

を取得しています:私は私がやっているのミスを知らない私は、エラー、次の取得していますjava.lang.IllegalArgumentExceptionが

java.lang.IllegalArgumentException: None of [static java.lang.String com.runtime.MyInterceptor.intercept()] allows for delegation from public java.lang.String java.lang.Object.toString()

public void interceptMethod() throws InstantiationException, IllegalAccessException { 
    Class<?> dynamicType = new ByteBuddy().subclass(Object.class) 
      .method(ElementMatchers.named("toString")) 
      .intercept(MethodDelegation 
      .to(MyInterceptor.class)) 
      .make() 
      .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) 
      .getLoaded(); 

    if (dynamicType.newInstance().toString().equals("intercept")) { 
     System.out.println("method intercept() is intercepted by byteBuddy"); 
    } else { 
     System.out.println("Failed to intercept the method toString()"); 
    } 

} 

class MyInterceptor { 
    static String intercept() { 
     return "intercept"; 
    } 
} 

答えて

1

インターセプタメソッドを公開してください:

public class MyInterceptor { 
    public static String intercept() { 
     return "intercept"; 
    } 
} 
+0

まだ私は奇妙だ、同じ問題に直面して – Tirumalesh

+0

よ私はその変更でそれを実行すると、あなたのコードは動作しています。 MyInterceptorはネストされたクラスですか?たぶんそれはトップレベルのクラスの場合は、パブリック静的またはパブリックにしようとします。 – kaos

+0

こんにちはKaos、私はMyInterceptorをpublicとして作っています。あなたのために多くのおかげで:) – Tirumalesh

関連する問題