• 关于反射与自定义注解的一些使用


    对实体类的属性进行校验,等处理。

    自定义注解

    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Inherited;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    @Documented
    @Inherited
    public @interface OrderAnnotation {
    
        int length();
    
        int sort();
    
    }

    实体类

    public class Merchandise {
    
        @OrderAnnotation(sort=1,length=10)
        private String id;
    
        @OrderAnnotation(sort=2,length=50)
        private String name;
    
        @OrderAnnotation(sort=3,length=100)
        private String desc;
    
        ... getset 方法和toString 方法
    
    }
    import java.beans.IntrospectionException;
    import java.beans.PropertyDescriptor;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    
    import org.springframework.util.ReflectionUtils;
    
    public class Main {
    
        public static void main(String[] args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException {
            Merchandise merchandise = new Merchandise();
            merchandise.setId("11");
            merchandise.setName("sanye");
            merchandise.setDesc("descdesc...中");
            re(merchandise);
            System.out.println(merchandise.toString());
        }
    
        public static void re(Object o) throws IntrospectionException, IllegalArgumentException, IllegalAccessException{
            Class c = o.getClass();
            Field[] fields = c.getDeclaredFields();
            for (Field field : fields) {
                // 得到注解
                if(field.isAnnotationPresent(OrderAnnotation.class)) {
                    OrderAnnotation orderAnnotation = field.getAnnotation(OrderAnnotation.class);
                    int sort = orderAnnotation.sort();
                    int length = orderAnnotation.length();
                    System.out.println("顺序:"+sort+"   长度:"+length);
    
                }
    
                //获取字段值
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), c);
                Method getMethod = pd.getReadMethod();
                Object valueObj = ReflectionUtils.invokeMethod(getMethod, o);//执行方法
                System.out.println(field.getName() + "  "+String.valueOf(valueObj));
    
                //设置字段值
                field.setAccessible(true);
                field.set(o, "aaaaaaaaa");
                field.setAccessible(false);
    
            }
    
        }
    
    }

    结果:

    顺序:1   长度:10
    id  11
    顺序:2   长度:50
    name  sanye
    顺序:3   长度:100
    desc  descdesc...中
    Merchandise [id=aaaaaaaaa, name=aaaaaaaaa, desc=aaaaaaaaa]
  • 相关阅读:
    tensorflow (七) k-means
    如何提升开发效率-前端技术选型篇
    Spring AOP详解
    Vue.js学习笔记-入门
    拥抱Vue,抛弃jQuery
    HTML+CSS实现审核流程步骤效果
    测试SpringMVC可能出现的线程安全问题
    CGLib动态创建对象和属性
    jconsole观察jvm中线程及GC情况
    Java NIO学习笔记-Selector
  • 原文地址:https://www.cnblogs.com/yrjns/p/12182254.html
Copyright © 2020-2023  润新知