美文网首页Java 杂谈
谈谈Java中的Type类

谈谈Java中的Type类

作者: 曾泽浩 | 来源:发表于2018-10-08 18:34 被阅读31次
    /**
     * 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.
     *
     * @since 1.5
     */
    public interface Type {
        /**
         * Returns a string describing this type, including information
         * about any type parameters.
         *
         * @implSpec The default implementation calls {@code toString}.
         *
         * @return a string describing this type
         * @since 1.8
         */
        default String getTypeName() {
            return toString();
        }
    }
    

    上面是在JDK1.8中Type类的源代码。

    Type是Java语言中所有类型的父接口,包括
    raw types(原始类型,包括类,枚举,接口,注解,数组(但不包括泛型数组)),
    parameterized types(参数化类型,如Set<String>,Map<String,String>,Class<?>),
    array types(泛型数组和参数类型数组,如T[],List<String>[]),
    type variables(类型变量,如T,K,V) and
    primitive types(基本类型,如boolean,char,byte,short,int,long,float,double).

    继承图


    image.png

    每种类型都有对应的类或者接口

    raw types 对应 Class

    parameterized types 对应 ParameterizedType

    array types 对应 GenericArrayType

    type variables 对应 TypeVariable

    primitive types 对应 Class

    还有一种是通配符?
    ? 对应 WildcardType

    下面看一下简单的例子吧

    声明一个枚举类

    package com.type;
    
    public enum EnumTest {
    }
    

    声明一个接口类

    package com.type;
    
    public interface InterfaceTest {
    }
    

    声明一个注解类

    package com.type;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface AnnotationTest {
    }
    
    package com.type;
    
    import java.util.List;
    import java.util.Map;
    
    public class TypeBean<T> {
        private T t;
        private String string;
        private Map<String,String> map;
        private List<T> list;
        private int aint;
        private T[] tArray;
        private int[] intAarry;
        private Integer[] integers;
        private List<String>[] lists;
        private Class<?> clazz;
        private EnumTest enumTest;
        private InterfaceTest interfaceTest;
        private AnnotationTest annotationTest;
    }
    

    测试类

    package com.type;
    
    import java.lang.reflect.*;
    
    public class TypeBeanTest {
        public static void main(String[] args) {
            Field[] declaredFields = TypeBean.class.getDeclaredFields();
            for (int i = 0; i < declaredFields.length; i++) {
                Field declaredField = declaredFields[i];
                Type genericType = declaredField.getGenericType();
                System.out.println("变量名:" + declaredField.getName());
                System.out.println("变量声明的类型:"+genericType.getTypeName());
                if (genericType instanceof Class) {
                    System.out.println("变量类型为Class");
                } else if (genericType instanceof ParameterizedType) {
                    System.out.println("变量类型为ParameterizedType");
                } else if (genericType instanceof TypeVariable) {
                    System.out.println("变量类型为TypeVariable");
                } else if (genericType instanceof GenericArrayType) {
                    System.out.println("变量类型为GenericArrayType");
                } else if (genericType instanceof WildcardType) {
                    System.out.println("变量类型为WildcardType");
                }
                System.out.println();
            }
        }
    }
    

    测试结果

    变量名:t
    变量声明的类型:T
    变量类型为TypeVariable
    
    变量名:string
    变量声明的类型:java.lang.String
    变量类型为Class
    
    变量名:map
    变量声明的类型:java.util.Map<java.lang.String, java.lang.String>
    变量类型为ParameterizedType
    
    变量名:list
    变量声明的类型:java.util.List<T>
    变量类型为ParameterizedType
    
    变量名:aint
    变量声明的类型:int
    变量类型为Class
    
    变量名:tArray
    变量声明的类型:T[]
    变量类型为GenericArrayType
    
    变量名:intAarry
    变量声明的类型:int[]
    变量类型为Class
    
    变量名:integers
    变量声明的类型:java.lang.Integer[]
    变量类型为Class
    
    变量名:lists
    变量声明的类型:java.util.List<java.lang.String>[]
    变量类型为GenericArrayType
    
    变量名:clazz
    变量声明的类型:java.lang.Class<?>
    变量类型为ParameterizedType
    
    变量名:enumTest
    变量声明的类型:com.type.EnumTest
    变量类型为Class
    
    变量名:interfaceTest
    变量声明的类型:com.type.InterfaceTest
    变量类型为Class
    
    变量名:annotationTest
    变量声明的类型:com.type.AnnotationTest
    变量类型为Class
    

    相关文章

      网友评论

        本文标题:谈谈Java中的Type类

        本文链接:https://www.haomeiwen.com/subject/khijaftx.html