AspectJ JoinPoint question

前端 未结 2 1048
梦如初夏
梦如初夏 2021-01-02 08:04

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\'

相关标签:
2条回答
  • 2021-01-02 08:34

    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.

    0 讨论(0)
  • 2021-01-02 08:34

    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);
    }
    
    0 讨论(0)
提交回复
热议问题