Skip to content

Commit 7797bff

Browse files
authored
Add support for trailing return or throws for pattern instanceof cleanup (#2148)
- modify PatternInstanceofToSwitchFixCore to recgonize the absence of an else for the last if statement and if the following statement is a return or throws statement, treat it as if it were in an else - add tests to CleanUpTest21 and CleanUpTest22 - fixes #2143
1 parent 27c66d7 commit 7797bff

File tree

3 files changed

+141
-4
lines changed

3 files changed

+141
-4
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ private PatternToSwitchExpressionOperation getOperation(List<IfStatement> ifStat
231231
List<SwitchCaseSection> returnList= new ArrayList<>();
232232
String assignmentName= null;
233233
IVariableBinding assignmentBinding= null;
234+
Statement addedSibling= null;
235+
if (remainingStatement == null) {
236+
IfStatement lastIfStatement= ifStatements.get(ifStatements.size() - 1);
237+
Statement sibling= ASTNodes.getNextSibling(lastIfStatement);
238+
if (sibling instanceof ReturnStatement || sibling instanceof ThrowStatement) {
239+
remainingStatement= sibling;
240+
addedSibling= sibling;
241+
}
242+
}
234243
if (remainingStatement == null || ASTNodes.asList(remainingStatement).size() == 0) {
235244
return null;
236245
}
@@ -322,10 +331,10 @@ private PatternToSwitchExpressionOperation getOperation(List<IfStatement> ifStat
322331
}
323332
if (returnList.size() + throwList.size() == extendedCases.size()) {
324333
return new PatternToSwitchExpressionOperation(ifStatements, switchExpression, extendedCases,
325-
true, null, null);
334+
addedSibling, true, null, null);
326335
} else if (assignmentList.size() == extendedCases.size()) {
327336
return new PatternToSwitchExpressionOperation(ifStatements, switchExpression, extendedCases,
328-
false, assignmentName, assignmentBinding);
337+
addedSibling, false, assignmentName, assignmentBinding);
329338
}
330339
return null;
331340
}
@@ -554,16 +563,18 @@ public static class PatternToSwitchExpressionOperation extends CompilationUnitRe
554563
private List<IfStatement> ifStatements;
555564
private Expression switchExpression;
556565
private List<SwitchCaseSection> cases;
566+
private Statement addedSibling;
557567
private boolean createReturnStatement;
558568
private String varName;
559569
private IVariableBinding assignmentBinding;
560570

561571
public PatternToSwitchExpressionOperation(final List<IfStatement> ifStatements, final Expression switchExpression,
562-
final List<SwitchCaseSection> cases, final boolean createReturnStatement,
572+
final List<SwitchCaseSection> cases, final Statement addedSibling, final boolean createReturnStatement,
563573
final String varName, final IVariableBinding assignmentBinding) {
564574
this.ifStatements= ifStatements;
565575
this.switchExpression= switchExpression;
566576
this.cases= cases;
577+
this.addedSibling= addedSibling;
567578
this.createReturnStatement= createReturnStatement;
568579
this.varName= varName;
569580
this.assignmentBinding= assignmentBinding;
@@ -732,6 +743,9 @@ public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore
732743
rewrite.remove(ifStatements.get(i), group);
733744
}
734745
}
746+
if (addedSibling != null) {
747+
rewrite.remove(addedSibling, group);
748+
}
735749
}
736750

737751
}

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,70 @@ public int foo(Object y) {
695695
assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 }, null);
696696
}
697697

698+
@Test
699+
public void testPatternInstanceofToSwitchExpression5() throws Exception {
700+
Hashtable<String, String> options= JavaCore.getOptions();
701+
options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.TAB);
702+
JavaCore.setOptions(options);
703+
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
704+
String sample= """
705+
package test1;
706+
707+
public class E {
708+
int i;
709+
double d;
710+
boolean b;
711+
712+
public int square(int x) {
713+
return x*x;
714+
}
715+
public int foo(Object y) {
716+
if (y instanceof final Integer xint) {
717+
return xint;
718+
}
719+
if (y instanceof final Double xdouble) {
720+
return square(8); // square
721+
} else if (y instanceof final Boolean xboolean) {
722+
throw new NullPointerException();
723+
} else if (y == null) {
724+
return 7;
725+
}
726+
return 11;
727+
}
728+
}
729+
""";
730+
ICompilationUnit cu1= pack1.createCompilationUnit("E.java", sample, false, null);
731+
732+
enable(CleanUpConstants.USE_SWITCH_FOR_INSTANCEOF_PATTERN);
733+
734+
sample= """
735+
package test1;
736+
737+
public class E {
738+
int i;
739+
double d;
740+
boolean b;
741+
742+
public int square(int x) {
743+
return x*x;
744+
}
745+
public int foo(Object y) {
746+
return switch (y) {
747+
case Integer xint -> xint;
748+
case Double xdouble -> square(8); // square
749+
case Boolean xboolean -> throw new NullPointerException();
750+
case null -> 7;
751+
default -> 11;
752+
};
753+
}
754+
}
755+
""";
756+
String expected1= sample;
757+
758+
assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 }, null);
759+
}
760+
761+
@Test
698762
public void testNoPatternInstanceofToSwitch1() throws Exception {
699763
Hashtable<String, String> options= JavaCore.getOptions();
700764
options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.TAB);

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

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2020, 2024 Red Hat Inc. and others.
2+
* Copyright (c) 2020, 2025 Red Hat Inc. and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -247,5 +247,64 @@ public int foo(Object y) {
247247

248248
assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 }, null);
249249
}
250+
@Test
251+
public void testPatternInstanceofToSwitchExpression2() throws Exception {
252+
Hashtable<String, String> options= JavaCore.getOptions();
253+
options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.TAB);
254+
JavaCore.setOptions(options);
255+
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
256+
String sample= """
257+
package test1;
258+
259+
public class E {
260+
int i;
261+
double d;
262+
boolean b;
263+
264+
public int square(int x) {
265+
return x*x;
266+
}
267+
public int foo(Object y) {
268+
if (y instanceof final Integer xint) {
269+
return xint;
270+
}
271+
if (y instanceof final Double xdouble) {
272+
return square(8); // square
273+
} else if (y instanceof final Boolean xboolean) {
274+
throw new NullPointerException();
275+
}
276+
return 11;
277+
}
278+
}
279+
""";
280+
ICompilationUnit cu1= pack1.createCompilationUnit("E.java", sample, false, null);
281+
282+
enable(CleanUpConstants.USE_SWITCH_FOR_INSTANCEOF_PATTERN);
283+
284+
sample= """
285+
package test1;
286+
287+
public class E {
288+
int i;
289+
double d;
290+
boolean b;
291+
292+
public int square(int x) {
293+
return x*x;
294+
}
295+
public int foo(Object y) {
296+
return switch (y) {
297+
case Integer xint -> xint;
298+
case Double _ -> square(8); // square
299+
case Boolean _ -> throw new NullPointerException();
300+
case null, default -> 11;
301+
};
302+
}
303+
}
304+
""";
305+
String expected1= sample;
306+
307+
assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 }, null);
308+
}
250309

251310
}

0 commit comments

Comments
 (0)