AspectJ JoinPoint question

此生再无相见时 提交于 2019-12-30 06:09:12

问题


I am currently using JoinPoint to capture the parameters passed to service methods at runtime. Though JoinPoint helps me retrieve the parameter values, I see that it doesn't provide any good API to retrieve the names of the parameters, parameter types, individual parameter values when the parameter passed is an array etc.

Here's an example:

public void doIt(String user, Attribute[] attr, Integer[] i, boolean bool, List<Attribute> list){.....}

For the above method, when I use JoinPoint.getArgs(), i see a garbage value for the parameter which is an array or a collection. If the parameter is an array or a collection, how can I verify if they are one of those and how can I traverse them to retrieve individual values?

Any suggestions ? Thanks


回答1:


This should work:

MethodSignature signature = (MethodSignature)joinPoint.getSignature();
String[] parameterNames = signature.getParameterNames();
Object[] parameterValues = joinPoint.getArgs();

The parameterNames should match what you have passed in.

Update 1: You are probably compiling with debugging symbols turned off -(explicitly passing in javac -g:none, or through flags in maven/ant). With debugging symbols off, names will not be available and will replaced with args1 etc by the compiler. Try with a compilation with debug symbols not explicitly turned off.




回答2:


AspectJ pointcut

public pointcut pointcutName():
        execution(* ClassName.method(..));

Getting Parameter names of the method

before():pointcutName(){
String[] paramNames = ((CodeSignature) thisJoinPointStaticPart
                .getSignature()).getParameterNames();
 for(String paramName:paramNames){
            System.out.println(paramName);
        }
}

Getting Parameter Values:

before():pointcutName(){
Object[] paramValues = thisJoinPoint.getArgs();

for (Object object:paramValues){
        System.out.println(object.toString);
        }
}

Getting Method Return Value:

after() returning(Object objectReturn) :pointcutName(){
    System.out.println(objectReturn);
}


来源:https://stackoverflow.com/questions/5720494/aspectj-joinpoint-question

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!