Java中反射操作注解与反射操作泛型
反射操作注解
-
getAnnotations
-
getAnnotation
对象关系映射(简称ORM)
-
ORM?
-
Object relationship Mapping
-
类和表结构对应
-
属性和字段对应
-
对象和记录对应
-
-
利用注解和反射完成类和表结构的映射关
代码:
package com.chao.reflection;
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("com.chao.reflection.Student2");
//通过反射获得注解
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
//获得注解的value的值.
Tablechao tablechao = (Tablechao)c1.getAnnotation(Tablechao.class);
String value = tablechao.value();
System.out.println(value);
//获得类指定的注解
Field f = c1.getDeclaredField("id");
Fieldchao annotation = f.getAnnotation(Fieldchao.class);
System.out.println(annotation.columnName());
System.out.println(annotation.type());
System.out.println(annotation.length());
}
}
@Tablechao("db_student")
class Student2{
@Fieldchao(columnName = "db_id",type = "int",length = 10)
private int id;
@Fieldchao(columnName = "db_age",type = "int",length = 10)
private int age;
@Fieldchao(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 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 "Student2{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Tablechao{
String value();
}
//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Fieldchao{
String columnName();
String type();
int length();
}
反射操作泛型
-
Java采用泛型擦除的机制来引入泛型(类加载进去的时候就产生一个class对象),java中的泛型仅仅是给编译器Javac使用的,确保数据的安全性和免去强制类型转换问题,但是,一旦编译完成,所有和泛型有关的类型全部擦除(垃圾回收站)。
-
为了通过反射操作这些类型,Java新增了ParameterizedType,GenericArrayType,TypeVariable和WildcaredType几种类型来代表不能被归一到Class类中的类型但是又和原始类型齐名的类型。
-
ParameterizedType:表示一种参数化类型,比如:Collection
-
GenericArrayType:表示一种元素类型是参数化类型或者类型变量的数组类型
-
TypeVariable:是各种类型变量的公共父接口
-
WildcaredType:代表一种通配符类型表达式
-
代码:
package com.chao.reflection;
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);
}
}
}
}
版权声明:本文为Answer-Chao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。