|
30 | 30 | import com.sun.source.tree.Tree; |
31 | 31 | import com.sun.source.tree.Tree.Kind; |
32 | 32 | import com.sun.source.tree.VariableTree; |
| 33 | +import com.sun.source.util.TreePathScanner; |
33 | 34 | import com.sun.tools.javac.tree.JCTree; |
34 | 35 | import java.io.IOException; |
35 | 36 | import java.util.ArrayList; |
@@ -222,4 +223,117 @@ public void run(WorkingCopy workingCopy) throws IOException { |
222 | 223 | js.runModificationTask(task).commit(); |
223 | 224 | } |
224 | 225 |
|
| 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 | + } |
225 | 339 | } |
0 commit comments