当前位置: 首页> 房产> 政策 > 获取指定类的所有成员属性上的指定注解的属性值

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

时间:2025/7/15 1:31:41来源:https://blog.csdn.net/qq_27579471/article/details/141760779 浏览次数:1次

示例

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);
}
关键字:获取指定类的所有成员属性上的指定注解的属性值

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: