org.hibernate.validator.internal.util.privilegedactions.GetMethodFromGetterNameCandidates 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.internal.util.privilegedactions;
import java.lang.reflect.Method;
import java.security.PrivilegedAction;
import java.util.Set;
/**
* Returns the method with the specified property name or {@code null} if it does not exist. This action will
* iterate through getter name candidates and return the first found method.
*
* {@code GetMethodFromPropertyName#lookForMethodsInHierarchy} parameter controls if we need to check for methods on
* superclasses/implemented interfaces or not, if equals to {@code false} it will use
* {@link Class#getDeclaredMethod(String, Class[])}, and {@link Class#getMethod(String, Class[])} otherwise.
*
* @author Marko Bekhta
*/
public final class GetMethodFromGetterNameCandidates implements PrivilegedAction {
private final Class clazz;
private final Set getterNameCandidates;
private final boolean lookForMethodsInHierarchy;
private GetMethodFromGetterNameCandidates(Class clazz, Set getterNameCandidates, boolean lookForMethodsInHierarchy) {
this.clazz = clazz;
this.getterNameCandidates = getterNameCandidates;
this.lookForMethodsInHierarchy = lookForMethodsInHierarchy;
}
public static GetMethodFromGetterNameCandidates action(Class clazz, Set getterNameCandidates) {
return new GetMethodFromGetterNameCandidates( clazz, getterNameCandidates, false );
}
public static GetMethodFromGetterNameCandidates action(Class clazz, Set possibleMethodNames, boolean lookForMethodsInHierarchy) {
return new GetMethodFromGetterNameCandidates( clazz, possibleMethodNames, lookForMethodsInHierarchy );
}
@Override
public Method run() {
for ( String methodName : getterNameCandidates ) {
try {
if ( lookForMethodsInHierarchy ) {
return clazz.getMethod( methodName );
}
else {
return clazz.getDeclaredMethod( methodName );
}
}
catch (NoSuchMethodException e) {
// just ignore the exception
}
}
return null;
}
}