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