feat: 抽离公共注解工具类
This commit is contained in:
parent
764d25f15c
commit
eb4b732416
28
flyfish-bean/pom.xml
Normal file
28
flyfish-bean/pom.xml
Normal file
@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>dev.flyfish.framework</groupId>
|
||||
<artifactId>flyfish-framework</artifactId>
|
||||
<version>2.0.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>flyfish-bean</artifactId>
|
||||
<name>flyfish-parser</name>
|
||||
<description>飞鱼解析器</description>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1 @@
|
||||
package dev.flyfish.framework.beans.enums;
|
@ -0,0 +1 @@
|
||||
package dev.flyfish.framework.beans.meta;
|
@ -6,6 +6,7 @@ import org.springframework.core.annotation.MergedAnnotation;
|
||||
import org.springframework.core.annotation.MergedAnnotations;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
|
||||
/**
|
||||
* 基于spring annotations最高级封装
|
||||
@ -17,6 +18,16 @@ import java.lang.annotation.Annotation;
|
||||
*/
|
||||
public interface BeanPropertyAnnotations {
|
||||
|
||||
/**
|
||||
* 来自反射
|
||||
*
|
||||
* @param annotated 字段
|
||||
* @return 结果
|
||||
*/
|
||||
static BeanPropertyAnnotations from(AnnotatedElement annotated) {
|
||||
return new SimpleBeanPropertyAnnotations(annotated);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否有某个注解
|
||||
*
|
||||
@ -84,4 +95,17 @@ public interface BeanPropertyAnnotations {
|
||||
* @return 结果
|
||||
*/
|
||||
<A extends Annotation, E> BeanPropertyAnnotationBatchChain<A, E> batchEmpty();
|
||||
|
||||
/**
|
||||
* 强制类型转换
|
||||
*
|
||||
* @param bean 入参
|
||||
* @param <T> 入参泛型
|
||||
* @param <R> 出参泛型
|
||||
* @return 结果
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
static <T, R> R cast(T bean) {
|
||||
return (R) bean;
|
||||
}
|
||||
}
|
@ -6,17 +6,18 @@ import dev.flyfish.framework.beans.meta.parser.chain.impl.EmptyAnnotationBatchCh
|
||||
import dev.flyfish.framework.beans.meta.parser.chain.impl.EmptyAnnotationChain;
|
||||
import dev.flyfish.framework.beans.meta.parser.chain.impl.SimpleAnnotationBatchChain;
|
||||
import dev.flyfish.framework.beans.meta.parser.chain.impl.SimpleAnnotationChain;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.springframework.core.annotation.MergedAnnotation;
|
||||
import org.springframework.core.annotation.MergedAnnotations;
|
||||
import org.springframework.data.util.CastUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static dev.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations.cast;
|
||||
|
||||
/**
|
||||
* bean属性注解容器
|
||||
*
|
||||
@ -24,14 +25,14 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
public class SimpleBeanPropertyAnnotations implements BeanPropertyAnnotations {
|
||||
|
||||
private final Field field;
|
||||
private final AnnotatedElement field;
|
||||
|
||||
private final MergedAnnotations annotations;
|
||||
private final EmptyAnnotationChain<? extends Annotation, ?> emptyChain = new EmptyAnnotationChain<>(this);
|
||||
private final EmptyAnnotationBatchChain<? extends Annotation, ?> emptyBatchChain = new EmptyAnnotationBatchChain<>(this);
|
||||
private boolean last;
|
||||
|
||||
public SimpleBeanPropertyAnnotations(Field field) {
|
||||
SimpleBeanPropertyAnnotations(AnnotatedElement field) {
|
||||
this.field = field;
|
||||
this.annotations = null == field ? MergedAnnotations.from() : MergedAnnotations.from(field);
|
||||
}
|
||||
@ -98,7 +99,7 @@ public class SimpleBeanPropertyAnnotations implements BeanPropertyAnnotations {
|
||||
List<MergedAnnotation<A>> values = this.annotations.stream(annotationType)
|
||||
.filter(MergedAnnotation::isPresent)
|
||||
.collect(Collectors.toList());
|
||||
if (CollectionUtils.isNotEmpty(values)) {
|
||||
if (!CollectionUtils.isEmpty(values)) {
|
||||
return new SimpleAnnotationBatchChain<>(this.last(true), values);
|
||||
}
|
||||
}
|
||||
@ -135,7 +136,7 @@ public class SimpleBeanPropertyAnnotations implements BeanPropertyAnnotations {
|
||||
*/
|
||||
@Override
|
||||
public <A extends Annotation, E> BeanPropertyAnnotationChain<A, E> empty() {
|
||||
return CastUtils.cast(emptyChain);
|
||||
return cast(emptyChain);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -144,7 +145,8 @@ public class SimpleBeanPropertyAnnotations implements BeanPropertyAnnotations {
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
|
||||
public <A extends Annotation, E> BeanPropertyAnnotationBatchChain<A, E> batchEmpty() {
|
||||
return CastUtils.cast(emptyBatchChain);
|
||||
return cast(emptyBatchChain);
|
||||
}
|
||||
}
|
@ -2,7 +2,6 @@ package dev.flyfish.framework.beans.meta.parser.chain.impl;
|
||||
|
||||
import dev.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations;
|
||||
import dev.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationBatchChain;
|
||||
import org.springframework.data.util.CastUtils;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Collections;
|
||||
@ -12,6 +11,8 @@ import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static dev.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations.cast;
|
||||
|
||||
/**
|
||||
* 单例的,空实现
|
||||
*
|
||||
@ -32,7 +33,7 @@ public class EmptyAnnotationBatchChain<A extends Annotation, T> extends BasicAnn
|
||||
*/
|
||||
@Override
|
||||
public <R> BeanPropertyAnnotationBatchChain<A, R> map(Function<T, R> mapper) {
|
||||
return CastUtils.cast(this);
|
||||
return cast(this);
|
||||
}
|
||||
|
||||
/**
|
@ -2,7 +2,6 @@ package dev.flyfish.framework.beans.meta.parser.chain.impl;
|
||||
|
||||
import dev.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations;
|
||||
import dev.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationChain;
|
||||
import org.springframework.data.util.CastUtils;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.function.Consumer;
|
||||
@ -10,6 +9,8 @@ import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static dev.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations.cast;
|
||||
|
||||
/**
|
||||
* 单例的,空实现
|
||||
*
|
||||
@ -31,7 +32,7 @@ public class EmptyAnnotationChain<A extends Annotation, T> extends BasicAnnotati
|
||||
*/
|
||||
@Override
|
||||
public <R> BeanPropertyAnnotationChain<A, R> map(Function<T, R> mapper) {
|
||||
return CastUtils.cast(this);
|
||||
return cast(this);
|
||||
}
|
||||
|
||||
/**
|
@ -2,9 +2,8 @@ package dev.flyfish.framework.beans.meta.parser.chain.impl;
|
||||
|
||||
import dev.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations;
|
||||
import dev.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationBatchChain;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.springframework.core.annotation.MergedAnnotation;
|
||||
import org.springframework.data.util.CastUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Collections;
|
||||
@ -16,6 +15,8 @@ import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static dev.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations.cast;
|
||||
|
||||
/**
|
||||
* 简单的批量注解链式实现
|
||||
*
|
||||
@ -50,8 +51,8 @@ public class SimpleAnnotationBatchChain<A extends Annotation, T> extends BasicAn
|
||||
return parent.batchEmpty();
|
||||
}
|
||||
this.mapped = true;
|
||||
this.values = CastUtils.cast(list);
|
||||
return CastUtils.cast(this);
|
||||
this.values = cast(list);
|
||||
return cast(this);
|
||||
}
|
||||
|
||||
/**
|
@ -3,7 +3,6 @@ package dev.flyfish.framework.beans.meta.parser.chain.impl;
|
||||
import dev.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations;
|
||||
import dev.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationChain;
|
||||
import org.springframework.core.annotation.MergedAnnotation;
|
||||
import org.springframework.data.util.CastUtils;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.function.Consumer;
|
||||
@ -11,6 +10,8 @@ import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static dev.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations.cast;
|
||||
|
||||
/**
|
||||
* 简单的注解链
|
||||
*
|
||||
@ -40,8 +41,8 @@ public class SimpleAnnotationChain<A extends Annotation, T> extends BasicAnnotat
|
||||
if (null == value) {
|
||||
return parent.empty();
|
||||
}
|
||||
this.value = CastUtils.cast(value);
|
||||
return CastUtils.cast(this);
|
||||
this.value = cast(value);
|
||||
return cast(this);
|
||||
}
|
||||
|
||||
/**
|
@ -120,7 +120,6 @@ public class User extends AuditDomain implements IUser {
|
||||
@Transient
|
||||
private Object detail;
|
||||
|
||||
|
||||
@Override
|
||||
public User toUser() {
|
||||
return this;
|
||||
|
@ -17,6 +17,11 @@
|
||||
<artifactId>flyfish-data-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>dev.flyfish.framework</groupId>
|
||||
<artifactId>flyfish-bean</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-core</artifactId>
|
||||
|
@ -8,6 +8,7 @@ import dev.flyfish.framework.validation.annotations.Money;
|
||||
import dev.flyfish.framework.validation.annotations.Phone;
|
||||
import dev.flyfish.framework.validation.annotations.UniqueField;
|
||||
import dev.flyfish.framework.validation.enums.PhoneType;
|
||||
import jakarta.validation.constraints.*;
|
||||
import lombok.Getter;
|
||||
import lombok.val;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
@ -17,7 +18,6 @@ import org.hibernate.validator.constraints.URL;
|
||||
import org.springframework.core.annotation.MergedAnnotation;
|
||||
import org.springframework.core.annotation.MergedAnnotations;
|
||||
|
||||
import jakarta.validation.constraints.*;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
@ -7,13 +7,11 @@ import dev.flyfish.framework.annotations.*;
|
||||
import dev.flyfish.framework.beans.enums.ValidationCandidate;
|
||||
import dev.flyfish.framework.beans.meta.builtin.BeanUris;
|
||||
import dev.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations;
|
||||
import dev.flyfish.framework.beans.meta.parser.SimpleBeanPropertyAnnotations;
|
||||
import dev.flyfish.framework.domain.base.Qo;
|
||||
import dev.flyfish.framework.domain.base.Vo;
|
||||
import dev.flyfish.framework.utils.ReflectionUtils;
|
||||
import dev.flyfish.framework.utils.StringFormats;
|
||||
import dev.flyfish.framework.validation.annotations.Money;
|
||||
import dev.flyfish.framework.annotations.*;
|
||||
import lombok.Data;
|
||||
import lombok.val;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
@ -119,7 +117,7 @@ public class BeanProperty {
|
||||
boolean strict = Qo.class.isAssignableFrom(beanClass) || Vo.class.isAssignableFrom(beanClass);
|
||||
// 尝试获取field
|
||||
Field field = FieldUtils.getField(beanClass, descriptor.getName(), true);
|
||||
BeanPropertyAnnotations annotations = new SimpleBeanPropertyAnnotations(field);
|
||||
BeanPropertyAnnotations annotations = BeanPropertyAnnotations.from(field);
|
||||
// 判断是否需要跳过,启用严格模式并未标记的属性,将直接跳过
|
||||
boolean skip = null == field || !annotations.isPresent(Property.class);
|
||||
if (skip && strict) {
|
||||
|
@ -1,10 +0,0 @@
|
||||
package dev.flyfish.framework.beans.meta.parser;
|
||||
|
||||
/**
|
||||
* bean属性注解解析器
|
||||
* @author wangyu
|
||||
*/
|
||||
public class BeanPropertyAnnotationParser {
|
||||
|
||||
|
||||
}
|
9
pom.xml
9
pom.xml
@ -17,7 +17,7 @@
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<revision>2.0.0</revision>
|
||||
<revision>2.0.1</revision>
|
||||
<docker.image.prefix>flyfish</docker.image.prefix>
|
||||
<docker.plugin.version>0.27.2</docker.plugin.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
@ -65,6 +65,7 @@
|
||||
<module>flyfish-form</module>
|
||||
<module>flyfish-approval</module>
|
||||
<module>flyfish-backup</module>
|
||||
<module>flyfish-bean</module>
|
||||
</modules>
|
||||
|
||||
<repositories>
|
||||
@ -120,6 +121,12 @@
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>dev.flyfish.framework</groupId>
|
||||
<artifactId>flyfish-bean</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>dev.flyfish.framework</groupId>
|
||||
<artifactId>flyfish-data-relational</artifactId>
|
||||
|
Loading…
x
Reference in New Issue
Block a user