注解和反射(四)反射获取泛型、反射获取注解
反射获取泛型
- Java采用泛型擦除机制来引入泛型,一旦编译完成,所有和泛型有关的类型全部擦除
反射操作泛型所需的方法
-
ParameterizedType
-
GenericArrayType
-
TypeVariable
-
WildcardType
反射获取注解
ORM
- 对象关系映射
- 类和表的对应关系:
- 类和表结构(整个表)对应
- 属性和字段(列名)对应
- 对象和记录(内容)对应
获得注解的方法
getAnnotations()
代码实例
反射操作泛型
public class Test07 {
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 = Test07.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 = Test07.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);
}
}
}
}
#java.util.Map<java.lang.String, com.reflect.User>
class java.lang.String
class com.reflect.User
#java.util.List<com.reflect.User>
class com.reflect.User
class java.lang.String
class com.reflect.User
反射操作注解(获得注解的信息)
public class Test08 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class c1 = Class.forName("com.reflect.Student2");
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
Tablesxp tablesxp = (Tablesxp)c1.getAnnotation(Tablesxp.class);
tablesxp.value();
Field f = c1.getDeclaredField("name");
Fieldsxp annotation = f.getAnnotation(Fieldsxp.class);
System.out.println(annotation.columnName());
System.out.println(annotation.type());
System.out.println(annotation.length());
}
}
@Tablesxp("db_student")
class Student2{
@Fieldsxp(columnName = "db_id",type = "int",length = 10)
private int id;
@Fieldsxp(columnName = "db_age",type = "int",length = 10)
private int age;
@Fieldsxp(columnName = "db_name",type = "varchar",length = 3)
private String name;
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 Tablesxp{
String value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Fieldsxp{
String columnName();
String type();
int length();
}
@com.reflect.Tablesxp(value=db_student)
db_name
varchar
3
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现