我收到了java.lang.IllegalArgumentException

时间:2022-12-15 20:51:36

I'm getting following error:

我收到以下错误:

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()

java.lang.IllegalArgumentException:[static java.lang.String com.runtime.MyInterceptor.intercept()]都不允许来自public java.lang.String java.lang.Object.toString()的委托

I don't know what mistake I'm doing.

我不知道我在做什么错。

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 个解决方案

#1


1  

Make the interceptor method public:

使拦截器方法公开:

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

#1


1  

Make the interceptor method public:

使拦截器方法公开:

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