Interface Type

All Superinterfaces:
AnnotationTarget
All Known Subinterfaces:
ArrayType, ClassType, ParameterizedType, PrimitiveType, TypeVariable, VoidType, WildcardType

public interface Type extends AnnotationTarget
A type is used in a program source code, but does not have to be declared anywhere.

For example, the int type exists even if it is not declared in any Java source file, while the java.lang.String type is declared by the class of the same name. Array types, such as int[] or String[][], are not declared anywhere either, but their element types may be. A generic class, such as java.util.List, declares a raw type of the same name, but it does not declare the parameterized types, such as List<String>. Parameterized types are created by applying type arguments to generic classes. For example, the List<String> type is created by applying String to List<T>.

Types occur on many places. A field has a type, a method has a return type, a method parameter has a type, even the extends clause in a class declaration contains a type. Occurences of types may be annotated.

Types are:

  • the void pseudo-type
  • a primitive type, such as int
  • a class type, such as String
  • an array type, such as int[] or String[][]
  • a parameterized type, such as List<String>
  • a type variable, such as T when used in a class that declares a type parameter T
  • a wildcard type, such as the type argument in List<? extends Number>
Class types and parameterized types allow obtaining their class declarations.
  • Method Details

    • isDeclaration

      default boolean isDeclaration()
      Description copied from interface: AnnotationTarget
      Returns whether this annotation target is a declaration.
      Specified by:
      isDeclaration in interface AnnotationTarget
      Returns:
      true if this is a declaration, false otherwise
    • isType

      default boolean isType()
      Description copied from interface: AnnotationTarget
      Returns whether this annotation target is a type.
      Specified by:
      isType in interface AnnotationTarget
      Returns:
      true if this is a type, false otherwise
    • asDeclaration

      default DeclarationInfo asDeclaration()
      Description copied from interface: AnnotationTarget
      Returns this annotation target as a declaration.
      Specified by:
      asDeclaration in interface AnnotationTarget
      Returns:
      this declaration, never null
    • asType

      default Type asType()
      Description copied from interface: AnnotationTarget
      Returns this annotation target as a type.
      Specified by:
      asType in interface AnnotationTarget
      Returns:
      this type, never null
    • kind

      Type.Kind kind()
      Returns the kind of this type.
      Returns:
      the kind of this type
    • isVoid

      default boolean isVoid()
      Returns whether this type is the void pseudo-type.
      Returns:
      true if this is void, false otherwise
    • isPrimitive

      default boolean isPrimitive()
      Returns whether this type is a primitive type.
      Returns:
      true if this is a primitive type, false otherwise
    • isClass

      default boolean isClass()
      Returns whether this type is a class type.
      Returns:
      true if this is a class type, false otherwise
    • isArray

      default boolean isArray()
      Returns whether this type is an array type.
      Returns:
      true if this is an array type, false otherwise
    • isParameterizedType

      default boolean isParameterizedType()
      Returns whether this type is a parameterized type.
      Returns:
      true if this is a parameterized type, false otherwise
    • isTypeVariable

      default boolean isTypeVariable()
      Returns whether this type is a type variable. Type variables are also used to represent type parameters in declarations of parameterized types.
      Returns:
      true if this is a primitive type, false otherwise
    • isWildcardType

      default boolean isWildcardType()
      Returns whether this type is a wildcard type.
      Returns:
      true if this is a wildcard type, false otherwise
    • asVoid

      default VoidType asVoid()
      Returns this type as the void pseudo-type.
      Returns:
      this void type, never null
      Throws:
      IllegalStateException - if isVoid() returns false
    • asPrimitive

      default PrimitiveType asPrimitive()
      Returns this type as a primitive type.
      Returns:
      this primitive type, never null
      Throws:
      IllegalStateException - if isPrimitive() returns false
    • asClass

      default ClassType asClass()
      Returns this type as a class type.
      Returns:
      this class type, never null
      Throws:
      IllegalStateException - if isClass() returns false
    • asArray

      default ArrayType asArray()
      Returns this type as an array type.
      Returns:
      this array type, never null
      Throws:
      IllegalStateException - if isArray() returns false
    • asParameterizedType

      default ParameterizedType asParameterizedType()
      Returns this type as a parameterized type.
      Returns:
      this parameterized type, never null
      Throws:
      IllegalStateException - if isParameterizedType() returns false
    • asTypeVariable

      default TypeVariable asTypeVariable()
      Returns this type as a type variable. Type variables are also used to represent type parameters in declarations of parameterized types.
      Returns:
      this type variable, never null
      Throws:
      IllegalStateException - if isTypeVariable() returns false
    • asWildcardType

      default WildcardType asWildcardType()
      Returns this type as a wildcard type.
      Returns:
      this wildcard type, never null
      Throws:
      IllegalStateException - if isWildcardType() returns false