Common

UUID

HTTP Request

Cryptography

Common

Reflection

IO

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.

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.getMethods()[0];
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.getMethods()[0];
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<?> actualClass = Types.getActualClass(ArrayList.class);
// actualClass will be the first generic type argument of ArrayList's superclass as a Class

6. type2class(Type type)

Converts a Type to a Class.

Example:

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