注解和反射(四)反射获取泛型、反射获取注解

注解和反射(四)反射获取泛型、反射获取注解

反射获取泛型

  • Java采用泛型擦除机制来引入泛型,一旦编译完成,所有和泛型有关的类型全部擦除

反射操作泛型所需的方法

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

反射获取注解

ORM

  • 对象关系映射
  • 类和表的对应关系:
    • 类和表结构(整个表)对应
    • 属性和字段(列名)对应
    • 对象和记录(内容)对应

获得注解的方法

getAnnotations()

代码实例

反射操作泛型

public class Test07 {
    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 = Test07.class.getMethod("test01", Map.class, List.class);
        //获得类型的参数类型
        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);
                }
            }
        }
        //方法二
        method = Test07.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);
            }
        }
    }
}
//输出:
#java.util.Map<java.lang.String, com.reflect.User>
class java.lang.String
class com.reflect.User
#java.util.List<com.reflect.User>
class com.reflect.User
class java.lang.String
class com.reflect.User

反射操作注解(获得注解的信息)

//练习反射操作注解
public class Test08 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        //获得类
        Class c1 = Class.forName("com.reflect.Student2");
        //通过反射获得注解
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }
        //获得注解的value值
        Tablesxp tablesxp = (Tablesxp)c1.getAnnotation(Tablesxp.class);
        tablesxp.value();
        //获得类指定的注解
        Field f = c1.getDeclaredField("name");
        Fieldsxp annotation = f.getAnnotation(Fieldsxp.class);
        System.out.println(annotation.columnName());
        System.out.println(annotation.type());
        System.out.println(annotation.length());
    }
}
@Tablesxp("db_student")
class Student2{
    @Fieldsxp(columnName = "db_id",type = "int",length = 10)
    private int id;
    @Fieldsxp(columnName = "db_age",type = "int",length = 10)
    private int age;
    @Fieldsxp(columnName = "db_name",type = "varchar",length = 3)
    private String name;

    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 + '\'' +
                '}';
    }
}
//创建注解
//作用在哪里
@Target(ElementType.TYPE)//类
//在什么时候获取
@Retention(RetentionPolicy.RUNTIME)//运行时
@interface Tablesxp{
    String value();
}
//属性的注解
//作用在哪里
@Target(ElementType.FIELD)//属性
//在什么时候获取
@Retention(RetentionPolicy.RUNTIME)//运行时
@interface Fieldsxp{
    String columnName();
    String type();
    int length();

}
//输出:
@com.reflect.Tablesxp(value=db_student)
db_name
varchar
3
posted @ 2022-02-22 09:52  史小鹏  阅读(68)  评论(0编辑  收藏  举报