Class Types

java.lang.Object
io.inverno.mod.base.reflect.Types

public final class Types extends Object

Utility methods for Types manipulation.

Type allows to represent parameterized type at runtime which is not possible with a regular Class due to type erasure. This can be useful especially when one needs to perform reflective operation at runtime.

A TypeBuilder makes it easy to create types, for instance:


 Type listOfStringType = Types.type(List.class).type(String.class).and().build();
 
Since:
1.0
Author:
Jeremy Kuhn
See Also:
  • Constructor Details

    • Types

      public Types()
  • Method Details

    • boxType

      public static Class<?> boxType(Class<?> type) throws IllegalArgumentException

      Returns the boxed type corresponding to the specified primitive type.

      Parameters:
      type - a primitive type
      Returns:
      a boxed type
      Throws:
      IllegalArgumentException - if the specified type is not a primitive type
    • unboxType

      public static Class<?> unboxType(Class<?> type) throws IllegalArgumentException

      Returns the primitive type corresponding to the specified boxed type.

      Parameters:
      type - a boxed type
      Returns:
      a primitive type
      Throws:
      IllegalArgumentException - if the specified type is not a boxed type
    • type

      public static TypeBuilder type(Class<?> rawType)

      Creates a type builder with the specified raw type.

      Parameters:
      rawType - an erased type
      Returns:
      a type builder
    • arrayType

      public static ArrayTypeBuilder arrayType()

      Creates an array type builder.

      Returns:
      an array type builder
    • toClass

      public static Class<?> toClass(Type type) throws IllegalArgumentException

      Determines the raw type of the specified type.

      Parameters:
      type - a type
      Returns:
      a raw type
      Throws:
      IllegalArgumentException - if it is not possible to determine a raw type from the specified type
    • typeAsMemberOf

      public static Type typeAsMemberOf(Type containing, AccessibleObject accessor) throws IllegalArgumentException

      Returns the type of an accessible object when viewed as a member of, or otherwise directly contained by, a given type.

      Parameters:
      containing - the containing type
      accessor - the accessible object
      Returns:
      a type
      Throws:
      IllegalArgumentException - if the specified accessor is neither a field nor a method or if there are missing type arguments in the containing type
    • typeAsMemberOf

      public static Type typeAsMemberOf(Type containing, Method method)

      Returns the type of a generic method return type when viewed as a member of, or otherwise directly contained by, a given type.

      Parameters:
      containing - the containing type
      method - the method
      Returns:
      a type
      Throws:
      IllegalArgumentException - if there are missing type arguments in the containing type
    • typeAsMemberOf

      public static Type typeAsMemberOf(Type containing, Field field)

      Returns the type of a field when viewed as a member of, or otherwise directly contained by, a given type.

      Parameters:
      containing - the containing type
      field - the field
      Returns:
      a type
      Throws:
      IllegalArgumentException - if there are missing type arguments in the containing type
    • isAssignable

      public static boolean isAssignable(Type source, Type target)

      Determines whether a type can be assignable to another.

      When a type is assignable to another, it can be implicitly casted to the other one following the Java generics rules.

      Parameters:
      source - a source type
      target - a target type
      Returns:
      true if the source type is assignable to the target type