Class TypeOperations

java.lang.Object
io.micronaut.sourcegen.model.TypeOperations

@Internal public final class TypeOperations extends Object
The operations on the types of the model that the hierarchy walk, override resolution and the generators share: unwrapping annotations, raw types, erasure, substitution, walking the variables a type names, expanding chains of bounds, and a type as one of its supertypes.

Each operation is defined once here, so that the places reading a type agree on what it erases to or which variables it names.

Since:
2.3
  • Method Details

    • unwrap

      public static TypeDef unwrap(TypeDef type)
      Unwraps an annotated type, at any depth.
      Parameters:
      type - The type
      Returns:
      The type without its annotations
    • rawClass

      public static ClassTypeDef rawClass(ClassTypeDef type)
      The class of a parameterized type: List of List<String>. A member of a parameterized type keeps the enclosing type it carries.
      Parameters:
      type - The type
      Returns:
      The type without its type arguments
    • rawClassOf

      public static TypeDef rawClassOf(TypeDef type)
      A type without its annotations, and a class type without its type arguments, as rawClass(ClassTypeDef) takes them: List of @A List<String>. Any other type is only unwrapped.
      Parameters:
      type - The type
      Returns:
      The unwrapped type, raw where it is a class type
    • declaredClass

      public static ClassTypeDef declaredClass(ClassTypeDef type)
      The class a type is of, as it is declared: without its type arguments, and without the enclosing type a member of a parameterized type carries - Member of Outer<String>.Member<Integer>.
      Parameters:
      type - The type
      Returns:
      The declared class
    • raw

      public static TypeDef raw(TypeDef type)
      The raw type of a parameterized type, or of the component of an array of one: List[] of List<String>[].
      Parameters:
      type - The type
      Returns:
      The raw type, or the type itself where it has no type arguments
    • erase

      public static TypeDef erase(TypeDef type)
      Erases a type whose variables carry their bounds: a variable is the erasure of its leftmost bound (JLS 4.6), and Object without one.
      Parameters:
      type - The type
      Returns:
      The erased type
      See Also:
    • erase

      public static TypeDef erase(TypeDef type, List<TypeDef.TypeVariable> methodVariables, List<TypeDef.TypeVariable> typeVariables)
      Erases a type in the scope of the variables a method and its type declare: a variable named without its bounds erases to the bound its nearest declaration gives it - the method's before the type's.
      Parameters:
      type - The type
      methodVariables - The variables the method declares
      typeVariables - The variables the type declares
      Returns:
      The erased type
    • erase

      public static TypeDef erase(TypeDef type, Function<String, @Nullable TypeDef> declared, @Nullable ClassTypeDef thisType)
      Erases a type (JLS 4.6). A variable is the erasure of its leftmost bound - Object for T extends Object & Comparable<T>, as the bytecode writers describe it - taken from the reference where it carries its bounds, else from its declaration in scope. A variable bounded by itself through others - A extends B and B extends A - erases to Object. A parameterized type is its class, without the enclosing type a member carries; a wildcard is the erasure of its upper bound; and an array keeps its rank with its component erased.
      Parameters:
      type - The type
      declared - The leftmost bound of a variable in scope by its name, or null where it declares none
      thisType - The type TypeDef.THIS stands for, or null to keep it
      Returns:
      The erased type
    • erase

      public static Class<?> erase(Type type)
      Erases a reflective type: a variable is the erasure of its leftmost bound, a parameterized type its raw class.
      Parameters:
      type - The type
      Returns:
      The erased class, Object for a type that has no class of its own
    • declaredBounds

      public static List<TypeDef> declaredBounds(String name, List<TypeDef.TypeVariable> methodVariables, List<TypeDef.TypeVariable> typeVariables)
      The bounds the nearest declaration of a variable in scope gives it: the method's variable of the name shadows the type's.
      Parameters:
      name - The name of the variable
      methodVariables - The variables the method declares
      typeVariables - The variables the type declares
      Returns:
      The bounds, empty where no variable of the name is declared or it has none
    • typeVariablesOf

      public static List<TypeDef.TypeVariable> typeVariablesOf(@Nullable ObjectDef objectDef)
      The type variables a definition declares: those of a class, an interface or a record.
      Parameters:
      objectDef - The definition, or null
      Returns:
      The variables, empty for any other definition
    • substitute

      public static TypeDef substitute(TypeDef type, Map<String,TypeDef> substitution)
      Substitutes type variables by name, at any depth of the type: in the type arguments, the component of an array, the bounds of a wildcard, and the enclosing type of a member. A nullable variable is substituted by the nullable replacement.
      Parameters:
      type - The type
      substitution - The types to substitute for the variables, by their names
      Returns:
      The substituted type, without its annotations
    • mentionsVariable

      public static boolean mentionsVariable(TypeDef type, Predicate<String> matches)
      Whether a type names a type variable: itself, or in its type arguments, the component of an array, the bounds of a wildcard, or the enclosing type of a member - Outer<X>.Member names X.
      Parameters:
      type - The type
      matches - Tells the names of the variables looked for
      Returns:
      true where a variable of a matching name is named
    • containsVariableOtherThan

      public static boolean containsVariableOtherThan(TypeDef type, Set<String> variables)
      Whether a type names a type variable that is not one of the given ones.
      Parameters:
      type - The type
      variables - The allowed variable names
      Returns:
      true if another variable is named
    • variablesOf

      public static Set<String> variablesOf(TypeDef type)
      The names of the variables a type names, in the order they are named.
      Parameters:
      type - The type
      Returns:
      The names, each once
    • expandBounds

      public static List<TypeDef> expandBounds(TypeDef.TypeVariable variable, Function<TypeDef.TypeVariable, @Nullable TypeDef.TypeVariable> declaration)
      Expands the bounds of a variable: each bound that is itself a variable in scope is replaced by that variable's bounds, along a chain of any length - U extends V with V extends Number is bounded by Number. A variable reached again along the chain ends it.
      Parameters:
      variable - The variable
      declaration - The variable in scope a reference stands for, with the bounds it is declared with, or null for one that is not expanded, and is a bound itself
      Returns:
      The bounds that are not expanded, as they are declared, in declaration order
    • asSupertype

      public static @Nullable ClassTypeDef asSupertype(ClassTypeDef type, String supertypeName, @Nullable TypeHierarchy.Lookup lookup)
      A type as one of its supertypes, with the type arguments it inherits that supertype with - from the model, reflection or annotation-processing type of each on the way. A raw type inherits its supertypes raw (JLS 4.8).
      Parameters:
      type - The type
      supertypeName - The binary name of the supertype
      lookup - Looks up the definition or the element of a type only known by name, or null
      Returns:
      The supertype, parameterized where it is inherited so, or null where it is not inherited