Java 注解和反射(六)获取泛型,注解信息
反射操作泛型
**Java采用泛型擦出的机制来引入泛型,Java中的泛型仅仅是给编译器javac使用的,确保数据
的安全性和免除强制类型转换问题,但是,一旦编译完成,所有和泛型有关的类型全部擦除
**为了通过反射操作这些类型,Java新增了ParameterizedType,GenericArrayType,TypeVariable
和WildcardType几种类型来代表不能被归一到Class类中的类型但是又和原始类型齐名的类型
**ParmeterizedType:表示一种参数化类型,比如Collection<String>
**GenericArrayType:表示一种元素类型是参数化类型或者类型变量的数组类型
**TypeVarible:是各种类型变量的公共父接口
**WildcardType:代表一种通配符类型表达式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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 --> 对象关系映射
类和表结构对应
属性和字段对应
对象和记录对应
要求:利用注解和反射完成类和表结构的映射关系
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | //练习反射操作注解 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(); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧