there is a difference in the processing of raw type and <?> type.
here is my use case :
@Target( { ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = SettingValueSettingTypeValidator.class)
@Documented
@Inherited
public @interface CheckSettingValueSettingType
{
String message() default "{com.XXX.settings.settingvaluesettingtype}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
public class SettingValueSettingTypeValidator implements ConstraintValidator<CheckSettingValueSettingType, SettingValue>
{
@Override
public void initialize(CheckSettingValueSettingType constraint)
{
// nothing to initialize
}
@Override
public boolean isValid(SettingValue settingValue, ConstraintValidatorContext context)
{
if (settingValue instanceof IntegerSettingValue)
{
return SettingTypes.isIntegerSettingType(settingValue.getSettingKey().getSettingTypeId());
}
if (settingValue instanceof StringSettingValue)
{
return SettingTypes.isStringSettingType(settingValue.getSettingKey().getSettingTypeId());
}
if (settingValue instanceof DateSettingValue)
{
return SettingTypes.isDateSettingType(settingValue.getSettingKey().getSettingTypeId());
}
return false;
}
}
@CheckSettingValueSettingType
public abstract class SettingValue<T> extends BaseEntity
{
...
}
public class IntegerSettingValue extends SettingValue<Integer>{
...
}
}
public class SettingValueSettingTypeValidator implements ConstraintValidator<CheckSettingValueSettingType, SettingValue>
everything works fine, though when,
public class SettingValueSettingTypeValidator implements ConstraintValidator<CheckSettingValueSettingType, SettingValue<?>>
I get the following exception :
javax.validation.UnexpectedTypeException: No validator could be found for type: com.XXX.settings.data.SettingValue
workaround :
- do not use @Inherited annotation
- use @SuppressWarnings("rawtypes") instead of inferring <?>
|