org.hibernate.validator.parameternameprovider.ReflectionParameterNameProvider Maven / Gradle / Ivy
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or .
*/
package org.hibernate.validator.parameternameprovider;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.List;
import javax.validation.ParameterNameProvider;
import org.hibernate.validator.internal.util.IgnoreJava6Requirement;
import static org.hibernate.validator.internal.util.CollectionHelper.newArrayList;
/**
* Uses Java 8 reflection to get the parameter names.
*
* For this provider to return the actual parameter names, classes must be compiled with the '-parameters' compiler
* argument. Otherwise, the JDK will return synthetic names in the form {@code arg0}, {@code arg1}, etc.
*
* See also JEP 118
* @author Khalid Alqinyah
*
* @since 5.2
*/
@IgnoreJava6Requirement
public class ReflectionParameterNameProvider implements ParameterNameProvider {
@Override
public List getParameterNames(Constructor> constructor) {
return getParameterNames( constructor.getParameters() );
}
@Override
public List getParameterNames(Method method) {
return getParameterNames( method.getParameters() );
}
private List getParameterNames(Parameter[] parameters) {
List parameterNames = newArrayList();
for ( Parameter parameter : parameters ) {
// If '-parameters' is used at compile time, actual names will be returned. Otherwise, it will be arg0, arg1...
parameterNames.add( parameter.getName() );
}
return parameterNames;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy