Skip to content

Commit f95768c

Browse files
committed
Attempting to fix codegen for rule switch cases.
1 parent fbf30f0 commit f95768c

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

java/java.source.base/src/org/netbeans/modules/java/source/save/CasualDiff.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,6 +2184,15 @@ protected int diffCase(JCCase oldT, JCCase newT, int[] bounds) {
21842184
int[] bodyBounds = getBounds(oldT.getBody());
21852185
copyTo(localPointer, bodyBounds[0]);
21862186
localPointer = diffTree(oldT.getBody(), newT.getBody(), bodyBounds);
2187+
if (oldT.getBody().getKind() != Kind.BLOCK) {
2188+
tokenSequence.move(localPointer);
2189+
moveToSrcRelevant(tokenSequence, Direction.FORWARD);
2190+
if (tokenSequence.token().id() == JavaTokenId.SEMICOLON && newT.getBody().getKind() == Kind.BLOCK) {
2191+
localPointer = tokenSequence.offset() + tokenSequence.token().length();
2192+
}
2193+
} else if (newT.getBody().getKind() != Kind.BLOCK && newT.getBody().getKind() != Kind.THROW) {
2194+
printer.print(";");
2195+
}
21872196
printer.undent(old);
21882197
} else {
21892198
PositionEstimator est = EstimatorFactory.statements(

java/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/SwitchExpressionTest.java

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,117 @@ public Void visitYield(YieldTree node, Void p) {
246246

247247
}
248248

249+
public void testAnythingToBlock() throws Exception {
250+
String code = """
251+
package test;
252+
public class Test {
253+
private void test(int p) {
254+
var v = switch (p) {
255+
case 1 -> p = 0;
256+
default -> throw IllegalStateException();
257+
}
258+
}
259+
}
260+
""";
261+
String golden = """
262+
package test;
263+
public class Test {
264+
private void test(int p) {
265+
var v = switch (p) {
266+
case 1 -> {
267+
}
268+
default -> {
269+
}
270+
}
271+
}
272+
}
273+
""";
274+
275+
prepareTest("Test", code);
276+
277+
278+
JavaSource js = getJavaSource();
279+
assertNotNull(js);
280+
281+
Task<WorkingCopy> task = new Task<WorkingCopy>() {
282+
283+
public void run(WorkingCopy workingCopy) throws IOException {
284+
workingCopy.toPhase(JavaSource.Phase.RESOLVED);
285+
286+
TreeMaker make = workingCopy.getTreeMaker();
287+
288+
new TreePathScanner<>() {
289+
@Override
290+
public Object visitCase(CaseTree node, Object p) {
291+
workingCopy.rewrite(node, make.CasePatterns(node.getLabels(), make.Block(List.of(), false)));
292+
return super.visitCase(node, p);
293+
}
294+
}.scan(workingCopy.getCompilationUnit(), null);
295+
}
296+
297+
};
298+
299+
js.runModificationTask(task).commit();
300+
String res = TestUtilities.copyFileToString(getTestFile());
301+
//System.err.println(res);
302+
assertEquals(golden, res);
303+
304+
}
305+
306+
public void testBlockToAnything() throws Exception {
307+
String code = """
308+
package test;
309+
public class Test {
310+
private void test(int p) {
311+
var v = switch (p) {
312+
case 1 -> {}
313+
default -> { throw IllegalStateException(); }
314+
}
315+
}
316+
}
317+
""";
318+
String golden = """
319+
package test;
320+
public class Test {
321+
private void test(int p) {
322+
var v = switch (p) {
323+
case 1 -> 0;
324+
default -> throw IllegalStateException();
325+
}
326+
}
327+
}
328+
""";
329+
330+
prepareTest("Test", code);
331+
332+
333+
JavaSource js = getJavaSource();
334+
assertNotNull(js);
335+
336+
Task<WorkingCopy> task = new Task<WorkingCopy>() {
337+
338+
public void run(WorkingCopy workingCopy) throws IOException {
339+
workingCopy.toPhase(JavaSource.Phase.RESOLVED);
340+
341+
TreeMaker make = workingCopy.getTreeMaker();
342+
343+
new TreePathScanner<>() {
344+
@Override
345+
public Object visitCase(CaseTree node, Object p) {
346+
BlockTree bt = (BlockTree) node.getBody();
347+
Tree newBody = bt.getStatements().isEmpty() ? make.Literal(0)
348+
: bt.getStatements().get(0);
349+
workingCopy.rewrite(node, make.CasePatterns(node.getLabels(), newBody));
350+
return super.visitCase(node, p);
351+
}
352+
}.scan(workingCopy.getCompilationUnit(), null);
353+
}
354+
355+
};
356+
357+
js.runModificationTask(task).commit();
358+
String res = TestUtilities.copyFileToString(getTestFile());
359+
//System.err.println(res);
360+
assertEquals(golden, res);
361+
}
249362
}

0 commit comments

Comments
 (0)