Skip to content

Commit 3b3cde2

Browse files
committed
Attempting to fix codegen for rule switch cases.
1 parent 1791ca8 commit 3b3cde2

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-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
@@ -2125,6 +2125,15 @@ protected int diffCase(JCCase oldT, JCCase newT, int[] bounds) {
21252125
int[] bodyBounds = getBounds(oldT.getBody());
21262126
copyTo(localPointer, bodyBounds[0]);
21272127
localPointer = diffTree(oldT.getBody(), newT.getBody(), bodyBounds);
2128+
if (oldT.getBody().getKind() != Kind.BLOCK) {
2129+
tokenSequence.move(localPointer);
2130+
moveToSrcRelevant(tokenSequence, Direction.FORWARD);
2131+
if (tokenSequence.token().id() == JavaTokenId.SEMICOLON && newT.getBody().getKind() == Kind.BLOCK) {
2132+
localPointer = tokenSequence.offset() + tokenSequence.token().length();
2133+
}
2134+
} else if (newT.getBody().getKind() != Kind.BLOCK && newT.getBody().getKind() != Kind.THROW) {
2135+
printer.print(";");
2136+
}
21282137
printer.undent(old);
21292138
} else {
21302139
PositionEstimator est = EstimatorFactory.statements(

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

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.sun.source.tree.Tree;
3131
import com.sun.source.tree.Tree.Kind;
3232
import com.sun.source.tree.VariableTree;
33+
import com.sun.source.util.TreePathScanner;
3334
import com.sun.tools.javac.tree.JCTree;
3435
import java.io.IOException;
3536
import java.util.ArrayList;
@@ -222,4 +223,117 @@ public void run(WorkingCopy workingCopy) throws IOException {
222223
js.runModificationTask(task).commit();
223224
}
224225

226+
public void testAnythingToBlock() throws Exception {
227+
String code = """
228+
package test;
229+
public class Test {
230+
private void test(int p) {
231+
var v = switch (p) {
232+
case 1 -> p = 0;
233+
default -> throw IllegalStateException();
234+
}
235+
}
236+
}
237+
""";
238+
String golden = """
239+
package test;
240+
public class Test {
241+
private void test(int p) {
242+
var v = switch (p) {
243+
case 1 -> {
244+
}
245+
default -> {
246+
}
247+
}
248+
}
249+
}
250+
""";
251+
252+
prepareTest("Test", code);
253+
254+
255+
JavaSource js = getJavaSource();
256+
assertNotNull(js);
257+
258+
Task<WorkingCopy> task = new Task<WorkingCopy>() {
259+
260+
public void run(WorkingCopy workingCopy) throws IOException {
261+
workingCopy.toPhase(JavaSource.Phase.RESOLVED);
262+
263+
TreeMaker make = workingCopy.getTreeMaker();
264+
265+
new TreePathScanner<>() {
266+
@Override
267+
public Object visitCase(CaseTree node, Object p) {
268+
workingCopy.rewrite(node, make.CasePatterns(node.getLabels(), make.Block(List.of(), false)));
269+
return super.visitCase(node, p);
270+
}
271+
}.scan(workingCopy.getCompilationUnit(), null);
272+
}
273+
274+
};
275+
276+
js.runModificationTask(task).commit();
277+
String res = TestUtilities.copyFileToString(getTestFile());
278+
//System.err.println(res);
279+
assertEquals(golden, res);
280+
281+
}
282+
public void testBlockToAnything() throws Exception {
283+
String code = """
284+
package test;
285+
public class Test {
286+
private void test(int p) {
287+
var v = switch (p) {
288+
case 1 -> {}
289+
default -> { throw IllegalStateException(); }
290+
}
291+
}
292+
}
293+
""";
294+
String golden = """
295+
package test;
296+
public class Test {
297+
private void test(int p) {
298+
var v = switch (p) {
299+
case 1 -> 0;
300+
default -> throw IllegalStateException();
301+
}
302+
}
303+
}
304+
""";
305+
306+
prepareTest("Test", code);
307+
308+
309+
JavaSource js = getJavaSource();
310+
assertNotNull(js);
311+
312+
Task<WorkingCopy> task = new Task<WorkingCopy>() {
313+
314+
public void run(WorkingCopy workingCopy) throws IOException {
315+
workingCopy.toPhase(JavaSource.Phase.RESOLVED);
316+
317+
TreeMaker make = workingCopy.getTreeMaker();
318+
319+
new TreePathScanner<>() {
320+
@Override
321+
public Object visitCase(CaseTree node, Object p) {
322+
BlockTree bt = (BlockTree) node.getBody();
323+
Tree newBody = bt.getStatements().isEmpty() ? make.Literal(0)
324+
: bt.getStatements().get(0);
325+
workingCopy.rewrite(node, make.CasePatterns(node.getLabels(), newBody));
326+
return super.visitCase(node, p);
327+
}
328+
}.scan(workingCopy.getCompilationUnit(), null);
329+
}
330+
331+
};
332+
333+
js.runModificationTask(task).commit();
334+
String res = TestUtilities.copyFileToString(getTestFile());
335+
//System.err.println(res);
336+
assertEquals(golden, res);
337+
338+
}
225339
}

0 commit comments

Comments
 (0)