lahodaj commented on PR #7973:
URL: https://github.com/apache/netbeans/pull/7973#issuecomment-2485440932
Nice catch. Overall, I agree with the solution, although having a test would
be good. Also, we could consider making the code a little bit more
understandable, e.g.:
```
diff --git
a/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOf.java
b/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOf.java
index a295ed153f..f52cd72e33 100644
---
a/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOf.java
+++
b/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOf.java
@@ -28,6 +28,7 @@ import com.sun.source.tree.StatementTree;
import com.sun.source.tree.SwitchTree;
import com.sun.source.tree.ThrowTree;
import com.sun.source.tree.Tree;
+import com.sun.source.tree.Tree.Kind;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePath;
import java.util.ArrayList;
@@ -272,18 +273,21 @@ public class ConvertToSwitchPatternInstanceOf {
}
Tree expression = ((ParenthesizedTree)
switchTree.getExpression()).getExpression();
Tree parent = (Tree) ctx.getPath().getParentPath().getLeaf();
- int indexOf;
- if (parent instanceof BlockTree) {
- indexOf = ((BlockTree)
parent).getStatements().indexOf(switchTree) - 1;
+ int indexOfSwitch;
+ if (parent.getKind() == Kind.BLOCK) {
+ indexOfSwitch = ((BlockTree)
parent).getStatements().indexOf(switchTree);
+ if (indexOfSwitch < 1) {
+ return null;
+ }
} else {
return null;
}
- Tree ifTree = ((BlockTree) parent).getStatements().get(indexOf);
- if ((!(ifTree instanceof IfTree) || !MatcherUtilities.matches(ctx,
new TreePath(ctx.getPath(), ((IfTree) ifTree).getCondition()), "($expr0 ==
null)", true))
+ Tree ifTree = ((BlockTree)
parent).getStatements().get(indexOfSwitch - 1);
+ if (ifTree.getKind() != Kind.IF || !MatcherUtilities.matches(ctx,
new TreePath(ctx.getPath(), ((IfTree) ifTree).getCondition()), "($expr0 ==
null)", true)
||
!(ctx.getVariables().get("$expr0").getLeaf().toString().equals(expression.toString())))
{
return null;
}
- Fix fix = new FixSwitchPatternMatchToSwitchNull(ctx.getInfo(),
ctx.getPath().getParentPath(), indexOf).toEditorFix();
+ Fix fix = new FixSwitchPatternMatchToSwitchNull(ctx.getInfo(),
ctx.getPath().getParentPath(), indexOfSwitch).toEditorFix();
return ErrorDescriptionFactory.forTree(ctx, ifTree,
Bundle.ERR_ConvertToSwitchPatternInstanceOf(), fix);
}
@@ -300,11 +304,11 @@ public class ConvertToSwitchPatternInstanceOf {
private static final class FixSwitchPatternMatchToSwitchNull extends
JavaFix {
- private final int indexOf;
+ private final int indexOfSwitch;
- public FixSwitchPatternMatchToSwitchNull(CompilationInfo info,
TreePath path, int indexOf) {
+ public FixSwitchPatternMatchToSwitchNull(CompilationInfo info,
TreePath path, int indexOfSwitch) {
super(info, path);
- this.indexOf = indexOf;
+ this.indexOfSwitch = indexOfSwitch;
}
@Override
@@ -318,9 +322,9 @@ public class ConvertToSwitchPatternInstanceOf {
TreePath main = ctx.getPath();
TreeMaker make = wc.getTreeMaker();
List<Tree> caseNullLabel = new LinkedList<>();
- SwitchTree switchTree = (SwitchTree) ((BlockTree)
main.getLeaf()).getStatements().get(indexOf + 1);
+ SwitchTree switchTree = (SwitchTree) ((BlockTree)
main.getLeaf()).getStatements().get(indexOfSwitch);
- Tree ifTree = ((BlockTree)
main.getLeaf()).getStatements().get(indexOf);
+ Tree ifTree = ((BlockTree)
main.getLeaf()).getStatements().get(indexOfSwitch - 1);
StatementTree thenStatement = ((IfTree)
ifTree).getThenStatement();
caseNullLabel.add(wc.getTreeMaker().Identifier("null"));
BlockTree blockTree = (BlockTree)thenStatement;
@@ -328,7 +332,7 @@ public class ConvertToSwitchPatternInstanceOf {
CaseTree caseMultipleSwitchPatterns =
wc.getTreeMaker().CasePatterns(caseNullLabel, statementTree);
SwitchTree insertSwitchCase = make.insertSwitchCase(switchTree,
0, caseMultipleSwitchPatterns);
wc.rewrite(switchTree, insertSwitchCase);
- BlockTree removeBlockStatement =
make.removeBlockStatement((BlockTree) main.getLeaf(), indexOf);
+ BlockTree removeBlockStatement =
make.removeBlockStatement((BlockTree) main.getLeaf(), indexOfSwitch - 1);
wc.rewrite(main.getLeaf(), removeBlockStatement);
}
}
diff --git
a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOfTest.java
b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOfTest.java
index 4e25857d72..4d7f7a7a49 100644
---
a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOfTest.java
+++
b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOfTest.java
@@ -209,6 +209,24 @@ public class ConvertToSwitchPatternInstanceOfTest
extends NbTestCase {
+ "}\n");
}
+ @Test
+ public void testSwitchWithNullExceptionSwitchIsFirst() throws Exception
{
+ HintTest.create()
+ .input("""
+ package test;
+ public class Test {
+ private static void test(Object o) {
+ switch (o) {
+ case String s -> {}
+ }
+ }
+ }
+ """, false)
+ .sourceLevel("21")
+ .run(ConvertToSwitchPatternInstanceOf.class)
+ .assertWarnings();
+ }
+
@Test
public void testSimpleSwitchWithNullNoHint() throws Exception {
HintTest.create()
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists