Class Conversions

java.lang.Object
io.micronaut.sourcegen.bytecode.core.Conversions

@Internal public final class Conversions extends Object
The conversions both bytecode writers emit, planned once as the steps each writer only emits: a checkcast, an unboxing with the source's wrapper, a primitive conversion and a boxing - plan(TypeDef, TypeDef, ObjectDef, MethodDef, Conversions.Checkcasts).

The unboxing is javac's (JLS 5.1.8): a Character read as an int is charValue() widened, not the intValue() of a Number a Character is not - for a Character parameter, and for the Character a generic method returns where the call requests a primitive.

Since:
2.3
  • Method Details

    • castOperand

      public static ExpressionDef castOperand(ExpressionDef.Cast cast, @Nullable ObjectDef current, @Nullable MethodDef enclosing)
      The expression a cast converts: the last cast of a chain is the one written, as javac writes it. An inner primitive cast of a reference would unbox and rebox it, turning a legitimate null into a NullPointerException; a primitive cast of something that is not an Object is a real conversion and stays, and so does a cast that checks a reference the outer cast does not, as javac writes (Object) (Zeta[]) alphas.
      Parameters:
      cast - The cast
      current - The definition being written, if any
      enclosing - The method being written, if any
      Returns:
      The expression to write and convert to the type of the cast
      Since:
      2.3
    • castOperand

      public static ExpressionDef castOperand(ExpressionDef.Cast cast, @Nullable ObjectDef current, @Nullable MethodDef enclosing, EnclosingScope enclosingScope)
      The expression a cast converts, as castOperand(ExpressionDef.Cast, ObjectDef, MethodDef) finds it, in the enclosing scope of the class being written.
      Parameters:
      cast - The cast
      current - The definition being written, if any
      enclosing - The method being written, if any
      enclosingScope - The enclosing scope of the class being written
      Returns:
      The expression to write and convert to the type of the cast
      Since:
      2.3
    • plan

      public static List<Conversions.Step> plan(TypeDef from, TypeDef to, @Nullable ObjectDef current, @Nullable MethodDef enclosing, Conversions.Checkcasts checkcasts)
      Plans the conversion of a value of one type to another, as the steps a writer emits in order.

      Which checkcasts are redundant is where the writers differ: the policy keeps each one's.

      Parameters:
      from - The type of the value
      to - The type to convert it to
      current - The definition being written, if any
      enclosing - The method being written, if any
      checkcasts - Which checkcasts to leave out as redundant
      Returns:
      The steps, none where the value already is of the type
      Since:
      2.3
    • plan

      public static List<Conversions.Step> plan(TypeDef from, TypeDef to, @Nullable ObjectDef current, @Nullable MethodDef enclosing, Conversions.Checkcasts checkcasts, EnclosingScope enclosingScope)
      Plans the conversion of a value of one type to another, as plan(TypeDef, TypeDef, ObjectDef, MethodDef, Checkcasts) does, in the enclosing scope of the class being written.
      Parameters:
      from - The type of the value
      to - The type to convert it to
      current - The definition being written, if any
      enclosing - The method being written, if any
      checkcasts - Which checkcasts to leave out as redundant
      enclosingScope - The enclosing scope of the class being written
      Returns:
      The steps, none where the value already is of the type
      Since:
      2.3
    • unboxing

      public static Conversions.Unboxing unboxing(String sourceDescriptor, TypeDef.Primitive target)
      How a reference unboxes to a primitive. A reference is read as the box of the target: a Boolean, a Character, or a Number for a numeric target - the numeric wrappers' own methods, which any number converts with. A Character is no Number: it unboxes with its own method, and the char it holds is converted to the target, as javac converts Character to int: charValue() widened. A numeric wrapper read as a char unboxes to the primitive it holds, which is converted to a char.
      Parameters:
      sourceDescriptor - The erased descriptor of the reference
      target - The primitive
      Returns:
      The unboxing
    • convertedFrom

      public static TypeDef convertedFrom(TypeDef declared, TypeDef requested, @Nullable TypeDef owner, MethodDef method, String descriptor, List<? extends ExpressionDef> values, @Nullable ObjectDef current)
      The type to convert the result of a call from, where the call requests another type than its method declares: the declared one, erased, but the wrapper the call returns where it requests a primitive of a generic return - the Character a <T> T identity(T) returns for a char argument unboxes as a Character to the int requested.
      Parameters:
      declared - The erased return type the method is invoked with
      requested - The type the call requests
      owner - The type the method is invoked on
      method - The invoked method
      descriptor - The descriptor the call is written with
      values - The arguments
      current - The definition being written, if any
      Returns:
      The type to convert from
    • returnedType

      public static @Nullable ClassTypeDef returnedType(@Nullable TypeDef owner, MethodDef method, String descriptor, List<? extends ExpressionDef> values, @Nullable ObjectDef current)
      The type the call of a generic method returns where it is written, where the method declares it with a variable: the type argument of the receiver's type for a variable of the class - Character of the T get() of a Supplier<Character> - and the type of the arguments for a variable of the method - Character of identity('x') for a <T> T identity(T). That is how javac unboxes the value a call requesting a primitive returns.
      Parameters:
      owner - The type the method is invoked on
      method - The invoked method
      descriptor - The descriptor the call is written with
      values - The arguments
      current - The definition being written, if any
      Returns:
      The type, or null where it is not known or the method declares no variable as its return type