Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.IllegalArgumentException: None of [public static java.lang.Object org.example.AopInterceptor.intercept(java.util.concurrent.Callable) throws java.lang.Exception] allows for delegation from public java.lang.String org.example.User.getName() #1765

Closed
yax1234567 opened this issue Feb 6, 2025 · 2 comments
Assignees
Labels

Comments

@yax1234567
Copy link

I want to intercept the getName method in the User class,
But after running it, I get the following error:
java.lang.IllegalArgumentException: None of [public static java.lang.Object org.example.AopInterceptor.intercept(java.util.concurrent.Callable) throws java.lang.Exception] allows for delegation from public java.lang.String org.example.User.getName()
at net.bytebuddy.implementation.bind.MethodDelegationBinder$Processor.bind(MethodDelegationBinder.java:1099)
at net.bytebuddy.implementation.MethodDelegation$Appender.apply(MethodDelegation.java:1349)
at net.bytebuddy.dynamic.scaffold.TypeWriter$MethodPool$Record$ForDefinedMethod$WithBody.applyCode(TypeWriter.java:730)
at net.bytebuddy.dynamic.scaffold.TypeWriter$MethodPool$Record$ForDefinedMethod$WithBody.applyBody(TypeWriter.java:715)
at net.bytebuddy.dynamic.scaffold.TypeWriter$Default$ForInlining$WithFullProcessing$RedefinitionClassVisitor$CodePreservingMethodVisitor.visitCode(TypeWriter.java:5502)
at net.bytebuddy.jar.asm.ClassReader.readMethod(ClassReader.java:1514)
at net.bytebuddy.jar.asm.ClassReader.accept(ClassReader.java:745)
at net.bytebuddy.jar.asm.ClassReader.accept(ClassReader.java:425)
Hope to get your help

public static void main(String[] args) throws Exception {
        User user = new User("TEST");
        Instrumentation inst = ByteBuddyAgent.install();
        AgentBuilder agentBuilder = new AgentBuilder.Default()
                .with(AgentBuilder.Listener.StreamWriting.toSystemOut())
                .with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
                .ignore(ElementMatchers.none())
                .disableClassFormatChanges().type(ElementMatchers.named(User.class.getName()))
                .transform((builder, typeDescription, classLoader, javaModule, protectionDomain)
                        -> builder.method(ElementMatchers.named("getName"))
                        .intercept(MethodDelegation.to(AopInterceptor.class)));
        ClassFileTransformer classFileTransformer = agentBuilder.makeRaw();
        inst.addTransformer(classFileTransformer, true);
        inst.retransformClasses(User.class);
        System.out.println(user.getName());
    }


public class AopInterceptor {
    @RuntimeType
    public static Object intercept(@SuperCall Callable<?> zuper)
            throws Exception {
        long before = System.currentTimeMillis();
        try {
            return zuper.call();
        } finally {
            System.out.println("Took: " + (System.currentTimeMillis() - before));
        }
    }
}
@raphw
Copy link
Owner

raphw commented Feb 8, 2025

disableClassFormatChanges will implicitly change the instrumentation type to replace existing code, as the JVM does not allow for adding new methods, what is required to place the original code.

Use Advice instead

@raphw raphw self-assigned this Feb 8, 2025
@raphw raphw added the question label Feb 8, 2025
@yax1234567
Copy link
Author

OK, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants