1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| @NotNull @Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = EnumValue.Validator.class) public @interface EnumValue {
String message() default "无效的值";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
Class<? extends Enum<?>> enumClass();
String enumMethod() default "isValid";
class Validator implements ConstraintValidator<EnumValue, Object> {
private Class<? extends Enum<?>> enumClass; private String enumMethod;
@Override public void initialize(EnumValue enumValue) { enumMethod = enumValue.enumMethod(); enumClass = enumValue.enumClass(); }
@Override public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) { if (value == null) { return Boolean.TRUE; } if (enumClass == null || enumMethod == null) { return Boolean.TRUE; }
Class<?> valueClass = value.getClass(); Method method; try { method = enumClass.getDeclaredMethod(enumMethod, valueClass); } catch (NoSuchMethodException e) { throw new RuntimeException(String.format("%s.%s 不存在", enumClass.getName(), enumMethod)); }
if (!Boolean.TYPE.equals(method.getReturnType()) && !Boolean.class.equals(method.getReturnType())) { throw new RuntimeException(String.format("%s.%s 返回值不是布尔类型", enumClass.getName(), enumMethod)); } if (!Modifier.isStatic(method.getModifiers())) { throw new RuntimeException(String.format("%s.%s 不是静态方法", enumClass.getName(), enumMethod)); }
Boolean result; try { method.setAccessible(true); result = (Boolean) method.invoke(null, value); } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(String.format("%s.%s 调用失败:%s", enumClass.getName(), enumMethod, e.getMessage())); } return result != null && result; } } }
|