Class Completion
java.lang.Object
io.micronaut.sourcegen.model.Completion
Whether a statement can complete normally, following JLS 14.22, which every backend asks: where control reaches the
end of a method, of a branch, of a try block or of a switch case decides whether a return, a jump or a finally block
is written after it, and whether a source compiler accepts what follows.
The rules every backend shares:
- A return or a throw never completes; a statement the rules below do not name always does.
- An if/else completes when either branch does; an if without an else always does.
- A switch completes without a default, or when the default or one of its cases does.
Where the backends answer differently, each one is a setting of this class:
- How a block completes: Java source takes a block that stops at any statement as stopping, as javac does; the bytecode writers and Kotlin take the last statement.
- The constant conditions: Java source takes a
whileloop over a constant expression of the value true as never completing - the model has no break - because javac's reachability depends on it, which needs Java's constant folding. The bytecode writers and Kotlin take every loop as completing. - The catches of a try: Java source counts the catches javac takes as reachable alone; the others count every catch.
- A try with a finally block: Java source and the bytecode writers analyze it - a finally that cannot complete ends the try, and one that can completes as the try and its catches do. Kotlin takes it as completing.
- A synchronized block: Java source and the bytecode writers complete it as its body does. Kotlin writes it as a
call of
synchronized, which it takes as completing.
Being wrong in the "cannot complete" direction is not just a compile or verifier error: an if/else then-branch judged as non-completing gets no jump over the else-branch and falls through into it, and a method judged as returning gets no implicit return.
- Since:
- 2.3
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final CompletionAs the bytecode writers analyze a statement: the last statement of a block, and no constant condition.static final CompletionAs the Kotlin generator analyzes a statement: the last statement of a block, no constant condition, and a try with a finally block and a synchronized block taken as completing. -
Method Summary
Modifier and TypeMethodDescriptionbooleancanCompleteNormally(StatementDef statement) booleancannotCompleteNormally(StatementDef statement) static CompletionjavaSource(Predicate<ExpressionDef> constantTrue, Function<StatementDef.Try, List<StatementDef.Try.Catch>> reachableCatches) The analysis of Java source, as javac decides reachability: a block stops at any statement that cannot complete, a loop over a constant expression of the value true never completes, and a try completes as the catches javac takes as reachable do.
-
Field Details
-
BYTECODE
As the bytecode writers analyze a statement: the last statement of a block, and no constant condition. -
KOTLIN
As the Kotlin generator analyzes a statement: the last statement of a block, no constant condition, and a try with a finally block and a synchronized block taken as completing.
-
-
Method Details
-
javaSource
public static Completion javaSource(Predicate<ExpressionDef> constantTrue, Function<StatementDef.Try, List<StatementDef.Try.Catch>> reachableCatches) The analysis of Java source, as javac decides reachability: a block stops at any statement that cannot complete, a loop over a constant expression of the value true never completes, and a try completes as the catches javac takes as reachable do.- Parameters:
constantTrue- Whether a loop condition is a constant expression of the value true, as Java folds itreachableCatches- The catches of a try that javac takes as reachable- Returns:
- The analysis
-
canCompleteNormally
- Parameters:
statement- The statement- Returns:
trueif control can reach the end of the statement
-
cannotCompleteNormally
- Parameters:
statement- The statement- Returns:
trueif control cannot reach the end of the statement
-