KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have the following BeanValidation code that works fine, and permits to validate that a bean annotated with: @EnumValue(enumClass = MyTestEnum.class) private String field; public enum MyTestEnum { VAL1, VAL2; } Will be validated only if the field value is "VAL1" or "VAL2". public class EnumNameValidator implements ConstraintValidator<EnumValue, String> { private Set<String> AVAILABLE_ENUM_NAMES; @Override public void initialize(EnumValue enumValue) { Class<? extends Enum<?>> enumSelected = enumValue.enumClass(); Set<? extends Enum<?>> enumInstances = Sets.newHashSet(enumSelected.getEnumConstants()); AVAILABLE_ENUM_NAMES = FluentIterable .from(enumInstances) .transform(PrimitiveGuavaFunctions.ENUM_TO_NAME) .toImmutableSet(); } @Override public boolean isValid(String value, ConstraintValidatorContext context) { if ( value == null ) { return true; } else { return AVAILABLE_ENUM_NAMES.contains(value); } } } What I don't understand is why my first attempt failed. Using instead of the enumSelected.getEnumConstants() above the following code: Set<? extends Enum<?>> enumInstances = EnumSet.allOf(enumSelected); Intellij 12 doesn't highlight any error, but the compiler says: java: method allOf in class java.util.EnumSet<E> cannot be applied to given types; required: java.lang.Class<E> found: java.lang.Class<capture#1 of ? extends java.lang.Enum<?>> reason: inferred type does not conform to declared bound(s) inferred: capture#1 of ? extends java.lang.Enum<?> bound(s): java.lang.Enum<capture#1 of ? extends java.lang.Enum<?>> I don't understand the problem, and I also have that code which works fine: p
Tags (comma-separated)
Save Edits
Cancel