反射操作泛型
**Java采用泛型擦出的机制来引入泛型,Java中的泛型仅仅是给编译器javac使用的,确保数据
的安全性和免除强制类型转换问题,但是,一旦编译完成,所有和泛型有关的类型全部擦除
**为了通过反射操作这些类型,Java新增了ParameterizedType,GenericArrayType,TypeVariable
和WildcardType几种类型来代表不能被归一到Class类中的类型但是又和原始类型齐名的类型
**ParmeterizedType:表示一种参数化类型,比如Collection<String>
**GenericArrayType:表示一种元素类型是参数化类型或者类型变量的数组类型
**TypeVarible:是各种类型变量的公共父接口
**WildcardType:代表一种通配符类型表达式
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.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 = Test10.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); } } } }
反射操作注解
getAnnotations
getAnnotation
练习ORM
了解什么是ORM?
Object relationship Mapping --> 对象关系映射
类和表结构对应
属性和字段对应
对象和记录对应
要求:利用注解和反射完成类和表结构的映射关系
//练习反射操作注解 public class Test11 { public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException { Class c1 = Class.forName("refection.Student"); //通过反射获得注解 Annotation[] annotations = c1.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation); } //获得注解Value的值 TableLuo tableLuo = (TableLuo) c1.getAnnotation(TableLuo.class); String value = tableLuo.value(); System.out.println(value); //获得属性的注解 Field name = c1.getDeclaredField("name"); FieldLuo annotations1 = name.getAnnotation(FieldLuo.class); System.out.println(annotations1.columnName()); System.out.println(annotations1.length()); System.out.println(annotations1.type()); } } @TableLuo("db_student") class Student{ @FieldLuo(columnName = "db_id",type = "int",length = 10) private int id; @FieldLuo(columnName = "db_age",type = "int",length = 10) private int age; @FieldLuo(columnName = "db_name",type = "varchar",length = 3) private String name; public Student() { } public Student(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 "student{" + "id=" + id + ", age=" + age + ", name='" + name + '\'' + '}'; } } //类名注解 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface TableLuo{ String value(); } //属性注解 @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface FieldLuo{ String columnName(); String type(); int length(); }