ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

注解结合反射,天衣无缝

2022-07-23 23:04:38  阅读:124  来源: 互联网

标签:aClass 天衣无缝 String 反射 System 注解 println import out


请看示例代码

    package com.xxd.modules.util;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.ToString;
    
    import java.lang.annotation.*;
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    public class RefTest {
    
        public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
            Class aClass = Class.forName("com.xxd.modules.util.Student");
            Student student = (Student) aClass.newInstance();
            // 通过反射执行方法
            Method method = aClass.getDeclaredMethod("setName", String.class);
            method.invoke(student, "润哥");
            System.out.println(student);
    
            // 通过反射获取注解
            Annotation[] annotations = aClass.getAnnotations();
            for (Annotation annotation : annotations) {
                System.out.println(annotation);
            }
    
            // 获取指定注解的值
            TableRun tableRun = (TableRun) aClass.getAnnotation(TableRun.class);
            String value = tableRun.value();
            System.out.println(value);
    
            // 获取属性中指定的注解
            Field field = aClass.getDeclaredField("name");
            FieldRun fieldRun = field.getAnnotation(FieldRun.class);
            System.out.println(fieldRun.columnName());
            System.out.println(fieldRun.type());
            System.out.println(fieldRun.length());
    
        }
    }
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @ToString
    @TableRun("db_student")
    class Student {
        @FieldRun(columnName = "id", type = "int", length = 8)
        private int id;
        @FieldRun(columnName = "age", type = "int", length = 8)
        private int age;
        @FieldRun(columnName = "name", type = "varchar", length = 8)
        private String name;
    
    }
    
    // 类名的注解
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @interface TableRun {
        String value();
    }
    
    // 属性的注解
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    @Documented
    @interface FieldRun {
        String columnName();
    
        String type();
    
        int length();
    }

标签:aClass,天衣无缝,String,反射,System,注解,println,import,out
来源: https://www.cnblogs.com/flyingrun/p/16513477.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有