Java注解与反射(九)

146 阅读2分钟

这是我参与11月更文挑战的第26天,活动详情查看:2021最后一次更文挑战

  • Parameterized Type : 表示一种参数化类型,比如Collection,String是参数化类型
  • GenericArray Type:表示一种元素类型是参数化类型或者类型变量的数组类型
  • TypeVariable:是各种类型变量的公共父接口
  • WildcardType :代表一种通配符类型表达式
 package refection;
 ​
 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 Test10 {
 ​
     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 = Test10.class.getDeclaredMethod("test01", Map.class, List.class);
 ​
         // 通过反射获取参数信息
         Type []type = method.getGenericParameterTypes();
         for (Type type1 : type) {
             System.out.println("#" + type1);
 ​
             //如果获取到的这个泛型是 ParameterizedType , 那么进行进行强转 , 然后输出
             if(type1 instanceof ParameterizedType){
                 // 整体打印(针对于多个泛型参数的集合的方法而言)
                // System.out.println(type1);
                 
                 // 分别打印(针对于多个泛型参数的集合的方法而言)
                 Type []types = ((ParameterizedType)type1).getActualTypeArguments();
                 for (Type type2 : types) {
                     System.out.println(type2);
                 }
             }
         }
 ​
     }
 ​
 }
 ​

2.12 获取注解信息

练习ORM

 package refection;
 ​
 ​
 import java.lang.annotation.*;
 import java.lang.reflect.Field;
 import java.util.Objects;
 ​
 @SuppressWarnings("all")
 public class Test11 {
     public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
         Class c1 = Class.forName("refection.Student2");
         // 通过反射获得注解
         Annotation[]annotations = c1.getAnnotations();
         for (Annotation annotation : annotations) {
             System.out.println(annotation);
         }
 ​
         // 通过注解获得value的值
         Table table = (Table)c1.getAnnotation(Table.class);
         String value = table.value();
         System.out.println(value);
 ​
         // 获得指定的注解
         Field f = c1.getDeclaredField("name");
         File annotation = f.getAnnotation(File.class);
         System.out.println(annotation.columnName());
     }
 ​
 }
 ​
 ​
 @Table("db_student")
 class Student2{
     @File(columnName = "db_name" , type = "varchar" , length = 3)
         private String name ;
     @File(columnName = "db_id" , type = "int " , length = 10)
         private int id ;
     @File(columnName = "db_age" , type = "int " , length = 10)
         private int age ;
 ​
         public Student2() {
         }
 ​
         public Student2(String name, int id, int age) {
             this.name = name;
             this.id = id;
             this.age = age;
         }
 ​
         public String getName() {
             return name;
         }
 ​
         public void setName(String name) {
             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;
         }
 ​
         @Override
         public String toString() {
             return "Student2{" +
                     "name='" + name + ''' +
                     ", id=" + id +
                     ", age=" + age +
                     '}';
         }
 ​
         @Override
         public boolean equals(Object o) {
             if (this == o) return true;
             if (o == null || getClass() != o.getClass()) return false;
             refection.Student2 student2 = (refection.Student2) o;
             return id == student2.id &&
                     age == student2.age &&
                     Objects.equals(name, student2.name);
         }
 ​
         @Override
         public int hashCode() {
             return Objects.hash(name, id, age);
         }
     }
 ​
 ​
  // 类名的注解
 @Target(ElementType.TYPE)
 @Retention(RetentionPolicy.RUNTIME)
 @interface Table{
     String value();
 }
 ​
 ​
 // 属性的注解
 @Target(ElementType.FIELD)
 @Retention(RetentionPolicy.RUNTIME)
 @interface File{
     String columnName() ;
     String type();
     int length();
 }