当前位置: 首页 > news >正文

获取指定类的所有成员属性上的指定注解的属性值

示例

javax.persistence.Column.java

@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {String name() default "";boolean unique() default false;boolean nullable() default true;boolean insertable() default true;boolean updatable() default true;String columnDefinition() default "";String table() default "";int length() default 255;int precision() default 0;int scale() default 0;
}

com.pxl.springbootdemo.entity.Address .java

@Entity
@Data
public class Address {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name = "id", nullable = false)private Long id;//id@Column(name = "phone", nullable = true, length = 11)private String phone;//手机@Column(name = "zipcode", nullable = true, length = 6)private String zipcode;//邮政编码@Column(name = "address", nullable = true, length = 100)private String addressStr;//地址@Columnprivate String addressStr1;//地址private String addressStr2;//地址public static final Map<String, Boolean> COLUMN_ANNOTATION_VALUE_MAP = new HashMap<>();static {// 获取类中所有声明的字段Field[] fields = Address.class.getDeclaredFields();for (Field field : fields) {// 获取字段上的@Column注解Column columnAnnotation = field.getAnnotation(Column.class);if (columnAnnotation == null) {continue;}// 获取注解的name和nullable属性String name = columnAnnotation.name();boolean nullable = columnAnnotation.nullable();if (StrUtil.isEmpty(name)) {continue;}// 将name和nullable添加到Map中COLUMN_ANNOTATION_VALUE_MAP.put(name, nullable);}
}

优化-抽取工具方法

package com.pxl.springbootdemo.util;import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.func.Func1;
import cn.hutool.core.util.ReflectUtil;
import com.sun.tools.javac.util.Pair;
import org.springframework.core.annotation.AnnotationUtils;import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.*;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import java.util.stream.Collectors;/*** 注解工具类** @date 2024/8/31*/
public class AnnotationUtil extends cn.hutool.core.annotation.AnnotationUtil {/*** 获取指定类的包含指定注解的所有成员属性列表。** @param beanClass       指定类* @param annotationClass 指定注解* @return 包含指定注解的所有成员属性列表*/public static <A extends Annotation> List<Field> findAnnotatedFields(Class<?> beanClass, Class<A> annotationClass) {Field[] annotatedFields = ReflectUtil.getFields(beanClass, field -> getAnnotation(field, annotationClass) != null);if (annotatedFields == null) {return Collections.emptyList();}return Arrays.asList(annotatedFields);}/*** 获取指定类的所有成员属性包含的指定注解列表。** @param beanClass       指定类* @param annotationClass 指定注解* @param <A>             注解类型* @return 所有成员属性包含的指定注解列表*/public static <A extends Annotation> List<A> findAnnotationsOnFields(Class<?> beanClass, Class<A> annotationClass) {Field[] fields = ReflectUtil.getFields(beanClass);if (fields == null) {return Collections.emptyList();}return Arrays.stream(fields).map(field -> AnnotationUtils.getAnnotation(field, annotationClass)).filter(Objects::nonNull).collect(Collectors.toList());}/*** 获取指定类的所有成员属性包含的指定注解的指定属性列表。** @param beanClass       指定类* @param annotationClass 指定注解* @param propertyName    获取属性值的函数* @param predicate       属性值过滤函数* @param <A>             注解类型* @param <R>             属性值类型* @return 所有成员属性包含的指定注解的指定属性列表*/public static <A extends Annotation, R> List<R> getAnnotationValuesOnFields(Class<?> beanClass, Class<A> annotationClass, Func1<A, R> propertyName, Predicate<R> predicate) {List<A> annotations = findAnnotationsOnFields(beanClass, annotationClass);return CollUtil.emptyIfNull(annotations).stream().map(propertyName::callWithRuntimeException).filter(predicate).collect(Collectors.toList());}/*** 获取指定类的所有成员属性上指定注解的指定两个属性值,并返回一个Map,key为第一个属性值,value为第二个属性值** @param beanClass         指定类* @param annotationClass   指定注解* @param keyPropertyName   获取第一个属性值的函数* @param valuePropertyName 获取第二个属性值的函数* @param predicate         第一个属性值和第二个属性值的过滤函数* @param <A>               注解类型* @param <R1>              第一个属性值类型* @param <R2>              第二个属性值类型* @return 指定类的所有成员属性上指定注解的指定两个属性值*/public static <A extends Annotation, R1, R2> Map<R1, R2> getTwoAnnotationValues(Class<?> beanClass, Class<A> annotationClass, Func1<A, R1> keyPropertyName, Func1<A, R2> valuePropertyName, BiPredicate<R1, R2> predicate) {List<A> annotations = findAnnotationsOnFields(beanClass, annotationClass);return CollUtil.emptyIfNull(annotations).stream().map(e -> Pair.of(keyPropertyName.callWithRuntimeException(e), valuePropertyName.callWithRuntimeException(e))).filter(pair -> predicate.test(pair.fst, pair.snd)).collect(Collectors.toMap(pair -> pair.fst, pair -> pair.snd, (k1, k2) -> k1));}
}
public class Address {public static final Map<String, Boolean> COLUMN_ANNOTATION_VALUE_MAP = AnnotationUtil.getTwoAnnotationValues(Address.class, Column.class, Column::name, Column::nullable, ObjUtil::isAllNotEmpty);
}

http://www.mrgr.cn/news/16856.html

相关文章:

  • C++ | Leetcode C++题解之第388题文件的最长绝对路径
  • Ansible自动化运维项目
  • 栈和队列的习题详解(1):有效的括号
  • 火语言RPA流程组件介绍--浏览选择文件夹
  • 算法设计与分析:实验六 图论——最大流应用问题
  • 地震微分方程代码 - 第一部分
  • Python虚拟环境创建和使用总结
  • python语言基础(五)--面向对象和三大特征
  • SqlDataAdapter和SqlDataReader的区别
  • 深入理解堆排序算法及其时间复杂度分析
  • HTB-Crocodile(FTP和web综合利用)
  • Matrix:重塑APM领域,以简驭繁的性能监控新纪元
  • 初等数学几百年重大错误:N各元n的对应n+1的全体是N的真子集N+——百年病态集论的症结
  • 2024年8月总结及随笔之逝
  • [免越狱]FLEXTool/FLEX 炫酷功能怎么添加到目标App
  • Python enumerate()函数详解
  • Flask中的g的作用
  • 旋转排序:搜索算法
  • git branch 不显示分支名称
  • docker实战基础一