Class Conversions
java.lang.Object
io.micronaut.sourcegen.bytecode.core.Conversions
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordA boxing of a primitive: thevalueOfof its wrapper.static final recordAcheckcastof a reference.static enumWhich checkcasts of a conversion are left out as redundant.static final recordA conversion of a primitive to another:i2l,l2ifollowed byi2b, and so on.static interfaceA step of a conversion, which a writer emits as its instructions.static final recordAn unboxing: a checkcast of the reference to the owner, which the method is invoked on, returning the primitive the owner holds, and a conversion of that primitive to the target, where it is another. -
Method Summary
Modifier and TypeMethodDescriptionstatic ExpressionDefcastOperand(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.static ExpressionDefcastOperand(ExpressionDef.Cast cast, @Nullable ObjectDef current, @Nullable MethodDef enclosing, EnclosingScope enclosingScope) The expression a cast converts, ascastOperand(ExpressionDef.Cast, ObjectDef, MethodDef)finds it, in the enclosing scope of the class being written.static TypeDefconvertedFrom(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 - theCharactera<T> T identity(T)returns for acharargument unboxes as aCharacterto theintrequested.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.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, asplan(TypeDef, TypeDef, ObjectDef, MethodDef, Checkcasts)does, in the enclosing scope of the class being written.static @Nullable ClassTypeDefreturnedType(@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 -Characterof theT get()of aSupplier<Character>- and the type of the arguments for a variable of the method -Characterofidentity('x')for a<T> T identity(T).static Conversions.Unboxingunboxing(String sourceDescriptor, TypeDef.Primitive target) How a reference unboxes to a primitive.
-
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 anObjectis 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 castcurrent- The definition being written, if anyenclosing- 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, ascastOperand(ExpressionDef.Cast, ObjectDef, MethodDef)finds it, in the enclosing scope of the class being written.- Parameters:
cast- The castcurrent- The definition being written, if anyenclosing- The method being written, if anyenclosingScope- 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.- A primitive to another: the
Conversions.PrimitiveConversion. - A primitive to a reference: the
Conversions.Boxof the primitive, then aConversions.CheckCastto the target where the wrapper is not known to be one. To the wrapper of another primitive that is no boolean: theConversions.PrimitiveConversionto that primitive, then itsConversions.Box. - A reference to a primitive: the
Conversions.Unboxingwith the source's wrapper, then theConversions.PrimitiveConversionof the primitive it holds to the target where it is another. - The wrapper of a number or a char to the wrapper of another: the
Conversions.Unboxingwith the source's wrapper, theConversions.PrimitiveConversionand theConversions.Boxof the target's primitive. - A reference to another: a
Conversions.CheckCastto the target where the value is not known to be one.
Which checkcasts are redundant is where the writers differ: the
policykeeps each one's.- Parameters:
from- The type of the valueto- The type to convert it tocurrent- The definition being written, if anyenclosing- The method being written, if anycheckcasts- Which checkcasts to leave out as redundant- Returns:
- The steps, none where the value already is of the type
- Since:
- 2.3
- A primitive to another: the
-
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, asplan(TypeDef, TypeDef, ObjectDef, MethodDef, Checkcasts)does, in the enclosing scope of the class being written.- Parameters:
from- The type of the valueto- The type to convert it tocurrent- The definition being written, if anyenclosing- The method being written, if anycheckcasts- Which checkcasts to leave out as redundantenclosingScope- The enclosing scope of the class being written- Returns:
- The steps, none where the value already is of the type
- Since:
- 2.3
-
unboxing
How a reference unboxes to a primitive. A reference is read as the box of the target: aBoolean, aCharacter, or aNumberfor a numeric target - the numeric wrappers' own methods, which any number converts with. ACharacteris noNumber: it unboxes with its own method, and the char it holds is converted to the target, as javac convertsCharactertoint:charValue()widened. A numeric wrapper read as acharunboxes to the primitive it holds, which is converted to achar.- Parameters:
sourceDescriptor- The erased descriptor of the referencetarget- 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 - theCharactera<T> T identity(T)returns for acharargument unboxes as aCharacterto theintrequested.- Parameters:
declared- The erased return type the method is invoked withrequested- The type the call requestsowner- The type the method is invoked onmethod- The invoked methoddescriptor- The descriptor the call is written withvalues- The argumentscurrent- 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 -Characterof theT get()of aSupplier<Character>- and the type of the arguments for a variable of the method -Characterofidentity('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 onmethod- The invoked methoddescriptor- The descriptor the call is written withvalues- The argumentscurrent- The definition being written, if any- Returns:
- The type, or
nullwhere it is not known or the method declares no variable as its return type
-