Skip to content

Commit 91b44c8

Browse files
authored
Fix some pattern instanceof to switch cleanup errors (eclipse-jdt#2072)
- fix PatternInstanceofToSwitchFixCore to create a block for no statements or one statement that is not an expression statement or a throw statement - add new tests to CleanUpTest21 - fixes eclipse-jdt#2066
1 parent f316384 commit 91b44c8

File tree

2 files changed

+192
-1
lines changed

2 files changed

+192
-1
lines changed

org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/PatternInstanceofToSwitchFixCore.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,8 @@ private void addCaseWithStatements(final ASTRewrite rewrite, final ImportRewrite
437437
final boolean isNameUsed,
438438
final List<Statement> innerStatements) {
439439
List<Statement> switchStatements= switchStatement.statements();
440-
boolean needBlock= innerStatements.size() > 1 || (innerStatements.size() == 1 && innerStatements.get(0) instanceof ReturnStatement);
440+
boolean needBlock= innerStatements.size() == 0 || innerStatements.size() > 1 || (innerStatements.size() == 1
441+
&& !(innerStatements.get(0) instanceof ExpressionStatement) && !(innerStatements.get(0) instanceof ThrowStatement));
441442

442443
// Add the case statement(s)
443444
if (caseValueOrNullForDefault != null) {

org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest21.java

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,196 @@ public int foo(Object x, Object y) {
242242
assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 }, null);
243243
}
244244

245+
@Test
246+
public void testPatternInstanceofToSwitch4() throws Exception {
247+
Hashtable<String, String> options= JavaCore.getOptions();
248+
options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.TAB);
249+
JavaCore.setOptions(options);
250+
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
251+
String sample= """
252+
package test1;
253+
254+
public class E {
255+
public int square(int x) {
256+
return x * x;
257+
}
258+
public int foo(Object x, Object y) {
259+
int i, j;
260+
double d;
261+
boolean b;
262+
if (y instanceof Integer xint) {
263+
return 7;
264+
} else if (y instanceof final Double xdouble) {
265+
return square(8); // square
266+
} else if (y instanceof final Boolean xboolean) {
267+
throw new NullPointerException();
268+
} else {
269+
}
270+
}
271+
}
272+
""";
273+
ICompilationUnit cu1= pack1.createCompilationUnit("E.java", sample, false, null);
274+
275+
enable(CleanUpConstants.USE_SWITCH_FOR_INSTANCEOF_PATTERN);
276+
sample= """
277+
package test1;
278+
279+
public class E {
280+
public int square(int x) {
281+
return x * x;
282+
}
283+
public int foo(Object x, Object y) {
284+
int i, j;
285+
double d;
286+
boolean b;
287+
switch (y) {
288+
case Integer xint -> {
289+
return 7;
290+
}
291+
case Double xdouble -> {
292+
return square(8); // square
293+
}
294+
case Boolean xboolean -> throw new NullPointerException();
295+
case null, default -> {
296+
}
297+
}
298+
}
299+
}
300+
""";
301+
String expected1= sample;
302+
303+
assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 }, null);
304+
}
305+
306+
@Test
307+
public void testPatternInstanceofToSwitch5() throws Exception {
308+
Hashtable<String, String> options= JavaCore.getOptions();
309+
options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.TAB);
310+
JavaCore.setOptions(options);
311+
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
312+
String sample= """
313+
package test1;
314+
315+
public class E {
316+
public int square(int x) {
317+
return x * x;
318+
}
319+
public int foo(Object x, Object y) {
320+
int i, j;
321+
double d;
322+
boolean b;
323+
if (y instanceof Integer xint) {
324+
return 7;
325+
} else if (y instanceof final Double xdouble) {
326+
return square(8); // square
327+
} else if (y instanceof final Boolean xboolean) {
328+
throw new NullPointerException();
329+
} else if (obj == null) {
330+
System.out.println("null");
331+
} else {
332+
System.out.println("unknown");
333+
}
334+
}
335+
}
336+
""";
337+
ICompilationUnit cu1= pack1.createCompilationUnit("E.java", sample, false, null);
338+
339+
enable(CleanUpConstants.USE_SWITCH_FOR_INSTANCEOF_PATTERN);
340+
sample= """
341+
package test1;
342+
343+
public class E {
344+
public int square(int x) {
345+
return x * x;
346+
}
347+
public int foo(Object x, Object y) {
348+
int i, j;
349+
double d;
350+
boolean b;
351+
switch (y) {
352+
case Integer xint -> {
353+
return 7;
354+
}
355+
case Double xdouble -> {
356+
return square(8); // square
357+
}
358+
case Boolean xboolean -> throw new NullPointerException();
359+
case null, default -> {
360+
if (obj == null) {
361+
System.out.println("null");
362+
} else {
363+
System.out.println("unknown");
364+
}
365+
}
366+
}
367+
}
368+
}
369+
""";
370+
String expected1= sample;
371+
372+
assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 }, null);
373+
}
374+
375+
@Test
376+
public void testPatternInstanceofToSwitch6() throws Exception {
377+
Hashtable<String, String> options= JavaCore.getOptions();
378+
options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.TAB);
379+
JavaCore.setOptions(options);
380+
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
381+
String sample= """
382+
package test1;
383+
384+
public class E {
385+
public int square(int x) {
386+
return x * x;
387+
}
388+
public int foo(Object x, Object y) {
389+
int i, j;
390+
double d;
391+
boolean b;
392+
if (y instanceof Integer xint) {
393+
return 7;
394+
} else if (y instanceof final Double xdouble) {
395+
return square(8); // square
396+
} else if (y instanceof final Boolean xboolean) {
397+
throw new NullPointerException();
398+
}
399+
}
400+
}
401+
""";
402+
ICompilationUnit cu1= pack1.createCompilationUnit("E.java", sample, false, null);
403+
404+
enable(CleanUpConstants.USE_SWITCH_FOR_INSTANCEOF_PATTERN);
405+
sample= """
406+
package test1;
407+
408+
public class E {
409+
public int square(int x) {
410+
return x * x;
411+
}
412+
public int foo(Object x, Object y) {
413+
int i, j;
414+
double d;
415+
boolean b;
416+
switch (y) {
417+
case Integer xint -> {
418+
return 7;
419+
}
420+
case Double xdouble -> {
421+
return square(8); // square
422+
}
423+
case Boolean xboolean -> throw new NullPointerException();
424+
case null, default -> {
425+
}
426+
}
427+
}
428+
}
429+
""";
430+
String expected1= sample;
431+
432+
assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 }, null);
433+
}
434+
245435
@Test
246436
public void testPatternInstanceofToSwitchExpression1() throws Exception {
247437
Hashtable<String, String> options= JavaCore.getOptions();

0 commit comments

Comments
 (0)