反射
反射的泛型操作
Java采用泛型擦除的机制来引入泛型,Java中的泛型仅仅是给编译器javac使用的,确保数据的安全性和免去强制类型转换问题,但是一旦编译完成,所有和泛型有关的类型全部擦除
@SuppressWarnings ( "all" )
public class Demo11 {
public static void test01 ( Map < String , Users > map, List < Users > list) {
System . out. println ( "test01" ) ;
}
public static Map < String , Users > test02 ( ) {
System . out. println ( "test02" ) ;
return null ;
}
public static void main ( String [ ] args) throws NoSuchMethodException {
Method method01 = Demo11 . class . getMethod ( "test01" , Map . class , List . class ) ;
Type [ ] genericParameterTypes = method01. 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 method02 = Demo11 . class . getMethod ( "test02" ) ;
Type genericReturnType = method02. getGenericReturnType ( ) ;
System . out. println ( genericReturnType) ;
if ( genericReturnType instanceof ParameterizedType ) {
Type [ ] actualTypeArguments = ( ( ParameterizedType ) genericReturnType) . getActualTypeArguments ( ) ;
for ( Type actualTypeArgument : actualTypeArguments) {
System . out. println ( actualTypeArgument) ;
}
}
}
}
获取注解信息
练习ORM
ORM(Object Relationship Mapping)–>对象关系映射 把Java类及属性映射到数据库中
类名---->表名 属性---->字段 对象---->记录 利用注解和反射完成类和表结构的映射关系
public class Demo12 {
public static void main ( String [ ] args) throws NoSuchFieldException , IllegalAccessException , InstantiationException {
Class c1 = Student1 . class ;
Annotation [ ] annotations = c1. getAnnotations ( ) ;
for ( Annotation annotation : annotations) {
System . out. println ( annotation) ;
}
MyTable myTable= ( MyTable ) c1. getAnnotation ( MyTable . class ) ;
String value = myTable. value ( ) ;
System . out. println ( value) ;
Field id = c1. getDeclaredField ( "id" ) ;
MyField myField = ( MyField ) id. getAnnotation ( MyField . class ) ;
System . out. println ( myField. columnName ( ) ) ;
System . out. println ( myField. type ( ) ) ;
System . out. println ( myField. length ( ) ) ;
}
}
@MyTable ( "db_student" )
class Student1 {
@MyField ( columnName = "db_id" , type = "int" , length = 10 )
private int id;
@MyField ( columnName = "db_name" , type = "varchar" , length = 20 )
private String name;
@MyField ( columnName = "db_age" , type = "int" , length = 10 )
private int age;
public Student1 ( int id, String name, int age) {
this . id = id;
this . name = name;
this . age = age;
}
public Student1 ( ) {
}
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
@Override
public String toString ( ) {
return "Student1{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}' ;
}
}
@Target ( ElementType . TYPE)
@Retention ( RetentionPolicy . RUNTIME)
@interface MyTable {
String value ( ) ;
}
@Target ( ElementType . FIELD)
@Retention ( RetentionPolicy . RUNTIME)
@interface MyField {
String columnName ( ) ;
String type ( ) ;
int length ( ) ;
}