添加注解校验逻辑

This commit is contained in:
zhaoyong 2021-08-05 19:37:41 +08:00
parent bb296e43d4
commit 502bcd12ef
7 changed files with 229 additions and 1 deletions

View File

@ -6,7 +6,7 @@
<groupId>cn.axzo.framework</groupId>
<artifactId>common-common</artifactId>
<version>1.0.6</version>
<version>1.0.7</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -0,0 +1,46 @@
package cn.azxo.framework.common.annotation;
import cn.azxo.framework.common.annotation.support.EnableEnumValidator;
import cn.azxo.framework.common.model.EnumBase;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
/**
* 枚举验证类
*
* @author zhaoyong
* @see EnableEnum
* @since 2021-06-22 14:53
*/
@Target({ FIELD})
@Retention(RUNTIME)
@Constraint(validatedBy = EnableEnumValidator.class)
@Documented
public @interface EnableEnum {
Class<? extends EnumBase> value();
String message() default "invalid value";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default {};
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
@interface List {
EnableEnum[] value();
}
}

View File

@ -0,0 +1,49 @@
package cn.azxo.framework.common.annotation;
import cn.azxo.framework.common.annotation.support.ValidNumberValueValidator;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
/**
* 有效的 int
*
* @author zhaoyong
* @see ValidNumberValue
* @since 2021-08-05 19:28
*/
@Target({ FIELD})
@Retention(RUNTIME)
@Constraint(validatedBy = ValidNumberValueValidator.class)
@Documented
public @interface ValidNumberValue {
/**
* 有效字符串集合类
* @return
*/
int[] value() default {};
String message() default "invalid value";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
@interface List {
ValidNumberValue[] value();
}
}

View File

@ -0,0 +1,49 @@
package cn.azxo.framework.common.annotation.support;
import cn.azxo.framework.common.annotation.EnableEnum;
import cn.azxo.framework.common.model.EnumBase;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.reflect.Modifier;
/**
* EnableEnum 注解实现类
*
* @author zhaoyong
* @see EnableEnumValidator
* @since 2021-06-22 14:59
*/
public class EnableEnumValidator implements ConstraintValidator<EnableEnum, Object> {
private Class<? extends EnumBase> enumBaseClazz;
@Override
public void initialize(EnableEnum constraintAnnotation) {
enumBaseClazz = constraintAnnotation.value();
}
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
if(enumBaseClazz == null) {
return false;
}
if(enumBaseClazz.isInterface() || Modifier.isAbstract(enumBaseClazz.getModifiers())){
return false;
}
if(!enumBaseClazz.isEnum()){
return false;
}
if(value == null){
return false;
}
EnumBase[] enumConstants = enumBaseClazz.getEnumConstants();
for (EnumBase enumConstant : enumConstants) {
if(enumConstant.getCode().equals(value)){
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,38 @@
package cn.azxo.framework.common.annotation.support;
import cn.azxo.framework.common.annotation.ValidNumberValue;
import cn.azxo.framework.common.utils.ArrayUtils;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Objects;
/**
* ValidValue 注解实现类
*
* @author zhaoyong_sh
* @see ValidNumberValueValidator
* @since 2020-10-10 13:57
*/
public class ValidNumberValueValidator implements ConstraintValidator<ValidNumberValue, Integer> {
private int[] validValues;
@Override
public void initialize(ValidNumberValue constraintAnnotation) {
validValues = constraintAnnotation.value();
}
@Override
public boolean isValid(Integer value, ConstraintValidatorContext context) {
if(ArrayUtils.isEmpty(validValues)){
return false;
}
for (int validValue : validValues) {
if(Objects.equals(validValue, value)){
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,24 @@
package cn.azxo.framework.common.model;
/**
* 枚举抽象类
*
* @author zhaoyong
* @see EnumBase
* @since 2021-08-05 19:13
*/
public interface EnumBase<T> {
/**
* 枚举 code
* @return
*/
T getCode();
/**
* 枚举描述信息
* @return
*/
String getMessage();
}

View File

@ -51,4 +51,26 @@ public abstract class ArrayUtils {
return Array.getLength(array);
}
/**
* <p>Checks if an array of primitive ints is not empty and not {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is not empty and not {@code null}
* @since 2.5
*/
public static boolean isNotEmpty(final int[] array) {
return !isEmpty(array);
}
/**
* <p>Checks if an array of primitive ints is empty or {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is empty or {@code null}
* @since 2.1
*/
public static boolean isEmpty(final int[] array) {
return getLength(array) == 0;
}
}