Java范型

import java.lang.reflect.AnnotatedType;
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.stream.Stream;

public class GenericApi {

    /**
     * java.lang.reflect.Type: Type is the common superinterface for all types in the Java programming language.
     * These include raw types, parameterized types, array types, type variables and primitive types.
     * raw types:原始类型,String.class,java.util.AbstractList.class(没有带泛型)
     * parameterized types:泛型参数类型,java.util.AbstractList<E>
     * array types:数组类型
     * type variables:泛型变量,Map<K,V>,K和V就是TypeVariable,如果
     * primitive types: types:原生类型,包括基本数据类型
     * <p>
     * java.lang.Class#getGenericInfo():泛型信息
     * java.lang.Class#getGenericSuperClass():如果类型有泛型信息,返回获取带有泛型参数的父类类型;如果类型没有泛型信息,返回父类类型
     * java.lang.Class#getGenericInterface():泛型接口
     *
     * @param args
     */
    public static void main(String[] args) {
        // primitive types
        Class intClass = int.class;
        // array types
        Class arrayClass = int[].class;
        // raw types
        Class stringClass = String.class;

        ParameterizedType genericClass = (ParameterizedType) ArrayList.class.getGenericSuperclass();
        // parameterized types
        System.out.println(genericClass);
        // raw types
        System.out.println(genericClass.getRawType());
        // type variables
        Stream.of(genericClass.getActualTypeArguments()).forEach(v->{
            TypeVariable v1 = (TypeVariable) v;
            Type[] bounds = v1.getBounds();
            GenericDeclaration genericDeclaration = v1.getGenericDeclaration();
            AnnotatedType[] annotatedBounds = v1.getAnnotatedBounds();
            System.out.println();
        });

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值