Java注解和反射05:获取泛型和注解


反射操作泛型

import java.lang.reflect.*;
import java.util.List;
import java.util.Map;

public class Main {

    public static void main(String[] args) throws NoSuchMethodException {

        /**
         * 获得泛型参数的方法对象
         * getGenericParameterTypes()方法得到泛型的参数化类型
         * getGenericReturnType()方法得到泛型的返回值类型
         */
        Method method = Main.class.getDeclaredMethod("test", Map.class, List.class);

        Type[] genericParameterTypes = method.getGenericParameterTypes();

        for (Type genericParameterType : genericParameterTypes) {

            /**
             * getActualTypeArguments()方法进一步获得详细的参数化类型
             */
            if (genericParameterType instanceof ParameterizedType){

                Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();

                for (Type actualTypeArgument : actualTypeArguments) {
                    System.out.println(actualTypeArgument);
                }
            }
        }

        System.out.println();

        Type genericReturnType = method.getGenericReturnType();

        if (genericReturnType instanceof ParameterizedType){

            Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();

            for (Type actualTypeArgument : actualTypeArguments) {
                System.out.println(actualTypeArgument);
            }
        }
    }

    public Map test(Map map, List list){

        System.out.println("test1");
        return null;
    }
}

反射操作注解

ORM

Object Relationship Mapping,对象关系映射

将类和数据库进行映射对应

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

public class Main {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {

        Class<?> c1 = Class.forName("Person");

        /**
         * getAnnotations()方法通过反射获得所有类的注解
         */
        Annotation[] annotations = c1.getAnnotations();

        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }

        /**
         * getAnnotation()方法和value()方法获得指定注解的value值
         *
         */
        MyClassAnnotation myClassAnnotation = c1.getAnnotation(MyClassAnnotation.class);
        System.out.println(myClassAnnotation.value());

        /**
         * 获得属性的注解值
         */
        Field id = c1.getDeclaredField("name");
        MyFieldAnnotation myFieldAnnotation = id.getAnnotation(MyFieldAnnotation.class);

        System.out.println(myFieldAnnotation.columnName());
        System.out.println(myFieldAnnotation.type());
        System.out.println(myFieldAnnotation.length());
    }
}

@MyClassAnnotation("db_person")
class Person{

    @MyFieldAnnotation(columnName = "db_id", type = "int", length = 10)
    private int id;

    @MyFieldAnnotation(columnName = "db_name", type = "varchar", length = 20)
    private String name;

    @MyFieldAnnotation(columnName = "db_age", type = "int", length = 10)
    private int age;

    public Person(){}
}

/**
 * 自定义类注解
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface MyClassAnnotation{

    String value();
}

/**
 * 自定义属性注解
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface MyFieldAnnotation{

    String columnName();
    String type();
    int length();
}