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

how to enhance method with @RequestMapping annotation declared in interface? #3847

Closed
1 task done
carrypann opened this issue Nov 13, 2019 · 7 comments
Closed
1 task done
Assignees
Labels
agent Language agent related. feature New feature plugin Plugin for agent or collector. Be used to extend the capabilities of default implementor.
Milestone

Comments

@carrypann
Copy link
Contributor

Please answer these questions before submitting your issue.

  • Why do you submit this issue?
  • Question

Question

  • What do you want to know?
    How to enhance method with @RequestMapping annotation declared in it's interface?

Here is the example code

interface RestApi {
    @RequestMapping("/test")
    String test();
}

@RestController
public class RestApiImpl implements RestApi {
    @Override
    public String test() {
        return "test";
    }
}

The problem is that skywalking can not enhance the RestApiImpl.test() method. I guess the reason is skywalking cannot find @RequestMapping annotation in overrided method.

And here is the related source code in sw's spring-plugins:

# RequestMappingMethodInterceptor.java
public class RequestMappingMethodInterceptor extends AbstractMethodInterceptor {
    @Override
    public String getRequestURL(Method method) {
        String requestURL = "";
        RequestMapping methodRequestMapping = method.getAnnotation(RequestMapping.class);
        if (methodRequestMapping.value().length > 0) {
            requestURL = methodRequestMapping.value()[0];
        } else if (methodRequestMapping.path().length > 0) {
            requestURL = methodRequestMapping.path()[0];
        }
        return requestURL;
    }
  // ...

But the method getAnnotation can not get annotation from interface, so how can i solve this problem?

@wu-sheng
Copy link
Member

If this is the case, we don't expect you to write in this way. You have to use more reflect API to get that.

@wu-sheng wu-sheng added agent Language agent related. feature New feature plugin Plugin for agent or collector. Be used to extend the capabilities of default implementor. labels Nov 13, 2019
@wu-sheng
Copy link
Member

This should be not hard, could you try to submit a pull request to fix this?

@liudantop
Copy link

I don't think the issue is the reason and test not pass.
found the matcher is the isAnnotatedWith, explain in bytebuddy. the match does not match inherited annotations :
isAnnotatedWith(named("org.springframework.web.bind.annotation.RequestMapping"));

  • Matches an {@link AnnotationSource} for declared annotations.
    • This matcher does not match inherited annotations which only exist for classes. Use
    • {@link net.bytebuddy.matcher.ElementMatchers#inheritsAnnotation(net.bytebuddy.matcher.ElementMatcher)}
    • for matching inherited annotations.

@wu-sheng
Copy link
Member

This issue is for fixing inheritance annotation meta inform. Yours may be different. You want to instrument the subclass without the annotation.

@liudantop
Copy link

liudantop commented Dec 20, 2019

case is similar.
If the annotation @RequestMapping("/test") is define interface method. Subclass implement the method, and enhance is inoperative.

thanks

@wu-sheng
Copy link
Member

I know it is similar, but at the codes level, it is different. You should be able to provide a pull request to fix this, as you have already been looking at the ByteBuddy APIs. There is only a chance to use suitable API to filter the classes.

@liudantop
Copy link

thanks, let me try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agent Language agent related. feature New feature plugin Plugin for agent or collector. Be used to extend the capabilities of default implementor.
Projects
None yet
Development

No branches or pull requests

4 participants