Class TypeHierarchy

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

@Internal public final class TypeHierarchy extends Object
Walks the supertypes of a definition and the methods they declare, with the type arguments of each supertype substituted - from Sourcegen model, reflection, and annotation-processing types.

Both the bridges a bytecode writer adds and the signatures a source generator overrides with are resolved from this walk. A supertype represented only by a name has no method metadata, so it is skipped unless a lookup of its element is given.

Since:
2.3
  • Method Details

    • visitInheritedMethods

      public static void visitInheritedMethods(ObjectDef objectDef, @Nullable Function<String, @Nullable io.micronaut.inject.ast.ClassElement> classElementLookup, TypeHierarchy.InheritedMethodVisitor visitor)
      Visits the methods of every supertype of a definition, breadth first, each supertype once.
      Parameters:
      objectDef - The definition
      classElementLookup - Looks up the element of a supertype only known by name, or null
      visitor - The visitor
    • visitInheritedMethods

      public static void visitInheritedMethods(ObjectDef objectDef, @Nullable TypeHierarchy.Lookup lookup, TypeHierarchy.InheritedMethodVisitor visitor)
      Visits the methods of every supertype of a definition, breadth first, each supertype once.
      Parameters:
      objectDef - The definition
      lookup - Looks up the definition or the element of a supertype only known by name
      visitor - The visitor
      Since:
      2.3
    • declaring

      public static TypeHierarchy.InheritedType declaring(ObjectDef objectDef)
      The definition itself as a type of the hierarchy, to erase its own type variables.
      Parameters:
      objectDef - The definition
      Returns:
      The type
    • declaring

      public static TypeHierarchy.InheritedType declaring(ObjectDef objectDef, List<TypeDef.TypeVariable> enclosingVariables)
      The definition itself as a type of the hierarchy, to erase its own type variables and those of the types enclosing it that are in its scope - the variables of the enclosing class of an inner class.
      Parameters:
      objectDef - The definition
      enclosingVariables - The variables of the enclosing types in scope, the nearest first
      Returns:
      The type
      Since:
      2.3
    • methodScope

      public static Map<String,TypeDef> methodScope(List<TypeDef.TypeVariable> variables)
      The variables a method declares as they read in its scope: each with its bounds, Object for one declared without - so that a reference to one by its name alone erases to what the method declares it with, and not to the bound of a variable of the class of the same name - and a bound naming another variable of the method with that variable's bounds.
      Parameters:
      variables - The variables the method declares
      Returns:
      The variables by their names
      Since:
      2.3
    • superTypesOf

      public static List<TypeDef> superTypesOf(ObjectDef objectDef)
      The direct supertypes of a definition.
      Parameters:
      objectDef - The definition
      Returns:
      The superclass, if any, and the superinterfaces
    • unwrap

      public static TypeDef unwrap(TypeDef type)
      Unwraps an annotated type.
      Parameters:
      type - The type
      Returns:
      The type without its annotations
    • containsVariableOtherThan

      public static boolean containsVariableOtherThan(TypeDef type, Set<String> variables)
      Whether a type refers to 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 referenced
    • 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
      Since:
      2.3
    • packageOf

      public static String packageOf(ObjectDef objectDef)
      The package of a definition. A member type is of the package of its outermost type, which the binary name tells: the canonical name of pkg.Outer$Member ends its package at Outer.
      Parameters:
      objectDef - The definition
      Returns:
      The package name
    • erasedName

      public static String erasedName(TypeDef erased)
      The name of an erased type, to compare erasures: the binary name of a class, the name of a primitive, and the component name followed by [] per dimension of an array.
      Parameters:
      erased - A type as returned by TypeHierarchy.InheritedType.erase(TypeDef)
      Returns:
      The name
    • erasedName

      public static String erasedName(TypeDef erased, @Nullable ObjectDef objectDef)
      The name of an erased type in the scope of a definition, which resolves a simple name of the definition or of one of its member types, as binaryName(ClassTypeDef, ObjectDef) does.
      Parameters:
      erased - A type as returned by TypeHierarchy.InheritedType.erase(TypeDef)
      objectDef - The definition, or null
      Returns:
      The name
    • binaryName

      public static String binaryName(ClassTypeDef classTypeDef, @Nullable ObjectDef objectDef)
      Returns the binary name a reference to a type resolves to in the scope of the type being written.

      The model is immutable, so ObjectDefBuilder.addInnerType(ObjectDef) can only qualify the copy of a member type it stores; the definition the caller holds on to keeps the simple name it was built with, and a reference taken from it reads as Inner rather than com.example.Outer$Inner. Java source has scoping that makes such a reference resolve anyway - javac reads Inner inside Outer as its member type - while a class file has none: every name in it is a binary name. This applies the same scoping, so that the generators accept the same definition.

      An unqualified name is resolved against the type being written and the member types it declares, which is the scope a definition knows about. A name that is already qualified, and one that matches nothing in scope, is left as it is.

      Parameters:
      classTypeDef - The referenced type
      objectDef - The contextual object, if any
      Returns:
      The binary name
    • inherits

      public static boolean inherits(ClassTypeDef type, String supertypeName, @Nullable Function<String, @Nullable io.micronaut.inject.ast.ClassElement> classElementLookup)
      Whether a type has the other among its supertypes, from the model, reflection or annotation-processing type of each - so that a type generated in the same round, which cannot be loaded, is known as well.
      Parameters:
      type - The type
      supertypeName - The binary name of the supertype
      classElementLookup - Looks up the element of a type only known by name, or null
      Returns:
      true if the type is the supertype or inherits it
    • inherits

      public static boolean inherits(ClassTypeDef type, String supertypeName, @Nullable TypeHierarchy.Lookup lookup)
      Whether a type has the other among its supertypes, from the model, reflection or annotation-processing type of each - so that a type generated in the same round, which cannot be loaded, is known as well.
      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
      Returns:
      true if the type is the supertype or inherits it
      Since:
      2.3
    • asSupertype

      public static @Nullable ClassTypeDef asSupertype(ClassTypeDef type, String supertypeName, @Nullable Function<String, @Nullable io.micronaut.inject.ast.ClassElement> classElementLookup)
      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.
      Parameters:
      type - The type
      supertypeName - The binary name of the supertype
      classElementLookup - Looks up 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
    • 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
      Returns:
      The supertype, parameterized where it is inherited so, or null where it is not inherited
      Since:
      2.3
    • enclosingOf

      public static @Nullable ClassTypeDef enclosingOf(ClassTypeDef type)
      The enclosing type of a member class, with its type arguments: Outer<String> of Outer<String>.Member. A compiler element names it in its type mirror, a substituted or reflected member carries it.
      Parameters:
      type - The class type, not parameterized: the raw type of a parameterized one
      Returns:
      The enclosing type, or null where the type is no member of a parameterized type
      Since:
      2.3
    • carriedEnclosingOf

      public static @Nullable ClassTypeDef carriedEnclosingOf(ClassTypeDef type)
      The enclosing type a substituted or reflected member carries, without reading that of an element.
      Parameters:
      type - The class type
      Returns:
      The enclosing type it carries, or null
      Since:
      2.3
    • memberClass

      public static ClassTypeDef memberClass(ClassTypeDef type)
      The member class of a type carrying its enclosing type, or the type itself.
      Parameters:
      type - The class type
      Returns:
      The member class
      Since:
      2.3
    • memberType

      public static ClassTypeDef memberType(ClassTypeDef enclosing, ClassTypeDef member)
      A member class named with a parameterized enclosing type, Outer<String>.Member; its own type arguments are those of a ClassTypeDef.Parameterized of it.
      Parameters:
      enclosing - The enclosing type
      member - The member class
      Returns:
      The member type
      Since:
      2.3
    • typeArguments

      public static Map<String,TypeDef> typeArguments(ClassTypeDef type, @Nullable TypeHierarchy.Lookup lookup)
      The type arguments a type binds the variables of its class with, and those its enclosing types bind: T, U of an Outer<Integer>.Inner<String> are Integer and String. A raw type binds none of its own.
      Parameters:
      type - The type
      lookup - Looks up the definition or the element of a type only known by name, or null
      Returns:
      The arguments by the variables they bind
      Since:
      2.3
    • typeDefOf

      public static TypeDef typeDefOf(Type type)
      Converts a reflective type, keeping the type arguments of a parameterized one.
      Parameters:
      type - The type
      Returns:
      The type
    • substituted

      public static TypeDef substituted(TypeDef type, Map<String,TypeDef> substitution)
      Substitutes type variables by name, at any depth of the type.
      Parameters:
      type - The type
      substitution - The types to substitute for the variables
      Returns:
      The substituted type