static < T extends Annotation > Class < ? > getTypeOfConstraintValidator ( final Class < ? extends ConstraintValidator < T , ? > > constraintClass ) { int candidateCount = 0 ; Class < ? > result = null ; for ( final Method method : constraintClass . getMethods ( ) ) { if ( method . getName ( ) . equals ( "isValid" ) && method . getParameterTypes ( ) . length == 2 && method . getReturnType ( ) . isAssignableFrom ( Boolean . TYPE ) ) { final Class < ? > firstArgType = method . getParameterTypes ( ) [ 0 ] ; if ( result == null || result . isAssignableFrom ( firstArgType ) ) { result = firstArgType ; } candidateCount ++ ; } } if ( candidateCount == 0 ) { throw new IllegalStateException ( "ConstraintValidators must have a isValid method" ) ; } else if ( candidateCount > 2 ) { throw new IllegalStateException ( "ConstraintValidators must have no more than two isValid methods" ) ; } return result ; }
Finds the type that a constraint validator will check .