Java 基础复习 Day 29

Java 基础复习 Day 29

注解与反射 2

1.通过反射获取方法的参数和返回值的泛型类型

/通过反射获取泛型
public class Test11 {
    public void test1(Map<String, User> map, List<User> users){
        System.out.println("test01");
    }
    public Map<String, User> test(){
        System.out.println("test02");
        return null;

    }

    public static void main(String[] args) throws NoSuchMethodException {
        //获得方法test1的泛型参数类型
        Method method1 = Test11.class.getDeclaredMethod("test1", Map.class, List.class);
        Type[] types = method1.getGenericParameterTypes();//获得泛型参数类型
        for (Type type : types) {
            System.out.println("###:" + type);
            if (type instanceof ParameterizedType) {//判断参数类型是不是结构化参数类型(Collection之类) 如果是就要获得真实的参数是什么
                Type[] actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments();;
                for (Type actualTypeArgument : actualTypeArguments) {
                    System.out.println("actual type: " + actualTypeArgument);
                }
            }
            //##:java.util.Map<java.lang.String, com.kou.reflection.User>
            //actual type: class java.lang.String
            //actual type: class com.kou.reflection.User
            //###:java.util.List<com.kou.reflection.User>
            //actual type: class com.kou.reflection.User
        }
        //获得方法test2的泛型参数类型
        Method method2 = Test11.class.getDeclaredMethod("test", null);
        Type returnType = method2.getGenericReturnType();
        if(returnType instanceof ParameterizedType){
            Type[] arguments = ((ParameterizedType) returnType).getActualTypeArguments();
            for (Type type : arguments) {
                System.out.println("actual type2" + type);
                //actual type2class java.lang.String
                //actual type2class com.kou.reflection.User
            }
        }
    }
}

2.通过反射操作注解

public class Test12 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class c1= Class.forName("com.kou.reflection.Worker");
        //通过反射获得注解
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println("annotation: " + annotation);//只是获得了注解 //annotation: @com.kou.reflection.TableW1(value=tb_worker)
        }
        //获取注解的值
        TableW1 tableW1 = (TableW1)c1.getAnnotation(TableW1.class);
        System.out.println(tableW1.value());//tb_worker
        //获得类指定的注解
        Field f1 = c1.getDeclaredField("name");
        FieldW1 annotation = f1.getAnnotation(FieldW1.class);
        System.out.println(annotation.columnName());//db_name
        System.out.println(annotation.length());//40
        System.out.println(annotation.type());//varchar
    }
}
@TableW1(value = "tb_worker")
class Worker{
    @FieldW1(columnName = "db_id",type = "int",length = 20)
    private int id;
    @FieldW1(columnName = "db_age",type = "int",length = 20)
    private int age;
    @FieldW1(columnName = "db_name",type = "varchar",length = 40)
    private String name;

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

    public Worker() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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;
    }

    @Override
    public String toString() {
        return "Worker{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

//1.类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableW1{
    String value();
}
//2.属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldW1{
    String columnName();
    String type();
    int length();
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值