Skip to content

io microsphere reflect TypeUtils

github-actions[bot] edited this page Mar 21, 2026 · 1 revision

TypeUtils

Type: Class | Module: microsphere-java-core | Package: io.microsphere.reflect | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/reflect/TypeUtils.java

Overview

The utilities class for Type

Declaration

public abstract class TypeUtils implements Utils

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.1.10-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

isClass

TypeUtils.isClass(String.class); // returns true
TypeUtils.isClass(Integer.TYPE); // returns true (for primitive types)
TypeUtils.isClass(new Object()); // returns false
TypeUtils.isClass(null); // returns false

isObjectClass

TypeUtils.isObjectClass(Object.class); // returns true
TypeUtils.isObjectClass(String.class); // returns false
TypeUtils.isObjectClass(null); // returns false

isObjectType

TypeUtils.isObjectType(Object.class); // returns true
TypeUtils.isObjectType(String.class); // returns false
TypeUtils.isObjectType(null); // returns false

isParameterizedType

TypeUtils.isParameterizedType(List.class); // returns false (raw type)
TypeUtils.isParameterizedType(ArrayList.class); // returns false (raw type)
TypeUtils.isParameterizedType(new ArrayList<String>().getClass().getGenericSuperclass()); // returns true
TypeUtils.isParameterizedType(null); // returns false

isTypeVariable

TypeVariable<?> typeVariable = List.class.getTypeParameters()[0]; // E
TypeUtils.isTypeVariable(typeVariable); // returns true

TypeUtils.isTypeVariable(String.class); // returns false
TypeUtils.isTypeVariable(new Object()); // returns false
TypeUtils.isTypeVariable(null); // returns false

isWildcardType

TypeVariable<?> typeVariable = List.class.getTypeParameters()[0]; // E
WildcardType wildcardType = (WildcardType) typeVariable.getBounds()[0]; // ? extends Object
TypeUtils.isWildcardType(wildcardType); // returns true

TypeUtils.isWildcardType(String.class); // returns false
TypeUtils.isWildcardType(new Object()); // returns false
TypeUtils.isWildcardType(null); // returns false

isGenericArrayType

class Example {
   public <T> T[] valueOf(T... values) {
     return values;
   }
}

Method method = Scratch.class.getMethod("valueOf", Object[].class);
TypeUtils.isGenericArrayType(method.getGenericReturnType()); // returns true
TypeUtils.isGenericArrayType(String[].class); // returns false (array class, not GenericArrayType)
TypeUtils.isGenericArrayType(null); // returns false

isActualType

class Example {
   public <T> T[] valueOf(T... values) {
     return values;
   }
}

TypeUtils.isActualType(Example.class); // returns true (Class)

ParameterizedType listType = (ParameterizedType) new ArrayList<String>().getClass().getGenericSuperclass();
TypeUtils.isActualType(listType); // returns true (ParameterizedType)

TypeVariable<?> typeVar = List.class.getTypeParameters()[0];
TypeUtils.isActualType(typeVar); // returns false (TypeVariable)

WildcardType wildcardType = (WildcardType) typeVar.getBounds()[0];
TypeUtils.isActualType(wildcardType); // returns false (WildcardType)

GenericArrayType arrayType = (GenericArrayType) Example.class.getMethod("valueOf", Object[].class).getGenericReturnType();
TypeUtils.isActualType(arrayType); // returns false (GenericArrayType)

TypeUtils.isActualType(null); // returns false (null value)

getRawType

TypeUtils.getRawType(List.class); // returns List.class

ParameterizedType parameterizedType = (ParameterizedType) new ArrayList<String>().getClass().getGenericSuperclass();
TypeUtils.getRawType(parameterizedType); // returns List.class

TypeUtils.getRawType(String.class); // returns String.class
TypeUtils.getRawType(null); // returns null

getRawClass

TypeUtils.getRawClass(List.class); // returns List.class

ParameterizedType parameterizedType = (ParameterizedType) new ArrayList<String>().getClass().getGenericSuperclass();
TypeUtils.getRawClass(parameterizedType); // returns List.class

TypeUtils.getRawClass(String.class); // returns String.class
TypeUtils.getRawClass(null); // returns null

isAssignableFrom

Type listType = new ArrayList<String>().getClass().getGenericSuperclass(); // ParameterizedType for List<String>
Type superType = List.class;

boolean assignable1 = TypeUtils.isAssignableFrom(superType, listType); // returns true

Type mapType = new HashMap<String, Integer>().getClass().getGenericSuperclass(); // ParameterizedType for AbstractMap<String, Integer>
Type superType2 = Map.class;

boolean assignable2 = TypeUtils.isAssignableFrom(superType2, mapType); // returns true

Type stringType = String.class;
Type integerType = Integer.class;

boolean assignable3 = TypeUtils.isAssignableFrom(stringType, integerType); // returns false

isAssignableFrom

Class<?> superClass = List.class;
Type targetType = new ArrayList<String>().getClass().getGenericSuperclass(); // ParameterizedType for List<String>

boolean isAssignable = TypeUtils.isAssignableFrom(superClass, targetType); // returns true

// Primitive types:
boolean isIntAssignable = TypeUtils.isAssignableFrom(Number.class, Integer.TYPE); // returns true

// Null handling:
boolean isNullAssignable = TypeUtils.isAssignableFrom(Object.class, null); // returns false

resolveActualTypeArguments

public class ExampleClass implements List<String> {
    // implementation details...
}

Type type = ExampleClass.class.getGenericInterfaces()[0]; // ParameterizedType for List<String>
List<Type> args = TypeUtils.resolveActualTypeArguments(type, List.class);
System.out.println(args.get(0)); // prints: java.lang.String

resolveActualTypeArgument

public class ExampleClass implements List<String> {
    // implementation details...
}

Type type = ExampleClass.class.getGenericInterfaces()[0]; // ParameterizedType for List<String>
Type arg = TypeUtils.resolveActualTypeArgument(type, List.class, 0);
System.out.println(arg); // prints: java.lang.String

resolveActualTypeArgumentClasses

public class ExampleClass implements Map<String, Integer> {
    // implementation details...
}

Type type = ExampleClass.class.getGenericInterfaces()[0]; // ParameterizedType for Map<String, Integer>
List<Class> args = TypeUtils.resolveActualTypeArgumentClasses(type, Map.class);
System.out.println(args); // prints: [class java.lang.String, class java.lang.Integer]

resolveActualTypeArgumentClass

public class ExampleClass implements List<String> {
    // implementation details...
}

Type type = ExampleClass.class.getGenericInterfaces()[0]; // ParameterizedType for List<String>
Class<?> argClass = TypeUtils.resolveActualTypeArgumentClass(type, List.class, 0);
System.out.println(argClass); // prints: class java.lang.String

resolveActualTypeArguments

public class ExampleClass implements List<String> {
    // implementation details...
}

Type type = ExampleClass.class.getGenericInterfaces()[0]; // ParameterizedType for List<String>
List<Type> args = TypeUtils.resolveActualTypeArguments(type, List.class);
System.out.println(args.get(0)); // prints: java.lang.String

getAllGenericSuperclasses

class ExampleClass extends HashMap<String, Integer> {}

Type type = ExampleClass.class;
List<Type> superclasses = TypeUtils.getAllGenericSuperclasses(type);

// The list will include:
// - ParameterizedType for java.util.HashMap<java.lang.String, java.lang.Integer>
// - Type for java.util.AbstractMap
// - Type for java.lang.Object

getAllGenericInterfaces

class ExampleClass implements List<String>, Map<String, Integer> {}

Type type = ExampleClass.class;
List<Type> interfaces = TypeUtils.getAllGenericInterfaces(type);

// The list will include:
// - ParameterizedType for java.util.List<java.lang.String>
// - ParameterizedType for java.util.Map<java.lang.String, java.lang.Integer>

getParameterizedTypes

class ExampleClass implements List<String>, Map<String, Integer> {}

Type type = ExampleClass.class;
List<ParameterizedType> result = TypeUtils.getParameterizedTypes(type);

// The list will include:
// - ParameterizedType for java.util.List<java.lang.String>
// - ParameterizedType for java.util.Map<java.lang.String, java.lang.Integer>

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.reflect.TypeUtils;

API Reference

Public Methods

Method Description
isClass The property name for resolved generic types cache size : "microsphere.reflect.resolved-generic-types.cache.size"
isObjectClass Checks if the given class is the Object class.
isObjectType Checks if the given object is the Object type.
isParameterizedType Checks if the given object is an instance of ParameterizedType.
isTypeVariable Checks if the given object is an instance of TypeVariable.
isWildcardType Checks if the given object is an instance of WildcardType.
isGenericArrayType Checks if the given object is an instance of GenericArrayType.
isActualType Checks if the given type is a concrete actual type, which means it is either a Class or a
getRawType Gets the raw type of the specified Type, if it is a ParameterizedType.
getRawClass Gets the raw class of the specified Type, if it is a ParameterizedType or a Class.
isAssignableFrom Determines whether one type can be assigned from another type, similar to the semantics of
isAssignableFrom Checks whether the target type can be assigned to the given super class.
resolveActualTypeArguments the semantics is same as Class#isAssignableFrom(Class)
resolveActualTypeArgument Resolves the actual type argument at the specified index from the given type for a base type.
resolveActualTypeArgumentClasses Resolves the actual type argument classes used in the specified type for a given base type (class or interface).
resolveActualTypeArgumentClass Resolves and returns the actual type argument class at the specified index from the given type for a base type.
resolveActualTypeArguments Resolves the actual type arguments used in the specified type for a given base class.
getAllGenericSuperclasses Retrieves all generic superclasses of the given type, including its hierarchical superclasses.
getAllGenericInterfaces Retrieves all generic interfaces implemented by the given type, including those inherited from its superclasses.
getParameterizedTypes Retrieves the parameterized types directly associated with the specified type,

Method Details

isClass

public static boolean isClass(Object type)

The property name for resolved generic types cache size : "microsphere.reflect.resolved-generic-types.cache.size" / public static final String RESOLVED_GENERIC_TYPES_CACHE_SIZE_PROPERTY_NAME = MICROSPHERE_PROPERTY_NAME_PREFIX + "reflect.resolved-generic-types.cache.size";

/** The default value of resolved generic types cache size : "256" / public static final String DEFAULT_RESOLVED_GENERIC_TYPES_CACHE_SIZE_PROPERTY_VALUE = "256";

/** The default size of resolved generic types cache / public static final int DEFAULT_RESOLVED_GENERIC_TYPES_CACHE_SIZE = parseInt(DEFAULT_RESOLVED_GENERIC_TYPES_CACHE_SIZE_PROPERTY_VALUE);

/** The size of resolved generic types cache /

isObjectClass

public static boolean isObjectClass(Class<?> klass)

Checks if the given class is the Object class.

Example Usage

`TypeUtils.isObjectClass(Object.class); // returns true
TypeUtils.isObjectClass(String.class); // returns false
TypeUtils.isObjectClass(null); // returns false
`

isObjectType

public static boolean isObjectType(Object type)

Checks if the given object is the Object type.

Example Usage

`TypeUtils.isObjectType(Object.class); // returns true
TypeUtils.isObjectType(String.class); // returns false
TypeUtils.isObjectType(null); // returns false
`

isParameterizedType

public static boolean isParameterizedType(Object type)

Checks if the given object is an instance of ParameterizedType.

Example Usage

`TypeUtils.isParameterizedType(List.class); // returns false (raw type)
TypeUtils.isParameterizedType(ArrayList.class); // returns false (raw type)
TypeUtils.isParameterizedType(new ArrayList().getClass().getGenericSuperclass()); // returns true
TypeUtils.isParameterizedType(null); // returns false
`

isTypeVariable

public static boolean isTypeVariable(Object type)

Checks if the given object is an instance of TypeVariable.

Example Usage

`TypeVariable typeVariable = List.class.getTypeParameters()[0]; // E
TypeUtils.isTypeVariable(typeVariable); // returns true

TypeUtils.isTypeVariable(String.class); // returns false
TypeUtils.isTypeVariable(new Object()); // returns false
TypeUtils.isTypeVariable(null); // returns false
`

isWildcardType

public static boolean isWildcardType(Object type)

Checks if the given object is an instance of WildcardType.

Example Usage

`TypeVariable typeVariable = List.class.getTypeParameters()[0]; // E
WildcardType wildcardType = (WildcardType) typeVariable.getBounds()[0]; // ? extends Object
TypeUtils.isWildcardType(wildcardType); // returns true

TypeUtils.isWildcardType(String.class); // returns false
TypeUtils.isWildcardType(new Object()); // returns false
TypeUtils.isWildcardType(null); // returns false
`

isGenericArrayType

public static boolean isGenericArrayType(Object type)

Checks if the given object is an instance of GenericArrayType.

Example Usage

`class Example {
   public  T[] valueOf(T... values) {
     return values;
   `
}

Method method = Scratch.class.getMethod("valueOf", Object[].class);
TypeUtils.isGenericArrayType(method.getGenericReturnType()); // returns true
TypeUtils.isGenericArrayType(String[].class); // returns false (array class, not GenericArrayType)
TypeUtils.isGenericArrayType(null); // returns false
}

isActualType

public static boolean isActualType(Type type)

Checks if the given type is a concrete actual type, which means it is either a Class or a ParameterizedType.

Example Usage

`class Example {
   public  T[] valueOf(T... values) {
     return values;
   `
}

TypeUtils.isActualType(Example.class); // returns true (Class)

ParameterizedType listType = (ParameterizedType) new ArrayList().getClass().getGenericSuperclass();
TypeUtils.isActualType(listType); // returns true (ParameterizedType)

TypeVariable typeVar = List.class.getTypeParameters()[0];
TypeUtils.isActualType(typeVar); // returns false (TypeVariable)

WildcardType wildcardType = (WildcardType) typeVar.getBounds()[0];
TypeUtils.isActualType(wildcardType); // returns false (WildcardType)

GenericArrayType arrayType = (GenericArrayType) Example.class.getMethod("valueOf", Object[].class).getGenericReturnType();
TypeUtils.isActualType(arrayType); // returns false (GenericArrayType)

TypeUtils.isActualType(null); // returns false (null value)
}

getRawType

public static Type getRawType(Type type)

Gets the raw type of the specified Type, if it is a ParameterizedType.

If the given type is a parameterized type (e.g., List), this method returns its raw type (e.g., List). If the type is not a parameterized type, it simply returns the original type.

Example Usage

`TypeUtils.getRawType(List.class); // returns List.class

ParameterizedType parameterizedType = (ParameterizedType) new ArrayList().getClass().getGenericSuperclass();
TypeUtils.getRawType(parameterizedType); // returns List.class

TypeUtils.getRawType(String.class); // returns String.class
TypeUtils.getRawType(null); // returns null
`

getRawClass

public static Class<?> getRawClass(Type type)

Gets the raw class of the specified Type, if it is a ParameterizedType or a Class.

If the given type is a parameterized type (e.g., List), this method returns its raw class (e.g., List.class). If the type is a plain class (e.g., String), it simply returns the same class. If the type does not represent a class, this method returns null.

Example Usage

`TypeUtils.getRawClass(List.class); // returns List.class

ParameterizedType parameterizedType = (ParameterizedType) new ArrayList().getClass().getGenericSuperclass();
TypeUtils.getRawClass(parameterizedType); // returns List.class

TypeUtils.getRawClass(String.class); // returns String.class
TypeUtils.getRawClass(null); // returns null
`

isAssignableFrom

public static boolean isAssignableFrom(Type superType, Type targetType)

Determines whether one type can be assigned from another type, similar to the semantics of Class#isAssignableFrom(Class). This method considers both raw types and parameterized types.

Example Usage

`Type listType = new ArrayList().getClass().getGenericSuperclass(); // ParameterizedType for List
Type superType = List.class;

boolean assignable1 = TypeUtils.isAssignableFrom(superType, listType); // returns true

Type mapType = new HashMap().getClass().getGenericSuperclass(); // ParameterizedType for AbstractMap
Type superType2 = Map.class;

boolean assignable2 = TypeUtils.isAssignableFrom(superType2, mapType); // returns true

Type stringType = String.class;
Type integerType = Integer.class;

boolean assignable3 = TypeUtils.isAssignableFrom(stringType, integerType); // returns false
`

isAssignableFrom

public static boolean isAssignableFrom(Class<?> superClass, Type targetType)

Checks whether the target type can be assigned to the given super class.

This method bridges between raw Class and more complex Type hierarchies, delegating to a more specific implementation that handles type compatibility checks.

Example Usage

`Class superClass = List.class;
Type targetType = new ArrayList().getClass().getGenericSuperclass(); // ParameterizedType for List

boolean isAssignable = TypeUtils.isAssignableFrom(superClass, targetType); // returns true

// Primitive types:
boolean isIntAssignable = TypeUtils.isAssignableFrom(Number.class, Integer.TYPE); // returns true

// Null handling:
boolean isNullAssignable = TypeUtils.isAssignableFrom(Object.class, null); // returns false
`

resolveActualTypeArguments

public static List<Type> resolveActualTypeArguments(Type type, Type baseType)

the semantics is same as Class#isAssignableFrom(Class)

resolveActualTypeArgument

public static Type resolveActualTypeArgument(Type type, Type baseType, int index)

Resolves the actual type argument at the specified index from the given type for a base type.

This method is useful when working with generic types and parameterized types, especially when trying to determine the actual type parameters used in a class hierarchy or interface implementation.

Example Usage

`public class ExampleClass implements List {
    // implementation details...
`

Type type = ExampleClass.class.getGenericInterfaces()[0]; // ParameterizedType for List
Type arg = TypeUtils.resolveActualTypeArgument(type, List.class, 0);
System.out.println(arg); // prints: java.lang.String
}

resolveActualTypeArgumentClasses

public static List<Class> resolveActualTypeArgumentClasses(Type type, Type baseType)

Resolves the actual type argument classes used in the specified type for a given base type (class or interface).

This method resolves generic type information and returns the concrete Class representations of the type arguments. If a type argument cannot be resolved to a class (e.g., it's a wildcard or type variable), it will be omitted from the result.

Example Usage

`public class ExampleClass implements Map {
    // implementation details...
`

Type type = ExampleClass.class.getGenericInterfaces()[0]; // ParameterizedType for Map
List args = TypeUtils.resolveActualTypeArgumentClasses(type, Map.class);
System.out.println(args); // prints: [class java.lang.String, class java.lang.Integer]
}

resolveActualTypeArgumentClass

public static Class resolveActualTypeArgumentClass(Type type, Class baseType, int index)

Resolves and returns the actual type argument class at the specified index from the given type for a base type.

This method resolves generic type information and returns the concrete Class representation of the type argument at the specified index. If the type argument cannot be resolved to a class (e.g., it's a wildcard or type variable), this method will return null.

Example Usage

`public class ExampleClass implements List {
    // implementation details...
`

Type type = ExampleClass.class.getGenericInterfaces()[0]; // ParameterizedType for List
Class argClass = TypeUtils.resolveActualTypeArgumentClass(type, List.class, 0);
System.out.println(argClass); // prints: class java.lang.String
}

resolveActualTypeArguments

public static List<Type> resolveActualTypeArguments(Type type, Class baseClass)

Resolves the actual type arguments used in the specified type for a given base class.

This method is useful when working with generic types and parameterized types, especially when trying to determine the actual type parameters used in a class hierarchy or interface implementation.

Example Usage

`public class ExampleClass implements List {
    // implementation details...
`

Type type = ExampleClass.class.getGenericInterfaces()[0]; // ParameterizedType for List
List args = TypeUtils.resolveActualTypeArguments(type, List.class);
System.out.println(args.get(0)); // prints: java.lang.String
}

In this example, we retrieve the actual type argument used for the List interface implemented by the class.

getAllGenericSuperclasses

public static List<Type> getAllGenericSuperclasses(Type type)

Retrieves all generic superclasses of the given type, including its hierarchical superclasses.

This method is useful when analyzing generic type information for classes that extend other generic classes.

Example Usage

`class ExampleClass extends HashMap {`

Type type = ExampleClass.class;
List superclasses = TypeUtils.getAllGenericSuperclasses(type);

// The list will include:
// - ParameterizedType for java.util.HashMap
// - Type for java.util.AbstractMap
// - Type for java.lang.Object
}

getAllGenericInterfaces

public static List<Type> getAllGenericInterfaces(Type type)

Retrieves all generic interfaces implemented by the given type, including those inherited from its superclasses.

This method is useful when analyzing generic type information for classes that implement generic interfaces.

Example Usage

`class ExampleClass implements List, Map {`

Type type = ExampleClass.class;
List interfaces = TypeUtils.getAllGenericInterfaces(type);

// The list will include:
// - ParameterizedType for java.util.List
// - ParameterizedType for java.util.Map
}

getParameterizedTypes

public static List<ParameterizedType> getParameterizedTypes(Type type)

Retrieves the parameterized types directly associated with the specified type, including the type itself if it is a ParameterizedType, and any interfaces or superclasses that are parameterized.

This method does not include hierarchical types — only the immediate generic information related to the given type is considered.

Example Usage

`class ExampleClass implements List, Map {`

Type type = ExampleClass.class;
List result = TypeUtils.getParameterizedTypes(type);

// The list will include:
// - ParameterizedType for java.util.List
// - ParameterizedType for java.util.Map
}

This documentation was auto-generated from the source code of microsphere-java.

Home

java-annotations

java-core

jdk-tools

lang-model

annotation-processor

java-test

Clone this wiki locally