注解和反射04(Java)

#拓展

获取泛型信息

反射操作泛型:

注解和反射04(Java)_第1张图片

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 map, List list){
        System.out.println("test01");
    }
    public Map 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);

            }
        }

    }


}

注解和反射04(Java)_第2张图片

获取注解信息:

了解什么是ORM

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

注解和反射04(Java)_第3张图片

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

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();
}

注解和反射04(Java)_第4张图片

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

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

你可能感兴趣的:(java,开发语言)