Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
194 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...