反射操作泛型
-
Java采用泛型擦除的机制来引入泛型,Java中的泛型仅仅是给编译器javac使用的,确保数据
的安全性和免去强制类型转换问题﹐但是,一旦编译完成,所有和泛型有关的类型全部擦除 -
为了通过反射操作这些类型,Java新增了ParameterizedType , GenericArrayType ,
TypeVariable和WildcardType几种类型来代表不能被归一到Class类中的类型,但是又和原始类型齐名的类型
-
ParameterizedType:表示一种参数化类型,比如Collection
-
GenericArrayType:表示一种元素类型是参数化类型或者类型变量的数组类型
-
TypeVariable:是各种类型变量的公共父接口
-
WildcardType:代表一种通配符类型表达式
package com.kuang.reflection;
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(name:"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( name: "test02",..parameterTypes: null);
Type genericReturnType = method.getGenericReturnType() ;
if (genericReturnType instanceof ParameterizedType){
Type[] actualTypeArguments = ((ParameterizedType)genericReturnType).getActualTypeArguments();
for(Type actualTypeArgument:actualTypeArguments){
system.out.println(actualTypeArgument);
}
}
}
}
反射操作注解
getAnnotations
getAnnotation
练习:0RM
-
了解什么是ORM ?
-
Object relationship Mapping -->对象关系映射
class Student{ int id; String name; int age; }
id name age 001 hlh 3 002 ll 30 - 类和表结构对应
- 属性和字段对应
- 对象和记录对应
要求:利用注解和反射完成类和表结构的映射关系
-
package com. kuang.reflection;
//练习反射操作注解
public class Test12 {
public static void main(String[] args) throws ClassNotFoundException {
class c1 = class.forName ( "com.kuang.reflection.student2");
//通过反射获得注解
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations){
system.out.println(annotation);
}
//获得注解的value的值.
Tablekuang tablekuang = (Tablekuang)c1.getAnnotation(Tablekuang.class);
String value = tablekuang.value();
system.out.println(value);
//获得类指定的注解
Field f = ci.getDeclaredField ( name: "name" );//可以换成id age等
Fieldkuang annotation = f.getAnnotation(Fieldkuang.class);
system.out.println(annotation. columnName());
system.out.println(annotation.type());
System.out.println(annotation.length());
}
}
@Tablekuang("db_student")
class student2{
@Fieldkuang(columnNmae="db_id",type="int",length=10)
private int id;
@Fieldkuang(columnNmae="db_age",type="int",length=10)
private int age;
@Fieldkuang(columnNmae="db_id",type="varchar",length=10)
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 "student2i" +
"id=" + id +
",age=" + age +
", name='" + name + '\'' +
'}';
}
}
//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Tablekuang{
String value();
}
//属性的注解
@Target(ElementType.Field)
@Retention(RetentionPolicy.RUNTIME)
@interface Fieldkuang{
String columnName();
String type();
int length();
}