Java之注解与反射 (3)

Java之注解与反射

反射操作泛型 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 TestFanXingDemo { 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 = TestFanXingDemo.class.getMethod("test01", Map.class, List.class); method.setAccessible(true); //关闭安全检测,暴力反射 Type[] genericParameterTypes = method.getGenericParameterTypes(); //获得泛型的参数类型 for (Type genericParameterType : genericParameterTypes) { System.out.println("#genericParameterType: " + genericParameterType); //遍历泛型类型 if (genericParameterType instanceof ParameterizedType){ //判断泛型参数类型是否是参数化类型 Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments(); //获得真实参数信息 //循环遍历参数 for (Type actualTypeArgument : actualTypeArguments) { System.out.println(actualTypeArgument); } } } } } 反射操作注解

ORM = Object Relationship Mapping ==> 对象关系映射

类和表对应,类的属性和表的字段对应,对象和记录对应

利用注解和反射完成类和表的结构的映射关系

import java.lang.annotation.*; import java.lang.reflect.Field; public class TestDemo12 { //反射操作注解 public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException { Class c1 = Class.forName("com.reflaction.Student2"); //通过反射获得全部注解 Annotation[] annotations = c1.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation); } //获得注解的value值 TableStudent tableStudent = (TableStudent) c1.getAnnotation(TableStudent.class); String value = tableStudent.value();//获取注解的value System.out.println(value); //获取指定类的注解value Field f = c1.getDeclaredField("name"); FieldStudent annotation = f.getAnnotation(FieldStudent.class); System.out.println(annotation.columnName()); System.out.println(annotation.type()); System.out.println(annotation.length()); } } @TableStudent("db_student") class Student2{ @FieldStudent(columnName = "db_id", type = "int", length = 10) private int id; @FieldStudent(columnName = "db_age", type = "int", length = 10) private int age; @FieldStudent(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 int getAge() { return age; } public String getName() { return name; } public void setId(int id) { this.id = id; } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student2{" + "id=" + id + ", age=" + age + ",\'' + '}'; } } //类名的注解 @Target(ElementType.TYPE) //作用范围为类 @Retention(RetentionPolicy.RUNTIME) //作用为运行时可获取 @interface TableStudent{ String value(); } //属性的注解 @Target(ElementType.FIELD) //作用范围为类 @Retention(RetentionPolicy.RUNTIME) //作用为运行时可获取 @interface FieldStudent{ String columnName(); String type(); int length(); } 结尾

参考文章:
https://www.cnblogs.com/nice0e3/p/13498308.html
https://www.cnblogs.com/nice0e3/p/13494147.html

关于Process类、反射操作泛型和注解因为暂时刚需不大且还没研究透,记录的较少,后门会另出文章记录一下。

到此JavaSE部分基本就学完了(除了GUI)后面要开JavaWeb和框架了,冲冲冲!

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgwdpz.html