@@ -246,4 +246,117 @@ public Void visitYield(YieldTree node, Void p) {
246246
247247 }
248248
249+ public void testAnythingToBlock () throws Exception {
250+ String code = """
251+ package test;
252+ public class Test {
253+ private void test(int p) {
254+ var v = switch (p) {
255+ case 1 -> p = 0;
256+ default -> throw IllegalStateException();
257+ }
258+ }
259+ }
260+ """ ;
261+ String golden = """
262+ package test;
263+ public class Test {
264+ private void test(int p) {
265+ var v = switch (p) {
266+ case 1 -> {
267+ }
268+ default -> {
269+ }
270+ }
271+ }
272+ }
273+ """ ;
274+
275+ prepareTest ("Test" , code );
276+
277+
278+ JavaSource js = getJavaSource ();
279+ assertNotNull (js );
280+
281+ Task <WorkingCopy > task = new Task <WorkingCopy >() {
282+
283+ public void run (WorkingCopy workingCopy ) throws IOException {
284+ workingCopy .toPhase (JavaSource .Phase .RESOLVED );
285+
286+ TreeMaker make = workingCopy .getTreeMaker ();
287+
288+ new TreePathScanner <>() {
289+ @ Override
290+ public Object visitCase (CaseTree node , Object p ) {
291+ workingCopy .rewrite (node , make .CasePatterns (node .getLabels (), make .Block (List .of (), false )));
292+ return super .visitCase (node , p );
293+ }
294+ }.scan (workingCopy .getCompilationUnit (), null );
295+ }
296+
297+ };
298+
299+ js .runModificationTask (task ).commit ();
300+ String res = TestUtilities .copyFileToString (getTestFile ());
301+ //System.err.println(res);
302+ assertEquals (golden , res );
303+
304+ }
305+
306+ public void testBlockToAnything () throws Exception {
307+ String code = """
308+ package test;
309+ public class Test {
310+ private void test(int p) {
311+ var v = switch (p) {
312+ case 1 -> {}
313+ default -> { throw IllegalStateException(); }
314+ }
315+ }
316+ }
317+ """ ;
318+ String golden = """
319+ package test;
320+ public class Test {
321+ private void test(int p) {
322+ var v = switch (p) {
323+ case 1 -> 0;
324+ default -> throw IllegalStateException();
325+ }
326+ }
327+ }
328+ """ ;
329+
330+ prepareTest ("Test" , code );
331+
332+
333+ JavaSource js = getJavaSource ();
334+ assertNotNull (js );
335+
336+ Task <WorkingCopy > task = new Task <WorkingCopy >() {
337+
338+ public void run (WorkingCopy workingCopy ) throws IOException {
339+ workingCopy .toPhase (JavaSource .Phase .RESOLVED );
340+
341+ TreeMaker make = workingCopy .getTreeMaker ();
342+
343+ new TreePathScanner <>() {
344+ @ Override
345+ public Object visitCase (CaseTree node , Object p ) {
346+ BlockTree bt = (BlockTree ) node .getBody ();
347+ Tree newBody = bt .getStatements ().isEmpty () ? make .Literal (0 )
348+ : bt .getStatements ().get (0 );
349+ workingCopy .rewrite (node , make .CasePatterns (node .getLabels (), newBody ));
350+ return super .visitCase (node , p );
351+ }
352+ }.scan (workingCopy .getCompilationUnit (), null );
353+ }
354+
355+ };
356+
357+ js .runModificationTask (task ).commit ();
358+ String res = TestUtilities .copyFileToString (getTestFile ());
359+ //System.err.println(res);
360+ assertEquals (golden , res );
361+ }
249362}
0 commit comments