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.
question from:
https://stackoverflow.com/questions/65662078/warn-the-developer-while-changing-the-param-name 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…