Description:
|
there is a difference in the processing of raw type and <?> type. here is my use case :
{code} @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 {}; } {code}
{code} 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; }
} {code}
{code} @CheckSettingValueSettingType public abstract class SettingValue<T> extends BaseEntity { ... } {code}
{code} public class IntegerSettingValue extends SettingValue<Integer> { ... } {code}
With:
{code} public class SettingValueSettingTypeValidator implements ConstraintValidator<CheckSettingValueSettingType, SettingValue> {code} everything works fine, though when
,
:
{code} public class SettingValueSettingTypeValidator implements ConstraintValidator<CheckSettingValueSettingType, SettingValue<?>> {code}
I get the following exception : {noformat} javax.validation.UnexpectedTypeException: No validator could be found for type: com.XXX.settings.data.SettingValue {noformat}
workaround :
-
*
do not use @Inherited annotation
-
*
use @SuppressWarnings("rawtypes") instead of inferring <?>
|