Skip to content

Commit 22719c5

Browse files
author
Paul Sandoz
committedJan 31, 2024
Test break statements within try statements
1 parent 21864db commit 22719c5

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed
 

‎src/java.base/share/classes/java/lang/reflect/code/op/ExtendedOps.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2248,7 +2248,7 @@ public Block.Builder lower(Block.Builder b, OpTransformer opT) {
22482248

22492249
boolean ifExitFromTry(JavaLabelOp lop) {
22502250
Op target = lop.target();
2251-
return ifAncestorOp(target, this);
2251+
return target == this || ifAncestorOp(target, this);
22522252
}
22532253

22542254
static boolean ifAncestorOp(Op ancestor, Op op) {

‎test/jdk/java/lang/reflect/code/TestTryFinallyNested.java

+77
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.testng.Assert;
3030
import org.testng.annotations.Test;
3131

32+
import java.lang.reflect.code.analysis.SSA;
3233
import java.lang.reflect.code.op.CoreOps;
3334
import java.lang.reflect.code.Op;
3435
import java.lang.reflect.code.interpreter.Interpreter;
@@ -119,6 +120,82 @@ public void testCatchFinally() {
119120
}
120121

121122

123+
@CodeReflection
124+
public static void tryCatchFinallyBreak(IntConsumer c, int i) {
125+
a: try {
126+
try {
127+
if (i == 0) {
128+
break a;
129+
}
130+
c.accept(0);
131+
} catch (IllegalStateException e) {
132+
if (i == 1) {
133+
break a;
134+
}
135+
c.accept(1);
136+
} finally {
137+
if (i == 2) {
138+
break a;
139+
}
140+
c.accept(2);
141+
}
142+
if (i == 3) {
143+
break a;
144+
}
145+
c.accept(3);
146+
} catch (IllegalStateException e) {
147+
if (i == 4) {
148+
break a;
149+
}
150+
c.accept(4);
151+
} finally {
152+
if (i == 5) {
153+
break a;
154+
}
155+
c.accept(5);
156+
}
157+
c.accept(6);
158+
}
159+
160+
@Test
161+
public void testCatchFinallyBreak() {
162+
CoreOps.FuncOp f = getFuncOp("tryCatchFinallyBreak");
163+
164+
f.writeTo(System.out);
165+
166+
CoreOps.FuncOp lf = f.transform((block, op) -> {
167+
if (op instanceof Op.Lowerable lop) {
168+
return lop.lower(block);
169+
} else {
170+
block.op(op);
171+
return block;
172+
}
173+
});
174+
175+
lf.writeTo(System.out);
176+
177+
for (int ra = -1; ra < 6; ra++) {
178+
int fra = ra;
179+
180+
Consumer<IntConsumer> test = testConsumer(
181+
c -> Interpreter.invoke(MethodHandles.lookup(), lf, c, fra),
182+
c -> tryCatchFinallyBreak(c, fra)
183+
);
184+
185+
test.accept(i -> {});
186+
for (int ea = 0; ea < 6; ea++) {
187+
int fea = ea;
188+
test.accept(i -> {
189+
if (i == fea) throw new IllegalStateException();
190+
});
191+
test.accept(i -> {
192+
if (i == fea) throw new RuntimeException();
193+
});
194+
}
195+
}
196+
}
197+
198+
122199
@CodeReflection
123200
public static void tryForLoop(IntConsumer c) {
124201
for (int i = 0; i < 8; i++) {

0 commit comments

Comments
 (0)