Skip to content

Commit 161fdcb

Browse files
authored
Merge pull request #8719 from lahodaj/print-switch-expression-with-implicit-yields
javac AST may contain an implicit/expanded yield, ignore it in when pretty printing.
2 parents 84ccfa6 + 98145e8 commit 161fdcb

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

java/java.source.base/src/org/netbeans/modules/java/source/pretty/VeryPretty.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import com.sun.source.doctree.VersionTree;
7474
import com.sun.source.tree.ExpressionTree;
7575
import com.sun.source.tree.SwitchExpressionTree;
76+
import com.sun.source.tree.YieldTree;
7677
import com.sun.source.util.DocTreePathScanner;
7778
import com.sun.source.util.DocTreeScanner;
7879

@@ -1362,16 +1363,20 @@ public void visitCase(JCCase tree) {
13621363
print(tree.getGuard());
13631364
}
13641365
}
1365-
Object caseKind = tree.getCaseKind();
1366-
if (caseKind == null || !String.valueOf(caseKind).equals("RULE")) {
1366+
if (tree.getCaseKind() != CaseTree.CaseKind.RULE) {
13671367
print(':');
13681368
newline();
13691369
indent();
13701370
printStats(tree.stats);
13711371
undent(old);
13721372
} else {
13731373
print(" -> "); //TODO: configure spaces!
1374-
printStat(tree.stats.head);
1374+
if (tree.stats.head.getKind() == Kind.YIELD) {
1375+
print((JCTree) ((YieldTree) tree.stats.head).getValue());
1376+
print(";");
1377+
} else {
1378+
printStat(tree.stats.head);
1379+
}
13751380
undent(old);
13761381
}
13771382
}

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.sun.source.tree.TypeParameterTree;
3535
import com.sun.source.tree.VariableTree;
3636
import com.sun.source.tree.WhileLoopTree;
37+
import com.sun.source.util.TreePath;
3738
import org.netbeans.api.java.source.support.ErrorAwareTreeScanner;
3839
import java.io.File;
3940
import java.io.IOException;
@@ -43,16 +44,19 @@
4344
import java.util.Map;
4445
import java.util.Objects;
4546
import javax.lang.model.element.Modifier;
47+
import javax.lang.model.element.TypeElement;
4648
import javax.lang.model.type.TypeKind;
4749
import org.netbeans.api.java.source.Task;
4850
import org.netbeans.api.java.source.JavaSource;
4951
import org.netbeans.api.java.source.JavaSource.Phase;
5052
import org.netbeans.api.java.source.ModificationResult;
53+
import org.netbeans.api.java.source.SourceUtils;
5154
import org.netbeans.api.java.source.TestUtilities;
5255
import org.netbeans.api.java.source.TreeMaker;
5356
import org.netbeans.api.java.source.WorkingCopy;
5457
import org.netbeans.junit.NbTestSuite;
5558
import org.netbeans.modules.java.ui.FmtOptions;
59+
import org.openide.filesystems.FileUtil;
5660

5761
/**
5862
* Tests method type parameters changes.
@@ -814,6 +818,70 @@ public void run(final WorkingCopy workingCopy) throws IOException {
814818
assertEquals("2", res.substring(dvojkaSpan[0], dvojkaSpan[1]));
815819
}
816820

821+
public void testMoveSwitchExpression() throws Exception {
822+
testFile = new File(getWorkDir(), "Test.java");
823+
File sourceFile = new File(getWorkDir(), "Source.java");
824+
TestUtilities.copyStringToFile(testFile,
825+
"""
826+
package hierbas.del.litoral;
827+
828+
public class Test {
829+
}
830+
""");
831+
TestUtilities.copyStringToFile(sourceFile,
832+
"""
833+
package hierbas.del.litoral;
834+
835+
class Source {
836+
public int taragui(String str) {
837+
return switch (str.length()) {
838+
case 0 -> 0;
839+
default -> 1;
840+
};
841+
}
842+
}
843+
""");
844+
String golden =
845+
"""
846+
package hierbas.del.litoral;
847+
848+
public class Test {
849+
850+
public int taragui(String str) {
851+
return switch (str.length()) {
852+
case 0 -> 0;
853+
default -> 1;
854+
};
855+
}
856+
}
857+
""";
858+
859+
JavaSource src = getJavaSource(testFile);
860+
Task<WorkingCopy> task = new Task<WorkingCopy>() {
861+
862+
public void run(final WorkingCopy workingCopy) throws IOException {
863+
SourceUtils.forceSource(workingCopy, FileUtil.toFileObject(sourceFile));
864+
workingCopy.toPhase(Phase.RESOLVED);
865+
TreeMaker make = workingCopy.getTreeMaker();
866+
CompilationUnitTree cut = workingCopy.getCompilationUnit();
867+
ClassTree main = (ClassTree) cut.getTypeDecls().get(0);
868+
869+
TypeElement sourceEl = workingCopy.getElements().getTypeElement("hierbas.del.litoral.Source");
870+
TreePath sourcePath = workingCopy.getTrees().getPath(sourceEl);
871+
ClassTree source = (ClassTree) sourcePath.getLeaf();
872+
MethodTree method = (MethodTree) source.getMembers().get(1);
873+
workingCopy.rewrite(main, make.removeClassMember(source, method));
874+
workingCopy.rewrite(main, make.addClassMember(main, method));
875+
}
876+
877+
};
878+
ModificationResult mr = src.runModificationTask(task);
879+
mr.commit();
880+
String res = TestUtilities.copyFileToString(testFile);
881+
//System.err.println(res);
882+
assertEquals(golden, res);
883+
}
884+
817885
String getGoldenPckg() {
818886
return "";
819887
}

0 commit comments

Comments
 (0)