开发者

Catch any call made with a type as a parameter with AspectJ

I'm trying to normalize URIs across an application using AspectJ. I would like to catch every call that is made to a method passing in a java.net.URI parameter.

A simple poi开发者_开发百科ntcut for this would be something like: public pointcut uriMethod(URI uri) : call(* *(..)) && args(uri);

However, this seems to only match method signatures with only one argument (the URI). I can change the args portion to "args(uri, ..)", but this only applies to methods with multiple arguments (losing the single-argument signatures) and only if the first argument is the URI.

Is is possible to construct a point cut to cover all cases: public void exampleMethod1(URI uri); public void exampleMethod2(URI uri, String s); public void exampleMethod3(int i, URI uri); public void exampleMethod4(float f, URI uri, short s);

Bonus points if it's possible to capture multiple URIs with the same pointcut: public void exampleMethod5(URI uri1, URI uri2);

I'm open to alternative routes, too. Perhaps I need something based on the URI class itself, not the classes it gets passed into?


This pointcut expression will satisfy your first condition:

execution(* *(..,java.net.URI,..))

then in the advise, use joinpoint to navigate through the args, get the arg with URI as the instance - this sort of satisfies your second condition:

    @Around("methodsWithURI()")
public void aroundMethodsWithURI(ProceedingJoinPoint joinpoint){
    for(Object objArg: joinpoint.getArgs()){
        if (objArg instanceof URI){
            System.out.println(objArg);
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜