@@ -172,8 +172,8 @@ protected Gen(Context context) {
172
172
Chain switchExpressionFalseChain ;
173
173
List <LocalItem > stackBeforeSwitchExpression ;
174
174
LocalItem switchResult ;
175
- Set < JCMethodInvocation > invocationsWithPatternMatchingCatch = Set . of ();
176
- ListBuffer < int []> patternMatchingInvocationRanges ;
175
+ PatternMatchingCatchConfiguration patternMatchingCatchConfiguration =
176
+ new PatternMatchingCatchConfiguration ( Set . of (), null , null , null ) ;
177
177
178
178
/** Cache the symbol to reflect the qualifying type.
179
179
* key: corresponding type
@@ -1087,21 +1087,31 @@ public void visitBlock(JCBlock tree) {
1087
1087
}
1088
1088
1089
1089
private void visitBlockWithPatterns (JCBlock tree ) {
1090
- Set <JCMethodInvocation > prevInvocationsWithPatternMatchingCatch = invocationsWithPatternMatchingCatch ;
1091
- ListBuffer <int []> prevRanges = patternMatchingInvocationRanges ;
1092
- State startState = code .state .dup ();
1090
+ PatternMatchingCatchConfiguration prevConfiguration = patternMatchingCatchConfiguration ;
1093
1091
try {
1094
- invocationsWithPatternMatchingCatch = tree .patternMatchingCatch .calls2Handle ();
1095
- patternMatchingInvocationRanges = new ListBuffer <>();
1092
+ patternMatchingCatchConfiguration =
1093
+ new PatternMatchingCatchConfiguration (tree .patternMatchingCatch .calls2Handle (),
1094
+ new ListBuffer <int []>(),
1095
+ tree .patternMatchingCatch .handler (),
1096
+ code .state .dup ());
1096
1097
internalVisitBlock (tree );
1097
1098
} finally {
1099
+ generatePatternMatchingCatch (env );
1100
+ patternMatchingCatchConfiguration = prevConfiguration ;
1101
+ }
1102
+ }
1103
+
1104
+ private void generatePatternMatchingCatch (Env <GenContext > env ) {
1105
+ if (patternMatchingCatchConfiguration .handler != null &&
1106
+ !patternMatchingCatchConfiguration .ranges .isEmpty ()) {
1098
1107
Chain skipCatch = code .branch (goto_ );
1099
- JCCatch handler = tree .patternMatchingCatch .handler ();
1100
- code .entryPoint (startState , handler .param .sym .type );
1101
- genPatternMatchingCatch (handler , env , patternMatchingInvocationRanges .toList ());
1108
+ JCCatch handler = patternMatchingCatchConfiguration .handler ();
1109
+ code .entryPoint (patternMatchingCatchConfiguration .startState (),
1110
+ handler .param .sym .type );
1111
+ genPatternMatchingCatch (handler ,
1112
+ env ,
1113
+ patternMatchingCatchConfiguration .ranges .toList ());
1102
1114
code .resolve (skipCatch );
1103
- invocationsWithPatternMatchingCatch = prevInvocationsWithPatternMatchingCatch ;
1104
- patternMatchingInvocationRanges = prevRanges ;
1105
1115
}
1106
1116
}
1107
1117
@@ -1926,12 +1936,26 @@ public void visitApply(JCMethodInvocation tree) {
1926
1936
if (!msym .isDynamic ()) {
1927
1937
code .statBegin (tree .pos );
1928
1938
}
1929
- if (invocationsWithPatternMatchingCatch .contains (tree )) {
1939
+ if (patternMatchingCatchConfiguration . invocations () .contains (tree )) {
1930
1940
int start = code .curCP ();
1931
1941
result = m .invoke ();
1932
- patternMatchingInvocationRanges .add (new int [] {start , code .curCP ()});
1942
+ patternMatchingCatchConfiguration . ranges () .add (new int [] {start , code .curCP ()});
1933
1943
} else {
1934
- result = m .invoke ();
1944
+ if (msym .isConstructor () && TreeInfo .isConstructorCall (tree )) {
1945
+ //if this is a this(...) or super(...) call, there is a pending
1946
+ //"uninitialized this" before this call. One catch handler cannot
1947
+ //handle exceptions that may come from places with "uninitialized this"
1948
+ //and (initialized) this, hence generate one set of handlers here
1949
+ //for the "uninitialized this" case, and another set of handlers
1950
+ //will be generated at the end of the method for the initialized this,
1951
+ //if needed:
1952
+ generatePatternMatchingCatch (env );
1953
+ result = m .invoke ();
1954
+ patternMatchingCatchConfiguration =
1955
+ patternMatchingCatchConfiguration .restart (code .state .dup ());
1956
+ } else {
1957
+ result = m .invoke ();
1958
+ }
1935
1959
}
1936
1960
}
1937
1961
@@ -2555,4 +2579,15 @@ void addCont(Chain c) {
2555
2579
}
2556
2580
}
2557
2581
2582
+ record PatternMatchingCatchConfiguration (Set <JCMethodInvocation > invocations ,
2583
+ ListBuffer <int []> ranges ,
2584
+ JCCatch handler ,
2585
+ State startState ) {
2586
+ public PatternMatchingCatchConfiguration restart (State newState ) {
2587
+ return new PatternMatchingCatchConfiguration (invocations (),
2588
+ new ListBuffer <int []>(),
2589
+ handler (),
2590
+ newState );
2591
+ }
2592
+ }
2558
2593
}
0 commit comments