Example usage for java.lang.reflect ParameterizedType getClass

List of usage examples for java.lang.reflect ParameterizedType getClass

Introduction

In this page you can find the example usage for java.lang.reflect ParameterizedType getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:ca.oson.json.util.ObjectUtil.java

public static <E> Class<E> getTypeClass(java.lang.reflect.Type type) {
    Class cl = type.getClass();//from  ww  w .  j  a  v  a2 s.  c om

    if (ComponentType.class.isAssignableFrom(cl)) {
        ComponentType componentType = (ComponentType) type;
        return componentType.getClassType();
    }

    //java.util.List<ca.oson.json.test.Dataset>
    String className = type.getTypeName();
    try {
        int idx = className.indexOf('<');
        if (idx > 0) {
            className = className.substring(0, idx);
        }

        return (Class<E>) Class.forName(className);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    // Collection<String>, return String.class
    if (ParameterizedType.class.isAssignableFrom(cl)) {
        java.lang.reflect.ParameterizedType pt = (java.lang.reflect.ParameterizedType) type;

        return (Class<E>) pt.getRawType().getClass();

        // GenericArrayType represents an array type whose component
        // type is either a parameterized type or a type variable.
    } else if (java.lang.reflect.GenericArrayType.class.isAssignableFrom(cl)) {
        java.lang.reflect.GenericArrayType pt = (java.lang.reflect.GenericArrayType) type;

        return (Class<E>) pt.getClass();

    } else if (java.lang.reflect.TypeVariable.class.isAssignableFrom(cl)) {
        java.lang.reflect.TypeVariable pt = (java.lang.reflect.TypeVariable) type;

        return (Class<E>) pt.getClass();
    }

    return cl;
}