java注解和反射(八)----反射操作泛型和注解

1. 反射操作泛型

[1] Java采用泛型擦除的机制来引入泛型,Java中泛型仅仅是给编译器javac使用的,确保数据的安全性和免去强制类型转化问题,但是,一旦编译完成,所有和泛型有关类型全部擦除。
[2] 为了通过反射操作这些类型,Java新增了ParameterizedType,GenericArrayType,TypeVariable和WildcardType几种类型来代表不能被归一到Class类中的类型但是又和原始类型齐名的类型。

  • ParameterizedType: 表示一种参数化类型,比如Collection<String>
  • GenericArrayType: 表示一种元素类型是参数化类型或者类型变量的数组类型
  • TypeVariable: 是各种类型变量的公共父接口
  • WildcardType: 代表一个通配符类型表达式

代码示例(在开发中使用较少)

package com.gs.reflection;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

//获得泛型的参数类型
public class Test11 {

    public void test01(Map<String,User> map, List<User> list){
        System.out.println("test01");
    }
    public Map<String,User> test02(){
        System.out.println("test02");
        return null;
    }

    public static void main(String[] args) throws NoSuchMethodException {
        //获取指定的方法
        Method method = Test11.class.getMethod("test01", Map.class, List.class);
        //获取相应的外层泛型参数
        // java.util.Map<java.lang.String, com.gs.reflection.User>
        //java.util.List<com.gs.reflection.User>
        Type[] genericParameterTypes = method.getGenericParameterTypes();
        for (Type genericParameterType : genericParameterTypes) {
            System.out.println("#"+genericParameterType);
            //输出内层的参数
            if(genericParameterType instanceof ParameterizedType){
                Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
                for (Type actualTypeArgument : actualTypeArguments) {
                    System.out.println(actualTypeArgument);
                }
            }
        }
        System.out.println("==========test02的返回值泛型=============");
        method = Test11.class.getMethod("test02",null);
        //获取相应的返回值泛型
        Type genericReturnType = method.getGenericReturnType();
        if(genericReturnType instanceof ParameterizedType){
            //得到返回值泛型的参数
            Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
            for (Type actualTypeArgument : actualTypeArguments) {
                System.out.println(actualTypeArgument);
            }
        }
    }
}

运行结果:
在这里插入图片描述

2. 反射操作注解

这里我们通过一个实例来说明这个操作的应用。
在这里插入图片描述

  • 类和表结构对应
  • 属性和字段对应
  • 对象和记录对应

这里我们就利用注解和反射完成类和表结构映射的关系

[1] 声明类名(相应表)的注解和属性字段的注解

//类名的注解(这里可以用来表示这个类与数据库中的哪张表对应)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableGs{
    String value();
}

//属性的注解(这里可以用来表示数据库中相应字段的属性)
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldGs{
	//列名
    String columnName();
    //类型
    String type();
    //长度
    int length();
}

[2] 声明一个相应的实体类

//这里表示与数据库中的哪张表进行映射
@TableGs("db_student")
class Student2{
	//声明属性中相应字段的值 ==>可以看成属性和字段对应
    @FieldGs(columnName = "db_id",type = "int",length = 10)
    private int id;
    @FieldGs(columnName = "db_age",type = "int",length = 10)
    private int age;
    @FieldGs(columnName = "db_name",type = "varchar",length = 3)
    private String name;

    public Student2() {
    }

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

    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 "Student2{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

[3] 我们通过一个Class类获得它相应的注解和值
getAnnotations() ===>获得所有注解
getAnnotation(注解类) ==>获得相应注解

public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class c1 = Class.forName("com.gs.reflection.Student2");

        //通过反射获得注解
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }

        //获得注解的value的值
        TableGs annotation = (TableGs)c1.getAnnotation(TableGs.class);
        String value = annotation.value();
        System.out.println(value);

        //获得类指定的注解
        Field f = c1.getDeclaredField("name");
        FieldGs annotation1 = f.getAnnotation(FieldGs.class);
        System.out.println(annotation1.columnName());
        System.out.println(annotation1.type());
        System.out.println(annotation1.length());
    }
}

运行结果
在这里插入图片描述

以上内容是学习了狂神说java后所写

发布了86 篇原创文章 · 获赞 6 · 访问量 7261

猜你喜欢

转载自blog.csdn.net/TheWindOfSon/article/details/105621260