-1
public void parametersForValidation(String name,String address){
validateParameters(name,address);
}

private void validateParameters(Object... values){
for(Object value:values){
Objects.requirNonNull(value,"Value is mandatory");
}
}

I want instead of "value is mandatory", parameter name like "Name is namdatory" and "address is mandatory" should come.

4
  • Pass the name of the parameter as well, e.g. in an object that contains name and value. Then concatenate the name to the message, e.g. something like this Objects.requirNonNull(param.getValue(),param.getName() + " is mandatory");. Commented Sep 26, 2016 at 15:35
  • If you're after the method parameters' names, it depends on your environment, e.g. whether your compiled code contains the names or not (AFAIK only available for Java 8+) - something like here: stackoverflow.com/questions/21455403/… . There are libraries that provide parameter names for lower Java versions by hooking into the build process if you can't use Java 8. But in both cases you'd have to pass the names to validateParameters() since that method doesn't know what is being passed. Commented Sep 26, 2016 at 15:38
  • Thanks,but i don't want to make like key value pair. If I am passing address as a parameter then i want that parameter name in string format. Commented Sep 26, 2016 at 15:39
  • You don't pass parameters, you pass values. This is not possible. Commented Sep 26, 2016 at 15:42

2 Answers 2

0

There is no such magic for the invoked method. Since you are passing values to the method, these arguments do not bear any information about their parameter roles. In fact, there is no guaranty that the values passed to validateParameters are parameters in the callers context. You can, e.g. invoke it as validateParameters(Math.sin(2), null) or validateParameters(bufferedReader.readLine()) and it’s not clear, what name the method should assume for its arguments, when it encounters null. The simplest solution is to provide the parameter names explicitly:

private static String[] parameterNames={ "name", "address" };
public void parametersForValidation(String name,String address){
    validateParameters(parameterNames, name, address);
}
private void validateParameters(String[] name, Object... values){
    for(int ix=0; ix<name.length; ix++) {
        if(values[ix]==null) {
            throw new NullPointerException(name[ix]+" is mandatory");
        }
    }
}

For comparison, if you are using Java 8 and have compiled your code using the -parameters option, the parameter names are available at runtime via Reflection, i.e. you can use

public void parametersForValidation(String name, String address) {
    String[] names;
    try {
        names = Arrays.stream(getClass()
            .getMethod("parametersForValidation", String.class, String.class)
            .getParameters()).map(Parameter::getName).toArray(String[]::new);
    }
    catch(ReflectiveOperationException ex) { throw new AssertionError(ex); }
    validateParameters(names, name, address);
}
private void validateParameters(String[] name, Object... values) {
    for(int ix=0; ix<name.length; ix++) {
        if(values[ix]==null) {
            throw new NullPointerException(name[ix]+ " is mandatory");
        }
    }
}

which is obviously not simpler than just providing the names and exchanges the explicit list of names with an explicit list of the name and parameter types of the method to look up reflectively.

Sign up to request clarification or add additional context in comments.

Comments

0

Not feasible.

public void parametersForValidation(String name, String address) {
    Objects.requirNonNull(name, "name is mandatory");
    Objects.requirNonNull(address, "address is mandatory");
    ...
}

You can use

public void parametersForValidation(@NonNull String name, @NonNull String address) {
    ...
}

and annotation processing. However since Optional<?> I would simple make it a hard convention. And use code style checkers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.