Skip to content

io microsphere reflect JavaType

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

JavaType

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

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

Overview

Represents a Java type, encapsulating various operations and utilities for handling different kinds of types, including classes, parameterized types, type variables, wildcard types, and generic array types.

This class provides the ability to:

- Retrieve the supertype of this type (`#getSuperType()`)
- Access implemented interfaces (`#getInterfaces()`)
- Obtain generic type arguments (`#getGenericTypes()`)
- Get the raw class representation (`#getRawType()`)
- Trace back to the original source type (`#getSource()`)
- Determine if this type is an instance of another type (`#as(Class)`)

Example Usage

`private static HashMap> mapField;

 public void example() throws Throwable {
    JavaType javaType = fromField(getClass(), "mapField");
    javaType.getSuperType();          // AbstractMap>
    javaType.asMap();                 // Map>
    javaType.getGeneric(0).resolve(); // Integer
    javaType.getGeneric(1).resolve(); // List
    javaType.getGeneric(1);           // List
    javaType.resolveGeneric(1, 0);    // String
 `
}

Declaration

public class JavaType implements Serializable

Author: Mercy

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

private static HashMap<Integer, List<String>> mapField;

 public void example() throws Throwable {
    JavaType javaType = fromField(getClass(), "mapField");
    javaType.getSuperType();          // AbstractMap<Integer, List<String>>
    javaType.asMap();                 // Map<Integer, List<String>>
    javaType.getGeneric(0).resolve(); // Integer
    javaType.getGeneric(1).resolve(); // List
    javaType.getGeneric(1);           // List<String>
    javaType.resolveGeneric(1, 0);    // String
 }

Method Examples

getType

private static HashMap<Integer, List<String>> mapField;

 public void example() throws Throwable {
    JavaType javaType = fromField(getClass(), "mapField");
    Type type = javaType.getType(); // Returns the actual Type object, e.g., ParameterizedType
 }

getKind

private static HashMap<Integer, List<String>> mapField;

 public void example() throws Throwable {
    JavaType javaType = fromField(getClass(), "mapField");
    javaType.asMap().getKind();       // returns PARAMETERIZED_TYPE -> Map<Integer, List<String>>
    javaType.getGeneric(0).getKind(); // returns CLASS -> Integer
 }

getSuperType

private static HashMap<Integer, List<String>> mapField;

 public void example() throws Throwable {
    JavaType javaType = fromField(getClass(), "mapField");
    javaType.getSuperType();          // AbstractMap<Integer, List<String>>
 }

getInterfaces

JavaType javaType = JavaType.from(ArrayList.class); // ArrayList<E>
JavaType[] interfaces = javaType.getInterfaces();   // e.g., List<E>, RandomAccess, etc.

getInterface

JavaType javaType = JavaType.from(ArrayList.class); // ArrayList<E>
JavaType listInterface = javaType.getInterface(0);  // List<E>

getGenericTypes

private static HashMap<Integer, List<String>> mapField;

 public void example() throws Throwable {
    JavaType javaType = fromField(getClass(), "mapField");
    javaType.getGenericTypes();       // [Integer, List<String>]
 }

as

private static HashMap<Integer, List<String>> mapField;

 public void example() throws Throwable {
    JavaType javaType = fromField(getClass(), "mapField");
    javaType.as(Map.class);                 // Map<Integer, List<String>
 }

toClass

private static HashMap<Integer, List<String>> mapField;

 public void example() throws Throwable {
    JavaType javaType = fromField(getClass(), "mapField");
    javaType.toClass();          // HashMap
 }

toParameterizedType

private static HashMap<Integer, List<String>> mapField;

 public void example() throws Throwable {
    JavaType javaType = fromField(getClass(), "mapField");
    javaType.toParameterizedType();          // ParameterizedType -> HashMap<Integer, List<String>>
 }

toTypeVariable

public class Example<T> {
    private T value;

    public void exampleMethod() throws Throwable {
        JavaType javaType = JavaType.fromField(Example.class, "value");
        TypeVariable typeVariable = javaType.toTypeVariable(); // Returns TypeVariable for T
    }
}

toWildcardType

public class Example {
    private List<? extends Number> list;

    public void exampleMethod() throws Throwable {
        JavaType javaType = JavaType.fromField(Example.class, "list");
        WildcardType wildcardType = javaType.toWildcardType(); // Returns WildcardType for "? extends Number"
    }
}

toGenericArrayType

public class Example {
    private Integer[] numbers;

    public void exampleMethod() throws Throwable {
        JavaType javaType = JavaType.fromField(Example.class, "numbers");
        GenericArrayType arrayType = javaType.toGenericArrayType(); // Returns GenericArrayType for "Integer[]"
    }
}

isSource

JavaType javaType = JavaType.from(ArrayList.class); // ArrayList<E>
boolean isSourceType = javaType.isSource();         // true (no source)

isRootSource

JavaType javaType = JavaType.from(ArrayList.class); // ArrayList<E>
boolean isRootSource = javaType.isRootSource();     // true (no source)

isClass

JavaType javaType = JavaType.from(String.class);
boolean isClass = javaType.isClass(); // true, because String is a regular class

JavaType mapType = JavaType.from(Map.class);
boolean isMapClass = mapType.isClass(); // true, because Map is a regular class

isParameterizedType

private static HashMap<Integer, List<String>> mapField;

public void example() throws Throwable {
    JavaType javaType = JavaType.fromField(getClass(), "mapField");
    boolean isParameterized = javaType.isParameterizedType(); // true -> HashMap<Integer, List<String>>
}

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.JavaType;

API Reference

Public Methods

Method Description
getType A shared empty array of JavaType, used to avoid unnecessary object creation for methods
getKind Retrieves the Kind of this JavaType, indicating what sort of type it represents.
getSource Retrieves the source JavaType from which this type was derived, if available.
getRootSource Retrieves the root source JavaType from which this type was ultimately derived, if available.
getRawType Retrieves the raw type represented by this JavaType.
getSuperType Retrieves the supertype of this JavaType.
getInterfaces Retrieves the interfaces directly implemented by the class or type represented by this JavaType.
getInterface Retrieves the interface at the specified index from the interfaces directly implemented by this type.
getGenericTypes Retrieves the generic type arguments associated with this JavaType.
getGenericType Retrieves the generic type argument at the specified index from this JavaType.
as Retrieves a JavaType instance representing the specified target class if it is assignable from this type.
toClass Converts this JavaType to a Class if possible.
toParameterizedType Converts this JavaType to a ParameterizedType if possible.
toTypeVariable Converts this JavaType to a TypeVariable if possible.
toWildcardType Converts this JavaType to a WildcardType if possible.
toGenericArrayType Converts this JavaType to a GenericArrayType if possible.
isSource Checks whether this JavaType is the source type.
isRootSource Checks whether this JavaType is the root source type.
isClass Checks if this JavaType represents a regular class or interface.
isParameterizedType Checks if this JavaType represents a parameterized type.

Method Details

getType

public Type getType()

A shared empty array of JavaType, used to avoid unnecessary object creation for methods that return arrays of JavaType instances. / public static final JavaType[] EMPTY_JAVA_TYPE_ARRAY = new JavaType[0];

/** A JavaType instance representing the Object class, which serves as the root class for all Java types. This constant provides a convenient way to reference the most general type in the Java type system. / public static final JavaType OBJECT_JAVA_TYPE = from(Object.class, CLASS);

/** A JavaType instance representing a null value, which is used when a JavaType cannot be determined. This constant provides a convenient way to handle cases where a JavaType is not available. / public static final JavaType NULL_JAVA_TYPE = from(null, UNKNOWN);

private final Type type;

private final transient Kind kind;

private final JavaType source;

// Local cache fields

private volatile JavaType superType;

private volatile JavaType[] interfaces;

private volatile JavaType[] genericTypes;

/** Constructs a new JavaType instance with the specified type.

This constructor initializes the JavaType by determining its kind based on the provided type.

getKind

public Kind getKind()

Retrieves the Kind of this JavaType, indicating what sort of type it represents.

The possible kinds include:

- `Kind#CLASS` for regular classes and interfaces
- `Kind#PARAMETERIZED_TYPE` for parameterized types (e.g., `List`)
- `Kind#TYPE_VARIABLE` for type variables (e.g., `T` in a generic class)
- `Kind#WILDCARD_TYPE` for wildcard types (e.g., `? extends Number`)
- `Kind#GENERIC_ARRAY_TYPE` for generic array types (e.g., `T[]`)
- `Kind#UNKNOWN` if the type cannot be classified

Example Usage

`private static HashMap> mapField;

 public void example() throws Throwable {
    JavaType javaType = fromField(getClass(), "mapField");
    javaType.asMap().getKind();       // returns PARAMETERIZED_TYPE -> Map>
    javaType.getGeneric(0).getKind(); // returns CLASS -> Integer
 `
}

getSource

public JavaType getSource()

Retrieves the source JavaType from which this type was derived, if available.

This method returns the JavaType that served as the origin for this type, typically used when resolving nested or parameterized types to provide context.

getRootSource

public JavaType getRootSource()

Retrieves the root source JavaType from which this type was ultimately derived, if available.

The "root source" refers to the original JavaType at the beginning of a chain of derived types. This method traverses through the source hierarchy until it reaches the topmost non-null source.

getRawType

public Type getRawType()

Retrieves the raw type represented by this JavaType.

The "raw type" refers to the underlying class or interface type before any generic type parameters are applied. For example, for a parameterized type like List, the raw type would be List.

getSuperType

public JavaType getSuperType()

Retrieves the supertype of this JavaType.

This method returns the direct superclass of the type represented by this JavaType instance. The returned JavaType may be null if the type does not have a superclass (e.g., for the Object class).

Example Usage

`private static HashMap> mapField;

 public void example() throws Throwable {
    JavaType javaType = fromField(getClass(), "mapField");
    javaType.getSuperType();          // AbstractMap>
 `
}

getInterfaces

public JavaType[] getInterfaces()

Retrieves the interfaces directly implemented by the class or type represented by this JavaType.

This method returns an array of JavaType objects representing the interfaces that the underlying type directly implements. If the type does not implement any interfaces, or if it represents a primitive or void type, then an empty array is returned.

Example Usage

`JavaType javaType = JavaType.from(ArrayList.class); // ArrayList
JavaType[] interfaces = javaType.getInterfaces();   // e.g., List, RandomAccess, etc.
`

getInterface

public JavaType getInterface(int interfaceIndex)

Retrieves the interface at the specified index from the interfaces directly implemented by this type.

This method provides access to individual interfaces that the underlying type directly implements. The returned JavaType represents the specific interface at the given index in the array of interfaces.

Example Usage

`JavaType javaType = JavaType.from(ArrayList.class); // ArrayList
JavaType listInterface = javaType.getInterface(0);  // List
`

getGenericTypes

public JavaType[] getGenericTypes()

Retrieves the generic type arguments associated with this JavaType.

This method provides access to the generic type parameters that define this type, such as the key and value types in a parameterized type like Map. The returned array contains one element for each generic type argument.

Example Usage

`private static HashMap> mapField;

 public void example() throws Throwable {
    JavaType javaType = fromField(getClass(), "mapField");
    javaType.getGenericTypes();       // [Integer, List]
 `
}

getGenericType

public JavaType getGenericType(int genericTypeIndex)

Retrieves the generic type argument at the specified index from this JavaType.

This method provides access to individual generic type parameters that define this type. For example, in a parameterized type like Map, this method can be used to retrieve either the key type (K) or the value type (V) by index.

as

public JavaType as(Class<?> targetClass)

Retrieves a JavaType instance representing the specified target class if it is assignable from this type.

This method checks whether the provided target class can be assigned from the current type, and returns a corresponding JavaType instance if the assignment is valid. If the target class cannot be assigned, then null is returned.

Example Usage

`private static HashMap> mapField;

 public void example() throws Throwable {
    JavaType javaType = fromField(getClass(), "mapField");
    javaType.as(Map.class);                 // Map
 `
}

toClass

public <T> Class<T> toClass()

Converts this JavaType to a Class if possible.

This method attempts to represent this JavaType as a concrete class. It will return null if the type cannot be resolved to a class, such as when it represents a parameterized type, wildcard type, or generic array type.

Example Usage

`private static HashMap> mapField;

 public void example() throws Throwable {
    JavaType javaType = fromField(getClass(), "mapField");
    javaType.toClass();          // HashMap
 `
}

toParameterizedType

public ParameterizedType toParameterizedType()

Converts this JavaType to a ParameterizedType if possible.

This method attempts to represent this JavaType as a parameterized type, such as List. If the underlying type is not a parameterized type, this method will return null.

Example Usage

`private static HashMap> mapField;

 public void example() throws Throwable {
    JavaType javaType = fromField(getClass(), "mapField");
    javaType.toParameterizedType();          // ParameterizedType -> HashMap>
 `
}

toTypeVariable

public TypeVariable toTypeVariable()

Converts this JavaType to a TypeVariable if possible.

This method attempts to represent this JavaType as a type variable, such as T in a generic class definition. If the underlying type is not a type variable, this method will return null.

Example Usage

`public class Example {
    private T value;

    public void exampleMethod() throws Throwable {
        JavaType javaType = JavaType.fromField(Example.class, "value");
        TypeVariable typeVariable = javaType.toTypeVariable(); // Returns TypeVariable for T
    `
}
}

toWildcardType

public WildcardType toWildcardType()

Converts this JavaType to a WildcardType if possible.

This method attempts to represent this JavaType as a wildcard type, such as ? extends Number or ? super String. If the underlying type is not a wildcard type, this method will return null.

Example Usage

`public class Example {
    private List list;

    public void exampleMethod() throws Throwable {
        JavaType javaType = JavaType.fromField(Example.class, "list");
        WildcardType wildcardType = javaType.toWildcardType(); // Returns WildcardType for "? extends Number"
    `
}
}

toGenericArrayType

public GenericArrayType toGenericArrayType()

Converts this JavaType to a GenericArrayType if possible.

This method attempts to represent this JavaType as a generic array type, such as T[]. If the underlying type is not a generic array type, this method will return null.

Example Usage

`public class Example {
    private Integer[] numbers;

    public void exampleMethod() throws Throwable {
        JavaType javaType = JavaType.fromField(Example.class, "numbers");
        GenericArrayType arrayType = javaType.toGenericArrayType(); // Returns GenericArrayType for "Integer[]"
    `
}
}

isSource

public boolean isSource()

Checks whether this JavaType is the source type.

A source JavaType is the origin of a chain of derived types. This method returns true if this JavaType has no associated source, meaning it's at the top of the derivation hierarchy.

Example Usage

`JavaType javaType = JavaType.from(ArrayList.class); // ArrayList
boolean isSourceType = javaType.isSource();         // true (no source)
`

isRootSource

public boolean isRootSource()

Checks whether this JavaType is the root source type.

A root source JavaType is at the very top of the derivation chain, meaning it has no parent sources. This method returns true if calling #getRootSource() returns null, indicating that this type is not derived from any other type in the hierarchy.

Example Usage

`JavaType javaType = JavaType.from(ArrayList.class); // ArrayList
boolean isRootSource = javaType.isRootSource();     // true (no source)
`

isClass

public boolean isClass()

Checks if this JavaType represents a regular class or interface.

This method returns true if the underlying type is a standard class or interface, as opposed to a parameterized type, type variable, wildcard type, or generic array type.

Example Usage

`JavaType javaType = JavaType.from(String.class);
boolean isClass = javaType.isClass(); // true, because String is a regular class

JavaType mapType = JavaType.from(Map.class);
boolean isMapClass = mapType.isClass(); // true, because Map is a regular class

`

isParameterizedType

public boolean isParameterizedType()

Checks if this JavaType represents a parameterized type.

A parameterized type is a type that has generic type arguments, such as List or Map>. This method returns true if the underlying type is a parameterized type.

Example Usage

`private static HashMap> mapField;

public void example() throws Throwable {
    JavaType javaType = JavaType.fromField(getClass(), "mapField");
    boolean isParameterized = javaType.isParameterizedType(); // true -> HashMap>
`
}

See Also

  • Type
  • Class
  • Field
  • Method

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