donphelix commented on code in PR #8860: URL: https://github.com/apache/netbeans/pull/8860#discussion_r2386804880
########## java/java.hints/src/org/netbeans/modules/java/hints/errors/AddDefaultCase.java: ########## @@ -0,0 +1,154 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.java.hints.errors; + +import com.sun.source.tree.BreakTree; +import com.sun.source.tree.CaseTree; +import com.sun.source.tree.CaseTree.CaseKind; +import com.sun.source.tree.ExpressionTree; +import com.sun.source.tree.ParenthesizedTree; +import com.sun.source.tree.StatementTree; +import com.sun.source.tree.SwitchExpressionTree; +import com.sun.source.tree.SwitchTree; +import com.sun.source.tree.Tree; +import com.sun.source.util.SourcePositions; +import com.sun.source.util.TreePath; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import org.netbeans.api.java.source.CompilationInfo; +import org.netbeans.api.java.source.TreeMaker; +import org.netbeans.api.java.source.WorkingCopy; +import org.netbeans.modules.java.hints.spi.ErrorRule; +import org.netbeans.spi.editor.hints.Fix; +import org.netbeans.spi.java.hints.JavaFix; +import org.openide.util.NbBundle; + +/** + * Resolves javac error by adding a missing default case to a switch. + * + * @author SANDEEMI + */ [email protected]("FIX_Add_Default_Case=Add Default Case") +public class AddDefaultCase implements ErrorRule<Void> { + + private static final Set<String> CODES = Set.of( + "compiler.err.not.exhaustive", + "compiler.err.not.exhaustive.statement" + ); + + private static final String THROW_ISE = "throw new IllegalStateException(\"Unexpected value: \" + %1$s);"; + + @Override + public Set<String> getCodes() { + return CODES; + } + + @Override + public List<Fix> run(CompilationInfo info, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) { + return List.of(new AddDefaultCaseFix(info, treePath).toEditorFix()); + } + + @Override + public String getId() { + return AddDefaultCase.class.getName(); + } + + @Override + public String getDisplayName() { + return Bundle.FIX_Add_Default_Case(); + } + + @Override + public void cancel() { + + } + + private static final class AddDefaultCaseFix extends JavaFix { + + public AddDefaultCaseFix(CompilationInfo info, TreePath path) { + super(info, path); + } + + @Override + protected String getText() { + return Bundle.FIX_Add_Default_Case(); + } + + @Override + protected void performRewrite(TransformationContext ctx) throws Exception { + WorkingCopy wc = ctx.getWorkingCopy(); + TreeMaker make = wc.getTreeMaker(); + TreePath path = ctx.getPath(); + + switch (path.getLeaf().getKind()) { + case SWITCH_EXPRESSION -> { + SwitchExpressionTree expTree = (SwitchExpressionTree) path.getLeaf(); + ParenthesizedTree expression = (ParenthesizedTree) expTree.getExpression(); + List<? extends CaseTree> cases = expTree.getCases(); + + String text = THROW_ISE.formatted(expression.toString()); + StatementTree parseStatement = wc.getTreeUtilities().parseStatement(text, new SourcePositions[1]); + CaseTree caseSwitchPatterns; + if (cases.get(0).getCaseKind() == CaseKind.RULE) { + caseSwitchPatterns = make.CasePatterns(List.of(), parseStatement); + } else { + caseSwitchPatterns = make.CasePatterns(List.of(), List.of(parseStatement)); + } + + List<CaseTree> newCases = new ArrayList<>(cases.size() + 1); + newCases.addAll(cases); + newCases.add(caseSwitchPatterns); + Tree switchExpression = make.SwitchExpression(expression, newCases); + wc.rewrite(expTree, switchExpression); + } + case SWITCH -> { + SwitchTree switchTree = (SwitchTree) path.getLeaf(); + ExpressionTree expression = ((ParenthesizedTree) switchTree.getExpression()).getExpression(); + List<? extends CaseTree> cases = switchTree.getCases(); + + String text = THROW_ISE.formatted(expression.toString()); + StatementTree parseStatement = wc.getTreeUtilities().parseStatement(text, new SourcePositions[1]); + CaseTree caseSwitchPatterns; + if (cases.get(0).getCaseKind() == CaseKind.RULE) { + caseSwitchPatterns = make.CasePatterns(List.of(), parseStatement); + } else { + handleLastCase(switchTree, wc, make, cases.size()); + caseSwitchPatterns = make.CasePatterns(List.of(), List.of(parseStatement)); + } + + SwitchTree insertSwitchCase = make.addSwitchCase(switchTree, caseSwitchPatterns); + wc.rewrite(switchTree, insertSwitchCase); + } + default -> throw new UnsupportedOperationException(path.getLeaf().getKind() + " not implemented"); + } + } + + private void handleLastCase(SwitchTree switchTree, WorkingCopy wc, TreeMaker make, int size) { + CaseTree lastCase = switchTree.getCases().get(size - 1); + List<? extends StatementTree> original = lastCase.getStatements(); + if (!(original.get(original.size() - 1) instanceof BreakTree)) { Review Comment: Here too, we should check emptiness `original.get(original.size() - 1)` ########## java/java.hints/src/org/netbeans/modules/java/hints/errors/AddDefaultCase.java: ########## @@ -0,0 +1,154 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.java.hints.errors; + +import com.sun.source.tree.BreakTree; +import com.sun.source.tree.CaseTree; +import com.sun.source.tree.CaseTree.CaseKind; +import com.sun.source.tree.ExpressionTree; +import com.sun.source.tree.ParenthesizedTree; +import com.sun.source.tree.StatementTree; +import com.sun.source.tree.SwitchExpressionTree; +import com.sun.source.tree.SwitchTree; +import com.sun.source.tree.Tree; +import com.sun.source.util.SourcePositions; +import com.sun.source.util.TreePath; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import org.netbeans.api.java.source.CompilationInfo; +import org.netbeans.api.java.source.TreeMaker; +import org.netbeans.api.java.source.WorkingCopy; +import org.netbeans.modules.java.hints.spi.ErrorRule; +import org.netbeans.spi.editor.hints.Fix; +import org.netbeans.spi.java.hints.JavaFix; +import org.openide.util.NbBundle; + +/** + * Resolves javac error by adding a missing default case to a switch. + * + * @author SANDEEMI + */ [email protected]("FIX_Add_Default_Case=Add Default Case") +public class AddDefaultCase implements ErrorRule<Void> { + + private static final Set<String> CODES = Set.of( + "compiler.err.not.exhaustive", + "compiler.err.not.exhaustive.statement" + ); + + private static final String THROW_ISE = "throw new IllegalStateException(\"Unexpected value: \" + %1$s);"; + + @Override + public Set<String> getCodes() { + return CODES; + } + + @Override + public List<Fix> run(CompilationInfo info, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) { + return List.of(new AddDefaultCaseFix(info, treePath).toEditorFix()); + } + + @Override + public String getId() { + return AddDefaultCase.class.getName(); + } + + @Override + public String getDisplayName() { + return Bundle.FIX_Add_Default_Case(); + } + + @Override + public void cancel() { + + } + + private static final class AddDefaultCaseFix extends JavaFix { + + public AddDefaultCaseFix(CompilationInfo info, TreePath path) { + super(info, path); + } + + @Override + protected String getText() { + return Bundle.FIX_Add_Default_Case(); + } + + @Override + protected void performRewrite(TransformationContext ctx) throws Exception { + WorkingCopy wc = ctx.getWorkingCopy(); + TreeMaker make = wc.getTreeMaker(); + TreePath path = ctx.getPath(); + + switch (path.getLeaf().getKind()) { + case SWITCH_EXPRESSION -> { + SwitchExpressionTree expTree = (SwitchExpressionTree) path.getLeaf(); + ParenthesizedTree expression = (ParenthesizedTree) expTree.getExpression(); + List<? extends CaseTree> cases = expTree.getCases(); + + String text = THROW_ISE.formatted(expression.toString()); + StatementTree parseStatement = wc.getTreeUtilities().parseStatement(text, new SourcePositions[1]); + CaseTree caseSwitchPatterns; + if (cases.get(0).getCaseKind() == CaseKind.RULE) { Review Comment: Just a nitpick : the lists are accessed without checking for emptiness `cases.get(0)` -- 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
