注解和反射04(Java)

本文展示了如何使用Java反射来获取方法参数和返回值的泛型信息,以及如何通过反射操作获取类和字段上的注解信息,包括注解的value值。示例代码详细解释了获取TableZ和FieldZ注解的过程。
摘要由CSDN通过智能技术生成

#拓展

获取泛型信息

反射操作泛型:

package 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 Test06{
    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 = Test06.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(actualTypeArguments);
                }
            }
        }
        method = Test06.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);

            }
        }

    }


}

获取注解信息:

了解什么是ORM

Object relationship Mapping ---->对象关系映射

要求:利用注解和反射完成类和表结构的映射关系

package reflection;

import java.lang.annotation.*;
import java.lang.reflect.Field;

//练习反射操作注解
public class Test07 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class c1 = Class.forName("reflection.Student2");

        //通过反射获得注解
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations){
            System.out.println(annotation);
        }

        //获得注解value的值
        TableZ tableZ = (TableZ)c1.getAnnotation(TableZ.class);
        String value = tableZ.value();
        System.out.println(value);

        //获得类指定的注解
        Field f = c1.getDeclaredField("name");
        FieldZ annotation = f.getAnnotation(FieldZ.class);
        System.out.println(annotation.columnName());
        System.out.println(annotation.type());
        System.out.println(annotation.length());
    }
}

@TableZ("db_student")
class Student2{
    @FieldZ(columnName = "db_id",type = "int",length = 10)
    private int id;
    @FieldZ(columnName = "db_age",type = "int",length = 10)
    private int age;
    @FieldZ(columnName = "db_name",type = "varcher",length = 3)
    private String name;

    @Override
    public String toString() {
        return "Student2{" +
                "id=" + id +
                ", age=" + age +
                ", 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;
    }

    public Student2(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }
    public Student2() {
    }

}

//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableZ{
    String value();
}

//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldZ{
    String columnName();
    String type();
    int length();
}

field.getAnnotation是Java中的一个方法,用于获取一个字段的注解。它可以通过反射机制获取指定字段上的注解信息,并返回一个Annotation对象。在使用该方法时,需要传入一个注解类型的Class对象作为参数,以指定要获取的注解类型。如果指定的字段上没有该注解,则返回null。

Class.getAnnotations() 获取所有的注解,包括自己声明的以及继承的
Class.getAnnotation(Class< A > annotationClass) 获取指定的注解,该注解可以是自己声明的,也可以是继承的
Class.getDeclaredAnnotations() 获取自己声明的注解
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值