当前位置: 首页 > 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.annotation.AnnotationUtil;
import cn.hutool.core.lang.func.Func1;
import org.springframework.core.annotation.AnnotationUtils;import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
import java.util.function.Predicate;/*** @author panxili* @date 2024/8/31*/
public class MyAnnotationUtil extends AnnotationUtil {/*** 获取JavaBean的所有成员属性上指定注解的列表。** @param beanClass       JavaBean的Class对象。* @param annotationClass 指定注解的Class对象。* @return 包含注解的成员属性列表。*/public static List<Field> findAnnotatedFields(Class<?> beanClass, Class<? extends Annotation> annotationClass) {List<Field> result = new ArrayList<>();Field[] fields = beanClass.getDeclaredFields();for (Field field : fields) {Annotation annotation = getAnnotation(field, annotationClass);if (annotation != null) {result.add(field);}}return result;}public static <A extends Annotation> List<A> findAnnotations(Class<?> beanClass, Class<A> annotationClass) {List<A> result = new ArrayList<>();Field[] fields = beanClass.getDeclaredFields();for (Field field : fields) {A annotation = AnnotationUtils.getAnnotation(field, annotationClass);if (annotation != null) {result.add(annotation);}}return result;}public static <A extends Annotation, R> List<R> getAnnotationValues(Class<?> beanClass, Class<A> annotationClass, Func1<A, R> propertyName, Predicate<R> predicate) {List<R> result = new ArrayList<>();List<A> annotations = findAnnotations(beanClass, annotationClass);for (final A annotation : annotations) {R called = propertyName.callWithRuntimeException(annotation);if (predicate.test(called)) {result.add(called);}}return result;}public static <A extends Annotation, R1, R2> Map<R1, R2> getAnnotationValueMap(Class<?> beanClass, Class<A> annotationClass, Func1<A, R1> keyPropertyName, Func1<A, R2> valuePropertyName, BiPredicate<R1, R2> predicate) {List<A> annotations = findAnnotations(beanClass, annotationClass);Map<R1, R2> result = new HashMap<>();for (final A annotation : annotations) {R1 key = keyPropertyName.callWithRuntimeException(annotation);R2 value = valuePropertyName.callWithRuntimeException(annotation);if (predicate.test(key, value)) {result.put(key, value);}}return result;}
}
public class Address {public static final Map<String, Boolean> COLUMN_ANNOTATION_VALUE_MAP = MyAnnotationUtil.getAnnotationValueMap(Address.class, Column.class, Column::name, Column::nullable, ObjUtil::isAllNotEmpty);
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 大数据技术之HBase 快速入门(2)
  • Parallels Desktop 19发布,无需重启即可在 Mac 上运行 Windows
  • VSCode设置复制 Ctrl+D想下复制
  • 知识社区的小程序源码系统 界面支持万能DIY装修 带源代码包以及搭建部署教程
  • 黑神话悟空-提高画质、防卡顿、修复等各种功能、各种CT表、各种存档、武器包、人物、装备替换等193+MOD合集
  • 汽车乘客热舒适度大挑战,如何利用仿真技术提高汽车环境舒适度
  • oracle11g常用基本字典和动态性能字典
  • Flask的上下文管理流程
  • 利用Streamlit前端框架开发Stable Diffusion模型图像生成网页应用(下篇)
  • SprinBoot+Vue实验室考勤管理小程序的设计与实现
  • c++关于字符串的练习
  • Camtasia 2024 v2024.0.6 for Mac 中文版 屏幕录像视频录制编辑软件
  • 开源项目管理工具Taiga
  • “添加”业务功能开发
  • 手把手带你实现C语言扫雷进阶(1)(接上回基础版扫雷,附上源码)
  • AHK 中 = 和 == 等比较运算符的用法
  • android图片蒙层
  • interface和setter,getter
  • iOS编译提示和导航提示
  • mongo索引构建
  • Mybatis初体验
  • PV统计优化设计
  • 第13期 DApp 榜单 :来,吃我这波安利
  • 近期前端发展计划
  • 容器服务kubernetes弹性伸缩高级用法
  • 携程小程序初体验
  • 用 Swift 编写面向协议的视图
  • 正则表达式小结
  • media数据库操作,可以进行增删改查,实现回收站,隐私照片功能 SharedPreferences存储地址:
  • linux 淘宝开源监控工具tsar
  • ​ 全球云科技基础设施:亚马逊云科技的海外服务器网络如何演进
  • ​软考-高级-系统架构设计师教程(清华第2版)【第1章-绪论-思维导图】​
  • #### golang中【堆】的使用及底层 ####
  • #13 yum、编译安装与sed命令的使用
  • #Linux(权限管理)
  • (附源码)计算机毕业设计SSM教师教学质量评价系统
  • (力扣记录)1448. 统计二叉树中好节点的数目
  • (四)linux文件内容查看
  • (完整代码)R语言中利用SVM-RFE机器学习算法筛选关键因子
  • .helper勒索病毒的最新威胁:如何恢复您的数据?
  • .NET 4.0中使用内存映射文件实现进程通讯
  • .NET Core中Emit的使用
  • .net dataexcel winform控件 更新 日志
  • .NET_WebForm_layui控件使用及与webform联合使用
  • .net开发引用程序集提示没有强名称的解决办法
  • .NET开发者必备的11款免费工具
  • .net下的富文本编辑器FCKeditor的配置方法
  • /ThinkPHP/Library/Think/Storage/Driver/File.class.php  LINE: 48
  • @Transaction注解失效的几种场景(附有示例代码)
  • [2015][note]基于薄向列液晶层的可调谐THz fishnet超材料快速开关——
  • [bzoj 3534][Sdoi2014] 重建
  • [CISCN2019 华东南赛区]Web4
  • [CSS] - 修正IE6不支持position:fixed的bug
  • [c语言]小课堂 day2
  • [ffmpeg] av_opt_set 解析