Common

Date Handing

Reflection

IO

HTTP Request

Cryptography

Misc.

Types

The Types class provides methods for retrieving generic type information, converting types, and handling parameterized types. These methods facilitate efficient and convenient manipulation of Java types at runtime.

type2class(Type) handles all standard reflection Type implementations. Type variables resolve through their single upper bound, upper-bounded and unbounded wildcards resolve through their single upper bound, and generic arrays resolve after their component type. A lower-bounded wildcard or multiple upper bounds cannot identify one unique class and therefore cause IllegalArgumentException.

Methods

1. getActualType(Type type)

Retrieves the actual type arguments of a parameterized type.

Example:

Type type = new ParameterizedType() {
    @Override
    public Type[] getActualTypeArguments() {
        return new Type[]{String.class};
    }

    @Override
    public Type getRawType() {
        return List.class;
    }

    @Override
    public Type getOwnerType() {
        return null;
    }
};
Type[] actualType = Types.getActualType(type);
// actualType will contain String.class

2. getGenericReturnType(Method method)

Retrieves the actual type arguments of a method's return type.

Example:

Method method = TestTypes.class.getMethod("getList2");
Type[] actualType = Types.getGenericReturnType(method);
// actualType will contain the generic return type of the method

3. getGenericFirstReturnType(Method method)

Retrieves the first actual type argument of a method's return type and converts it to a Class.

Example:

Method method = TestTypes.class.getMethod("getList2");
Class<?> actualType = Types.getGenericFirstReturnType(method);
// actualType will be the first generic return type of the method as a Class

4. getActualType(Class<?> clz)

Retrieves the actual type arguments of a class's superclass.

Example:

Type[] actualType = Types.getActualType(ArrayList.class);
// actualType will contain the generic type arguments of ArrayList's superclass

5. getActualClass(Class<?> clz)

Retrieves the first actual type argument of a class's superclass and converts it to a Class.

Example:

class StringList extends ArrayList<String> {
}

Class<?> actualClass = Types.getActualClass(StringList.class);
// String.class

Types.getActualClass(String.class);
// throws IllegalArgumentException instead of failing with NullPointerException

6. type2class(Type type)

Converts a Type to a Class according to these rules:

Example:

Type type = String.class;
Class<?> actualClass = Types.type2class(type);
// actualClass will be String.class

class Example {
    List<String> names;
}

Type listType = Example.class.getDeclaredField("names").getGenericType();
Class<?> rawClass = Types.type2class(listType);
// rawClass will be List.class for a field declared as List<String>