Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2144,7 +2144,7 @@ protected int diffCase(JCCase oldT, JCCase newT, int[] bounds) {
if (oldT.guard != null && newT.guard != null) {
int[] guardBounds = getBounds(oldT.guard);
copyTo(localPointer, guardBounds[0]);
diffTree(oldT.guard, newT.guard, guardBounds);
localPointer = diffTree(oldT.guard, newT.guard, guardBounds);
} else if (oldT.guard != null && newT.guard == null) {
//TODO:
} else if (oldT.guard == null && newT.guard != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,59 @@ public Void visitSwitch(SwitchTree node, Void p) {
assertEquals(golden, res);
}

// test as advised by lahodaj
public void testGuardPreserved() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
String test
= """
public class Test {
void m(Object o) {
switch (o) {
case String str when str.isEmpty() -> System.err.println();
default -> {}
}
}
}
""";
String golden
= """
public class Test {
void m(Object o) {
switch (o) {
case String str when str.isEmpty() -> {
}
default -> {}
}
}
}
""";
TestUtilities.copyStringToFile(testFile, test.replace("|", ""));
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(final WorkingCopy copy) throws IOException {
if (copy.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) {
return;
}
final TreeMaker make = copy.getTreeMaker();
new ErrorAwareTreePathScanner<Void, Void>() {
@Override
public Void visitCase(CaseTree node, Void p) {
if (!node.getLabels().isEmpty()) {
copy.rewrite(getCurrentPath().getLeaf(),
make.CasePatterns(node.getLabels(), node.getGuard(), make.Block(List.of(), false)));
}
return super.visitCase(node, p);
}
}.scan(copy.getCompilationUnit(), null);
}
};
src.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
//System.err.println(res);
assertEquals(golden, res);
}


// XXX I don't understand what these are used for
@Override
String getSourcePckg() {
Expand Down
Loading