|
34 | 34 | import com.sun.source.tree.TypeParameterTree;
|
35 | 35 | import com.sun.source.tree.VariableTree;
|
36 | 36 | import com.sun.source.tree.WhileLoopTree;
|
| 37 | +import com.sun.source.util.TreePath; |
37 | 38 | import org.netbeans.api.java.source.support.ErrorAwareTreeScanner;
|
38 | 39 | import java.io.File;
|
39 | 40 | import java.io.IOException;
|
|
43 | 44 | import java.util.Map;
|
44 | 45 | import java.util.Objects;
|
45 | 46 | import javax.lang.model.element.Modifier;
|
| 47 | +import javax.lang.model.element.TypeElement; |
46 | 48 | import javax.lang.model.type.TypeKind;
|
47 | 49 | import org.netbeans.api.java.source.Task;
|
48 | 50 | import org.netbeans.api.java.source.JavaSource;
|
49 | 51 | import org.netbeans.api.java.source.JavaSource.Phase;
|
50 | 52 | import org.netbeans.api.java.source.ModificationResult;
|
| 53 | +import org.netbeans.api.java.source.SourceUtils; |
51 | 54 | import org.netbeans.api.java.source.TestUtilities;
|
52 | 55 | import org.netbeans.api.java.source.TreeMaker;
|
53 | 56 | import org.netbeans.api.java.source.WorkingCopy;
|
54 | 57 | import org.netbeans.junit.NbTestSuite;
|
55 | 58 | import org.netbeans.modules.java.ui.FmtOptions;
|
| 59 | +import org.openide.filesystems.FileUtil; |
56 | 60 |
|
57 | 61 | /**
|
58 | 62 | * Tests method type parameters changes.
|
@@ -814,6 +818,70 @@ public void run(final WorkingCopy workingCopy) throws IOException {
|
814 | 818 | assertEquals("2", res.substring(dvojkaSpan[0], dvojkaSpan[1]));
|
815 | 819 | }
|
816 | 820 |
|
| 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 | + |
817 | 885 | String getGoldenPckg() {
|
818 | 886 | return "";
|
819 | 887 | }
|
|
0 commit comments