Class OverrideResolver

java.lang.Object
io.micronaut.sourcegen.generator.OverrideResolver

@Internal public final class OverrideResolver extends Object
Resolves the signature a declared method takes as source when it overrides a generic inherited method with that method's erased signature: the inherited parameter and return types with the type arguments of the supertype substituted.

A model written for bytecode overrides with the erased form, which the verifier accepts, such as Object get() for Supplier<String>; source only overrides with the substituted one. How much of it depends on the language: Java accepts erased parameters and a raw type for a parameterized one, and needs the substitution where an erasure changes or a return type is a type variable; Kotlin has no raw types and matches parameters exactly, so it needs the whole substituted signature.

Since:
2.3
  • Method Details

    • resolve

      public static @Nullable OverrideResolver.OverriddenMethod resolve(@Nullable ObjectDef objectDef, MethodDef methodDef, GenerationScope scope)
      Resolves the substituted signature of an erased override for Java source.
      Parameters:
      objectDef - The declaring definition
      methodDef - The declared method
      scope - The scope of the file being written, which looks up the supertypes only known by name
      Returns:
      The substituted signature, or null when Java source can override with the declared one or a type variable of the supertype cannot be resolved
    • resolve

      public static @Nullable OverrideResolver.OverriddenMethod resolve(@Nullable ObjectDef objectDef, MethodDef methodDef, GenerationScope scope, boolean exact)
      Resolves the substituted signature of an erased override.
      Parameters:
      objectDef - The declaring definition
      methodDef - The declared method
      scope - The scope of the file being written, which looks up the supertypes only known by name
      exact - Whether the source language overrides with the substituted types only, as Kotlin does, rather than accepting their erasures and raw types as Java does
      Returns:
      The substituted signature, or null when the source can override with the declared one or a type variable of the supertype cannot be resolved
    • recordComponentType

      public static @Nullable TypeDef recordComponentType(RecordDef recordDef, PropertyDef property, GenerationScope scope, boolean exact)
      The type a record component is written with, where the accessor it implies overrides a generic inherited method with the erasure of that method's signature: an Object value of a record implementing T value() of a Source<String> is a String value. Java requires the accessor of a component to return the type of the component, so the accessor is narrowed by narrowing the component.
      Parameters:
      recordDef - The record
      property - The component
      scope - The scope of the file being written
      exact - Whether the source language overrides with the substituted types only
      Returns:
      The type, or null where the component is written as the model declares it
    • emittedSignature

      public static @Nullable OverrideResolver.OverriddenMethod emittedSignature(@Nullable ClassTypeDef owner, @Nullable ObjectDef current, @Nullable MethodDef caller, MethodDef callMethod, GenerationScope scope, boolean exact)
      The signature a generated method is written with, as the receiver of the call sees it: with the type arguments the receiver binds the definition's variables to - `apply(String)` on a `GenericTarget`.
      Parameters:
      owner - The type of the receiver, or null
      current - The definition being written, or null
      caller - The method the call is written in, or null
      callMethod - The invoked method, as the model calls it
      scope - The scope of the file being written
      exact - Whether the source language overrides with the substituted types only
      Returns:
      The signature, or null where the method is written as the model declares it, or its types cannot be expressed where it is called
    • adaptReference

      public static @Nullable OverrideResolver.ReferenceAdaptation adaptReference(@Nullable ClassTypeDef owner, @Nullable ObjectDef current, @Nullable MethodDef caller, MethodReferenceExpression reference, GenerationScope scope, boolean exact)
      How a method reference is written where override resolution narrowed the referenced generated method: as a lambda converting the values the functional interface passes to the narrowed parameter types, and the result to the type the functional interface returns.
      Parameters:
      owner - The type of the receiver, or null
      current - The definition being written, or null
      caller - The method the reference is written in, or null
      reference - The reference
      scope - The scope of the file being written
      exact - Whether the source language overrides with the substituted types only
      Returns:
      The conversions, or null where the reference is written as is
    • inheritedAs

      public static @Nullable TypeDef inheritedAs(TypeDef value, ClassTypeDef.Parameterized bound, GenerationScope scope)
      A value type as the class a parameterized bound is of, with the type arguments it inherits it with - `GenericList extends ArrayList` as a `List`.
      Parameters:
      value - The value type
      bound - The bound
      scope - The scope of the file being written, which looks up the types only known by name
      Returns:
      The type, or null where the value does not inherit the bound's class
    • receiverArguments

      public static Map<String,TypeDef> receiverArguments(@Nullable ClassTypeDef owner, @Nullable ObjectDef current, MethodDef callMethod, GenerationScope scope)
      The type arguments a receiver binds the variables of the class declaring the invoked method with: the receiver's type as that class, with the type arguments it inherits it with - through generated and compiled supertypes alike, and none through a raw one (JLS 4.8) - and those its enclosing types bind, for a member of a parameterized type.
      Parameters:
      owner - The type of the receiver, or null
      current - The definition being written, or null
      callMethod - The invoked method
      scope - The scope of the file being written
      Returns:
      The type arguments by the variables they bind, empty where there are none
    • upperBounds

      public static List<TypeDef> upperBounds(TypeDef type, @Nullable ObjectDef current, @Nullable MethodDef method)
      The bounds of a type variable that are no variables themselves, in declaration order, with the bounds of the variables a bound names in its place - `U extends V` with `V extends Number` is bounded by `Number`.
      Parameters:
      type - The type
      current - The definition being written, or null
      method - The method being written, or null
      Returns:
      The bounds, empty where the type is no variable or an unbounded one