1

I am having a requirement where I need to fetch a param the value and validate before a method is invoked. I use MethodInterceptor (org.aopalliance.intercept.MethodInvocation) for this.

My method has three String parameters. I need to validate only the second parameter or specifically the parameter with the name pname. Currently, I use the following logic to validate the value of this parameter. But I have to hard code the param name or hardcode the index.

    int i=0;
    for(Parameter param: invocation.getMethod().getParameters()) {
     
        if(param.getName().equalsIgnoreCase("pname")) {
            //do validation using invocation.getArguments()[i]
        }
     i++;
    }

Either approach will fail if somebody modifies the method. Do we have a better approach or in worst case warn the future developer while he modifies the method param.

kaya3
  • 47,440
  • 4
  • 68
  • 97
Renjith
  • 1,122
  • 1
  • 19
  • 45

0 Answers0