Skip to content

Commit

Permalink
Merge pull request #4 from stick-i/v0.0.3-beta
Browse files Browse the repository at this point in the history
V0.0.3 beta
  • Loading branch information
stick-i authored May 9, 2024
2 parents e2f3447 + d7d7d21 commit 6bd7097
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 52 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cn.sticki</groupId>
<artifactId>spel-validator</artifactId>
<version>0.0.2-beta</version>
<version>0.0.3-beta</version>
<packaging>jar</packaging>
<name>Spel Validator</name>
<url>https://github.com/stick-i/spel-validator</url>
Expand Down Expand Up @@ -57,9 +57,9 @@

<dependencies>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.2.5.Final</version>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
<optional>true</optional>
</dependency>

Expand Down
37 changes: 0 additions & 37 deletions src/main/java/cn/sticki/validator/spel/GetMethod.java

This file was deleted.

30 changes: 20 additions & 10 deletions src/main/java/cn/sticki/validator/spel/SpelValidExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.sticki.validator.spel.exception.SpelNotSupportedTypeException;
import cn.sticki.validator.spel.exception.SpelValidException;
import cn.sticki.validator.spel.manager.AnnotationMethodManager;
import cn.sticki.validator.spel.parse.SpelParser;
import cn.sticki.validator.spel.result.FieldValidResult;
import cn.sticki.validator.spel.result.ObjectValidResult;
Expand Down Expand Up @@ -58,6 +59,7 @@ public static ObjectValidResult validateObject(@NotNull Object verifiedObject) {
public static ObjectValidResult validateObject(@NotNull Object verifiedObject, @NotNull Set<Object> validateGroups) {
log.debug("Spel validate start, class [{}], groups [{}]", verifiedObject.getClass().getName(), validateGroups);
log.debug("Verified object [{}]", verifiedObject);
long startTime = System.nanoTime();

List<FieldValidResult> validationResults = new ArrayList<>();

Expand All @@ -71,7 +73,8 @@ public static ObjectValidResult validateObject(@NotNull Object verifiedObject, @
ObjectValidResult validResult = new ObjectValidResult();
validResult.addFieldResults(validationResults);

log.debug("Spel validate over, result {}", validResult.getErrors());
log.debug("Spel validate over,error list {}", validResult.getErrors());
log.debug("Spel validate cost time {} ms", (System.nanoTime() - startTime) / 1000000);
return validResult;
}

Expand Down Expand Up @@ -133,12 +136,12 @@ private static void validateFieldAnnotation(
Class<? extends SpelConstraintValidator<?>> validatorClass = annoClazz.getAnnotation(SpelConstraint.class).validatedBy();
SpelConstraintValidator<? extends Annotation> validator = getValidatorInstance(validatorClass);

// 判断对象的类型是否受支持
// 判断字段的类型是否受支持
Set<Class<?>> supported = validator.supportType();
Class<?> verifiedObjectClass = verifiedObject.getClass();
if (supported.stream().noneMatch(clazz -> clazz.isInstance(verifiedObjectClass))) {
Class<?> verifiedFieldClass = verifiedField.getType();
if (supported.stream().noneMatch(clazz -> clazz.isAssignableFrom(verifiedFieldClass))) {
log.error("===> Object type not supported, skip validate. supported types [{}]", supported);
throw new SpelNotSupportedTypeException(verifiedObjectClass, supported);
throw new SpelNotSupportedTypeException(verifiedFieldClass, supported);
}

// 匹配分组
Expand Down Expand Up @@ -215,17 +218,17 @@ private static boolean isSpelConstraintAnnotation(@NotNull Class<? extends Annot
return false;
}

if (GetMethod.action(annotationType, MESSAGE).run() == null) {
if (AnnotationMethodManager.get(annotationType, MESSAGE) == null) {
log.warn("The annotation [{}] must have a method named [message] that returns a string.", annotationType.getName());
return false;
}

if (GetMethod.action(annotationType, CONDITION).run() == null) {
if (AnnotationMethodManager.get(annotationType, CONDITION) == null) {
log.warn("The annotation [{}] must have a method named [condition] that returns a string.", annotationType.getName());
return false;
}

if (GetMethod.action(annotationType, GROUP).run() == null) {
if (AnnotationMethodManager.get(annotationType, GROUP) == null) {
log.warn("The annotation [{}] must have a method named [group] that returns a Array<String>.", annotationType.getName());
return false;
}
Expand Down Expand Up @@ -295,9 +298,16 @@ private static <T extends SpelConstraintValidator<?>> SpelConstraintValidator<?>
});
}

// todo 缓存
/**
* 获取注解方法的属性值
*
* @param annotation 注解对象
* @param methodName 方法名
* @param <T> 返回值类型
* @return 属性值
*/
private static <T> T getAnnotationValue(@NotNull Annotation annotation, @NotNull String methodName) {
Method method = GetMethod.action(annotation.annotationType(), methodName).run();
Method method = AnnotationMethodManager.get(annotation.annotationType(), methodName);
try {
//noinspection unchecked
return (T) method.invoke(annotation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* 被标记的元素必须为非空字符串,不能为 {@code null},也不能为长度为0的字符串,也不能全是空白字符。
* 被标记的元素必须为非空字符串。
* <p>
* 不能为 {@code null},长度必须大于 0,不能全是空白字符。
* <p>
* 接受 {@link CharSequence} 类型。
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cn.sticki.validator.spel.manager;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.concurrent.ConcurrentHashMap;

/**
* 注解类方法管理器。
*
* @author 阿杆
* @version 1.0
* @since 2024/5/9
*/
public class AnnotationMethodManager {

private final static ConcurrentHashMap<String, Method> METHOD_CACHE = new ConcurrentHashMap<>();

/**
* 获取方法。
*
* @param clazz 注解类
* @param methodName 方法名
* @return 方法,如果不存在则返回null
*/
public static Method get(Class<? extends Annotation> clazz, String methodName) {
// 由于注解类的方法无法重载,可以通过注解类和方法名来唯一确定一个方法
return METHOD_CACHE.computeIfAbsent(clazz.toString() + methodName, s -> {
try {
return clazz.getMethod(methodName);
} catch (NoSuchMethodException e) {
return null;
}
});
}

}

0 comments on commit 6bd7097

Please sign in to comment.