diff --git a/java/java.hints/src/org/netbeans/modules/java/hints/errors/AddDefaultCase.java b/java/java.hints/src/org/netbeans/modules/java/hints/errors/AddDefaultCase.java new file mode 100644 index 000000000000..05f9111bb164 --- /dev/null +++ b/java/java.hints/src/org/netbeans/modules/java/hints/errors/AddDefaultCase.java @@ -0,0 +1,159 @@ +/* + * 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 + */ +@NbBundle.Messages("FIX_Add_Default_Case=Add Default Case") +public class AddDefaultCase implements ErrorRule { + + private static final Set 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 getCodes() { + return CODES; + } + + @Override + public List run(CompilationInfo info, String diagnosticKey, int offset, TreePath treePath, Data 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(); + List cases = expTree.getCases(); + if (cases.isEmpty()) { + return; + } + ParenthesizedTree expression = (ParenthesizedTree) expTree.getExpression(); + + 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 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(); + List cases = switchTree.getCases(); + if (cases.isEmpty()) { + return; + } + ExpressionTree expression = ((ParenthesizedTree) switchTree.getExpression()).getExpression(); + + 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(cases.get(cases.size() - 1), wc, make); + 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 static void handleLastCase(CaseTree lastCase, WorkingCopy wc, TreeMaker make) { + List statements = lastCase.getStatements(); + if (statements.isEmpty() || !(statements.get(statements.size() - 1) instanceof BreakTree)) { + List expanded = new ArrayList<>(statements.size() + 1); + expanded.addAll(statements); + expanded.add(make.Break(null)); + wc.rewrite(lastCase, make.CasePatterns(lastCase.getLabels(), expanded)); + } + } + } +} diff --git a/java/java.hints/src/org/netbeans/modules/java/hints/errors/DifferentCaseKindsFix.java b/java/java.hints/src/org/netbeans/modules/java/hints/errors/DifferentCaseKindsFix.java index 3afb93e73617..602f0406c94c 100644 --- a/java/java.hints/src/org/netbeans/modules/java/hints/errors/DifferentCaseKindsFix.java +++ b/java/java.hints/src/org/netbeans/modules/java/hints/errors/DifferentCaseKindsFix.java @@ -119,13 +119,8 @@ public void cancel() { private static final class FixImpl extends JavaFix { - CompilationInfo info; - TreePath path; - public FixImpl(CompilationInfo info, TreePath path) { super(info, path); - this.info = info; - this.path = path; } @Override diff --git a/java/java.hints/src/org/netbeans/modules/java/hints/errors/VarCompDeclaration.java b/java/java.hints/src/org/netbeans/modules/java/hints/errors/VarCompDeclaration.java index 390b1aac3509..11dd86ab9adf 100644 --- a/java/java.hints/src/org/netbeans/modules/java/hints/errors/VarCompDeclaration.java +++ b/java/java.hints/src/org/netbeans/modules/java/hints/errors/VarCompDeclaration.java @@ -85,12 +85,10 @@ public void cancel() { private static final class FixImpl extends JavaFix { CompilationInfo info; - TreePath path; public FixImpl(CompilationInfo info, TreePath path) { super(info, path); this.info = info; - this.path = path; } @Override diff --git a/java/java.hints/src/org/netbeans/modules/java/hints/resources/layer.xml b/java/java.hints/src/org/netbeans/modules/java/hints/resources/layer.xml index 7de0a453a1d8..033ee7da014f 100644 --- a/java/java.hints/src/org/netbeans/modules/java/hints/resources/layer.xml +++ b/java/java.hints/src/org/netbeans/modules/java/hints/resources/layer.xml @@ -164,6 +164,7 @@ + @@ -327,7 +328,7 @@ - + diff --git a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AccessErrorTest.java b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AccessErrorTest.java index 207592e37707..20b67c265e8a 100644 --- a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AccessErrorTest.java +++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AccessErrorTest.java @@ -18,12 +18,7 @@ */ package org.netbeans.modules.java.hints.errors; -import com.sun.source.util.TreePath; -import java.util.List; -import java.util.Set; -import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.modules.java.hints.infrastructure.ErrorHintsTestBase; -import org.netbeans.spi.editor.hints.Fix; import org.openide.util.NbBundle; /** @@ -33,7 +28,7 @@ public class AccessErrorTest extends ErrorHintsTestBase { public AccessErrorTest(String name) { - super(name); + super(name, AccessError.class); } public void testSimple() throws Exception { @@ -61,21 +56,6 @@ public void testSimple() throws Exception { "}\n").replaceAll("[\\s]+", " ")); } - @Override - protected List computeFixes(CompilationInfo info, int pos, TreePath path) throws Exception { - return new AccessError().run(info, null, pos, path, null); - } - - @Override - protected String toDebugString(CompilationInfo info, Fix f) { - return f.getText(); - } - - @Override - protected Set getSupportedErrorKeys() { - return new AccessError().getCodes(); - } - static { NbBundle.setBranding("test"); } diff --git a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AddCastTest.java b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AddCastTest.java index 37d427824917..fc3228f83513 100644 --- a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AddCastTest.java +++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AddCastTest.java @@ -19,12 +19,7 @@ package org.netbeans.modules.java.hints.errors; -import com.sun.source.util.TreePath; -import java.util.List; -import java.util.Set; -import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.modules.java.hints.infrastructure.ErrorHintsTestBase; -import org.netbeans.spi.editor.hints.Fix; import org.openide.util.NbBundle; /** @@ -34,7 +29,7 @@ public class AddCastTest extends ErrorHintsTestBase { public AddCastTest(String testName) { - super(testName); + super(testName, AddCast.class); } public void test117868() throws Exception { @@ -157,21 +152,6 @@ public void test214835b() throws Exception { "package test; public class Test { private static void x(long l) { t((byte) l); } void t(byte b) {} void t(int i) {} void t(String s) {} }"); } - @Override - protected List computeFixes(CompilationInfo info, int pos, TreePath path) throws Exception { - return new AddCast().run(info, null, pos, path, null); - } - - @Override - protected String toDebugString(CompilationInfo info, Fix f) { - return f.getText(); - } - - @Override - protected Set getSupportedErrorKeys() { - return new AddCast().getCodes(); - } - static { NbBundle.setBranding("test"); } diff --git a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AddCatchFixTest.java b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AddCatchFixTest.java index c7a67178e1a6..a33308a753a0 100644 --- a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AddCatchFixTest.java +++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AddCatchFixTest.java @@ -18,12 +18,7 @@ */ package org.netbeans.modules.java.hints.errors; -import com.sun.source.util.TreePath; -import java.util.List; -import java.util.Set; -import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.modules.java.hints.infrastructure.ErrorHintsTestBase; -import org.netbeans.spi.editor.hints.Fix; import org.openide.util.NbBundle; /** @@ -33,7 +28,7 @@ public class AddCatchFixTest extends ErrorHintsTestBase { public AddCatchFixTest(String testName) { - super(testName); + super(testName, UncaughtException.class); } public void test207480a() throws Exception { @@ -44,20 +39,6 @@ public void test207480a() throws Exception { "package test; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; public class Test {public void test() {try { test2(); } catch (IOException ex) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); } finally{ } } private void test2() throws IOException {} }"); } - protected List computeFixes(CompilationInfo info, int pos, TreePath path) throws Exception { - return new UncaughtException().run(info, null, pos, path, null); - } - - @Override - protected Set getSupportedErrorKeys() { - return new UncaughtException().getCodes(); - } - - @Override - protected String toDebugString(CompilationInfo info, Fix f) { - return f.getText(); - } - static { NbBundle.setBranding("test"); } diff --git a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AddConstructorTest.java b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AddConstructorTest.java index b879e3cadc26..eb4337285511 100644 --- a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AddConstructorTest.java +++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AddConstructorTest.java @@ -18,13 +18,7 @@ */ package org.netbeans.modules.java.hints.errors; -import org.netbeans.modules.java.hints.errors.AddConstructor; -import com.sun.source.util.TreePath; -import java.util.List; -import java.util.Set; -import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.modules.java.hints.infrastructure.ErrorHintsTestBase; -import org.netbeans.spi.editor.hints.Fix; import org.openide.util.NbBundle; /** diff --git a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AddDefaultCaseTest.java b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AddDefaultCaseTest.java new file mode 100644 index 000000000000..ef8d7ccb22a5 --- /dev/null +++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AddDefaultCaseTest.java @@ -0,0 +1,173 @@ +/* + * 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 org.netbeans.modules.java.hints.infrastructure.ErrorHintsTestBase; +import org.openide.util.NbBundle; + +public class AddDefaultCaseTest extends ErrorHintsTestBase { + + static { + NbBundle.setBranding("test"); + } + + public AddDefaultCaseTest(String testName) { + super(testName, AddDefaultCase.class); + } + + @Override + protected void setUp() throws Exception { + sourceLevel = "21"; + super.setUp(); + } + + public void testSwitch() throws Exception { + performFixTest( + "test/Test.java", + """ + package test; + public class Test { + private void test(String o) { + switch (o) { + case null: break; + case "1": break; + case "2": break; + } + } + } + """, + -1, + "Add Default Case", + """ + package test; + public class Test { + private void test(String o) { + switch (o) { + case null: break; + case "1": break; + case "2": break; + default: throw new IllegalStateException("Unexpected value: " + o); + } + } + } + """.replaceAll("\\s+", " ") + ); + } + + public void testRuleSwitch() throws Exception { + performFixTest( + "test/Test.java", + """ + package test; + public class Test { + private void test(Object o) { + switch (o) { + case null -> System.out.println("null case"); + case Integer i when i > 0 -> System.out.println("2nd case"); + case Integer i -> System.out.println("3rd case"); + } + } + } + """, + -1, + "Add Default Case", + """ + package test; + public class Test { + private void test(Object o) { + switch (o) { + case null -> System.out.println("null case"); + case Integer i when i > 0 -> System.out.println("2nd case"); + case Integer i -> System.out.println("3rd case"); + default -> throw new IllegalStateException("Unexpected value: " + o); + } + } + } + """.replaceAll("\\s+", " ") + ); + } + + public void testRuleSwitchExpression() throws Exception { + performFixTest( + "test/Test.java", + """ + package test; + public class Test { + private Object test(Object o) { + return switch (o) { + case null -> 1; + case Integer i when i > 0 -> 2; + case Integer i -> 3; + }; + } + } + """, + -1, + "Add Default Case", + """ + package test; + public class Test { + private Object test(Object o) { + return switch (o) { + case null -> 1; + case Integer i when i > 0 -> 2; + case Integer i -> 3; + default -> throw new IllegalStateException("Unexpected value: " + (o)); + }; + } + } + """.replaceAll("\\s+", " ") + ); + } + + public void testSwitchExpression() throws Exception { + performFixTest( + "test/Test.java", + """ + package test; + public class Test { + private Object test(String o) { + return switch (o) { + case null: yield 1; + case "1": yield 2; + case "2": yield 3; + }; + } + } + """, + -1, + "Add Default Case", + """ + package test; + public class Test { + private Object test(String o) { + return switch (o) { + case null: yield 1; + case "1": yield 2; + case "2": yield 3; + default: throw new IllegalStateException("Unexpected value: " + (o)); + }; + } + } + """.replaceAll("\\s+", " ") + ); + } + +} diff --git a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/ChangeMethodReturnTypeTest.java b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/ChangeMethodReturnTypeTest.java index c83b956275e9..87feb0c5291a 100644 --- a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/ChangeMethodReturnTypeTest.java +++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/ChangeMethodReturnTypeTest.java @@ -18,11 +18,7 @@ */ package org.netbeans.modules.java.hints.errors; -import com.sun.source.util.TreePath; -import java.util.List; -import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.modules.java.hints.infrastructure.ErrorHintsTestBase; -import org.netbeans.spi.editor.hints.Fix; import org.openide.util.NbBundle; /** @@ -90,11 +86,6 @@ public void testConditional() throws Exception { "package test; import java.util.Collections;import java.util.List; public class Test { private List t() { return 1 == 1 ? Collections.emptyList() : Collections.emptyList(); } }"); } - @Override - protected String toDebugString(CompilationInfo info, Fix f) { - return f.getText(); - } - static { NbBundle.setBranding("test"); } diff --git a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayTypeTest.java b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayTypeTest.java index a5d79cbee59f..0563cb846820 100644 --- a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayTypeTest.java +++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayTypeTest.java @@ -216,6 +216,7 @@ public void testArrayObject9ElementsFix() throws Exception { "package test; public class Test {{int[]/*comment1*/ k = {1,'c'};}}"); } + @Override protected List computeFixes(CompilationInfo info, int pos, TreePath path) { List fixes = new ConvertInvalidVarToExplicitArrayType().run(info, null, pos, path, null); List result = new LinkedList(); @@ -229,11 +230,6 @@ protected List computeFixes(CompilationInfo info, int pos, TreePath path) { return result; } - @Override - protected String toDebugString(CompilationInfo info, Fix f) { - return (f.getText()); - } - static { NbBundle.setBranding("test"); } diff --git a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/DifferentCaseKindsFixTest.java b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/DifferentCaseKindsFixTest.java index 936f8ae7830c..a8ea40c3a713 100644 --- a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/DifferentCaseKindsFixTest.java +++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/DifferentCaseKindsFixTest.java @@ -18,16 +18,12 @@ */ package org.netbeans.modules.java.hints.errors; -import com.sun.source.util.TreePath; import java.util.ArrayList; import java.util.List; -import java.util.Set; import javax.lang.model.SourceVersion; import javax.swing.event.ChangeListener; -import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.modules.java.hints.infrastructure.ErrorHintsTestBase; import org.netbeans.modules.java.source.parsing.JavacParser; -import org.netbeans.spi.editor.hints.Fix; import org.netbeans.spi.java.queries.CompilerOptionsQueryImplementation; import org.openide.filesystems.FileObject; import org.openide.util.NbBundle; @@ -388,18 +384,4 @@ public void testCase10() throws Exception { + "}\n").replaceAll("[\\s]+", " ")); } - @Override - protected List computeFixes(CompilationInfo info, int pos, TreePath path) throws Exception { - return new DifferentCaseKindsFix().run(info, null, pos, path, null); - } - - @Override - protected Set getSupportedErrorKeys() { - return new DifferentCaseKindsFix().getCodes(); - } - - @Override - protected String toDebugString(CompilationInfo info, Fix f) { - return f.getText(); - } } diff --git a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/NewImportClassTest.java b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/NewImportClassTest.java index fd227e3864fd..865b036cd80f 100644 --- a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/NewImportClassTest.java +++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/NewImportClassTest.java @@ -18,14 +18,9 @@ */ package org.netbeans.modules.java.hints.errors; -import com.sun.source.util.TreePath; -import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; -import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.modules.java.hints.infrastructure.ErrorHintsTestBase; -import org.netbeans.modules.java.hints.spi.ErrorRule.Data; -import org.netbeans.spi.editor.hints.Fix; /** * @@ -34,7 +29,7 @@ public class NewImportClassTest extends ErrorHintsTestBase { public NewImportClassTest(String name) { - super(name); + super(name, ImportClass.class); doRunIndexing = true; } @@ -62,14 +57,4 @@ public void testImportHint200742b() throws Exception { "public class Test { Map b; }\n").replaceAll("[ \t\n]+", " ")); } - @Override - protected List computeFixes(CompilationInfo info, int pos, TreePath path) throws Exception { - return new ImportClass().run(info, null, pos, path, new Data()); - } - - @Override - protected String toDebugString(CompilationInfo info, Fix f) { - return f.getText(); - } - } diff --git a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/UncaughtExceptionTest.java b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/UncaughtExceptionTest.java index 37b012a68ce1..7b6dd22164a0 100644 --- a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/UncaughtExceptionTest.java +++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/UncaughtExceptionTest.java @@ -20,9 +20,7 @@ import java.util.Collections; import java.util.Set; -import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.modules.java.hints.infrastructure.ErrorHintsTestBase; -import org.netbeans.spi.editor.hints.Fix; import org.openide.util.NbBundle; /** @@ -211,12 +209,6 @@ public void testJDK8ResourceImplicitClose() throws Exception { " }\n" + "}").replaceAll("\\s+", " ")); } - - - @Override - protected String toDebugString(CompilationInfo info, Fix f) { - return f.getText(); - } static { NbBundle.setBranding("test"); diff --git a/java/java.source.base/src/org/netbeans/modules/java/source/builder/TreeFactory.java b/java/java.source.base/src/org/netbeans/modules/java/source/builder/TreeFactory.java index edfab4b19099..f4df92838029 100644 --- a/java/java.source.base/src/org/netbeans/modules/java/source/builder/TreeFactory.java +++ b/java/java.source.base/src/org/netbeans/modules/java/source/builder/TreeFactory.java @@ -82,7 +82,6 @@ import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.stream.Collectors; import javax.lang.model.element.*; import javax.lang.model.type.ArrayType; import javax.lang.model.type.TypeKind; @@ -242,14 +241,17 @@ public CaseTree Case(ExpressionTree expression, List st } public CaseTree Case(List expressions, List statements) { - return CaseMultiplePatterns(expressions.isEmpty() ? Collections.singletonList(DefaultCaseLabel()) : expressions.stream().map(e -> ConstantCaseLabel(e)).collect(Collectors.toList()), null, statements); + return CaseMultiplePatterns(expressions.stream().map(this::ConstantCaseLabel).toList(), null, statements); } public CaseTree Case(List expressions, Tree body) { - return CaseMultiplePatterns(expressions.isEmpty() ? Collections.singletonList(DefaultCaseLabel()) : expressions.stream().map(e -> ConstantCaseLabel(e)).collect(Collectors.toList()), null, body); + return CaseMultiplePatterns(expressions.stream().map(this::ConstantCaseLabel).toList(), null, body); } public CaseTree CaseMultiplePatterns(List expressions, ExpressionTree guard, Tree body) { + if (expressions.isEmpty()) { + expressions = Collections.singletonList(DefaultCaseLabel()); + } ListBuffer lb = new ListBuffer<>(); lb.append(body instanceof ExpressionTree ? (JCStatement) Yield((ExpressionTree) body) : (JCStatement) body); ListBuffer exprs = new ListBuffer<>(); @@ -257,10 +259,12 @@ public CaseTree CaseMultiplePatterns(List expressions, exprs.append((JCCaseLabel)t); return make.at(NOPOS).Case(CaseKind.RULE, exprs.toList(), (JCExpression) guard, lb.toList(), (JCTree) body); } - public CaseTree CaseMultiplePatterns(List expressions, ExpressionTree guard, List statements) { - ListBuffer lb = new ListBuffer(); + if (expressions.isEmpty()) { + expressions = Collections.singletonList(DefaultCaseLabel()); + } + ListBuffer lb = new ListBuffer<>(); for (StatementTree t : statements) lb.append((JCStatement)t); ListBuffer exprs = new ListBuffer<>();