private static <A extends Annotation> Class<? extends ConstraintValidator<A, ?>> //
      getValidatorForType(final ConstraintDescriptor<A> constraint, final Class<?> targetType)
          throws UnexpectedTypeException {
    final List<Class<? extends ConstraintValidator<A, ?>>> constraintValidatorClasses =
        constraint.getConstraintValidatorClasses();
    if (constraintValidatorClasses.isEmpty()) {
      throw new UnexpectedTypeException(
          "No ConstraintValidator found for  " + constraint.getAnnotation());
    }
    final ImmutableSet<Class<? extends ConstraintValidator<A, ?>>> best =
        getValidatorForType(targetType, constraintValidatorClasses);
    if (best.isEmpty()) {
      throw new UnexpectedTypeException(
          "No " + constraint.getAnnotation() + " ConstraintValidator for type " + targetType);
    }
    if (best.size() > 1) {
      throw new UnexpectedTypeException("More than one maximally specific "
          + constraint.getAnnotation() + " ConstraintValidator for type " + targetType + ", found "
          + Ordering.usingToString().sortedCopy(best));
    }
    return Iterables.get(best, 0);
  }
private static < A extends Annotation > Class < ? extends ConstraintValidator < A , ? > > getValidatorForType ( final ConstraintDescriptor < A > constraint , final Class < ? > targetType ) throws UnexpectedTypeException { final List < Class < ? extends ConstraintValidator < A , ? > > > constraintValidatorClasses = constraint . getConstraintValidatorClasses ( ) ; if ( constraintValidatorClasses . isEmpty ( ) ) { throw new UnexpectedTypeException ( "No ConstraintValidator found for  " + constraint . getAnnotation ( ) ) ; } final ImmutableSet < Class < ? extends ConstraintValidator < A , ? > > > best = getValidatorForType ( targetType , constraintValidatorClasses ) ; if ( best . isEmpty ( ) ) { throw new UnexpectedTypeException ( "No " + constraint . getAnnotation ( ) + " ConstraintValidator for type " + targetType ) ; } if ( best . size ( ) > 1 ) { throw new UnexpectedTypeException ( "More than one maximally specific " + constraint . getAnnotation ( ) + " ConstraintValidator for type " + targetType + ", found " + Ordering . usingToString ( ) . sortedCopy ( best ) ) ; } return Iterables . get ( best , 0 ) ; }
Gets the best {@link ConstraintValidator}.

<p>
The ConstraintValidator chosen to validate a declared type {@code targetType} is the one where
the type supported by the ConstraintValidator is a supertype of {@code targetType} and where
there is no other ConstraintValidator whose supported type is a supertype of {@code type} and
not a supertype of the chosen ConstraintValidator supported type.
</p>

@param constraint the constraint to find ConstraintValidators for.
@param targetType The type to find a ConstraintValidator for.
@return ConstraintValidator

@throws UnexpectedTypeException if there is not exactly one maximally specific constraint
validator for targetType.