注解反射之使用Class对象获取注解

代码如下

package com.loubin;

import java.lang.annotation.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        User user = new User("张三", 18);
        Class c = user.getClass();
        UserTable userTable = (UserTable) c.getAnnotation(UserTable.class);
        String tableName = userTable.tableName();
        System.out.println(tableName);

        Field fieldName = c.getDeclaredField("name");
        UserColumn userColumn = fieldName.getAnnotation(UserColumn.class);
        System.out.println(userColumn.columnName());
        System.out.println(userColumn.columnType());
        System.out.println(userColumn.columnLength());

//        Class c = User.class;
//        String name = c.getName();
//        System.out.println(name);
//        System.out.println("获取所有属性-------------------------------");
//        Field[] fields = c.getDeclaredFields();
//        for (Field field : fields) {
//            System.out.println(field.getName());
//        }
//        System.out.println("获取指定属性-----------------------------------");
//        Field field = c.getDeclaredField("name");
//        System.out.println(field.getName());
//        System.out.println("获取所有方法----------------------------------");
//        Method[] methods = c.getDeclaredMethods();
//        for (Method method : methods) {
//            System.out.println(method.getName());
//        }
//        System.out.println("获取指定方法----------------------------------");
//        Method method = c.getDeclaredMethod("getName");
//        System.out.println(method.getName());
//        System.out.println("获取无参构造器------------------------------------");
//        Constructor constructor = c.getDeclaredConstructor();
//        System.out.println(constructor.getName());
//        System.out.println("获取有参构造器-----------------------------------");
//        Constructor constructor1 = c.getDeclaredConstructor(String.class, int.class);
    }
}

@UserTable(tableName = "db_user")
class User{
    @UserColumn(columnName = "db_name", columnType = "string", columnLength = 20)
    String  name;
    @UserColumn(columnName = "db_age", columnType = "int", columnLength = 20)
    int age;

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void shout(String name){
        System.out.println("是谁叫了一下" + name);
    }
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


}

@Target(value = ElementType.TYPE)
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
@Inherited
@interface UserTable{
    String tableName();
}

@Target(value = ElementType.FIELD)
@Retention(value = RetentionPolicy.RUNTIME)
@interface UserColumn{
    String columnName();
    String columnType();
    int columnLength();
}

 

测试结果如下

 

posted @ 2025-02-07 23:28  地球上最后一个直男  阅读(12)  评论(0)    收藏  举报