-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.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Shailesh Jain
  • 43
  • 1
  • 1
  • 7
  • 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");`. – Thomas Sep 26 '16 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: http://stackoverflow.com/questions/21455403/how-to-get-method-parameter-names-in-java-8-using-reflection . 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. – Thomas Sep 26 '16 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. – Shailesh Jain Sep 26 '16 at 15:39
  • You don't pass parameters, you pass values. This is not possible. – Sotirios Delimanolis Sep 26 '16 at 15:42

2 Answers2

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.

Holger
  • 285,553
  • 42
  • 434
  • 765
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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138