我收到了java.lang.IllegalArgumentException

时间:2017-12-10 09:46:04

标签: java reflection byte-buddy

我收到了以下错误:

  

java.lang.IllegalArgumentException:[static java.lang.String com.runtime.MyInterceptor.intercept()]都不允许来自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 个答案:

答案 0 :(得分:1)

使拦截器方法公开:

getSharedPreferences