Skip to content

Commit

Permalink
Add quick fixes to suppress certain warnings using @SuppressWarnings.
Browse files Browse the repository at this point in the history
Signed-off-by: Hope Hadfield <[email protected]>
  • Loading branch information
hopehadfield authored Jan 30, 2024
1 parent 1c05786 commit 0b409f5
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.manipulation.CUCorrectionProposalCore;
import org.eclipse.jdt.core.manipulation.ChangeCorrectionProposalCore;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.ui.text.correction.IInvocationContextCore;
import org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore;
import org.eclipse.jdt.internal.ui.text.correction.IProposalRelevance;
Expand Down Expand Up @@ -533,17 +534,12 @@ private void process(CodeActionParams params, IInvocationContextCore context, IP
// problem, proposals);
// break;
// case IProblem.UnhandledWarningToken:
// SuppressWarningsSubProcessor.addUnknownSuppressWarningProposals(context,
// problem, proposals);
// break;
// case IProblem.ProblemNotAnalysed:
// SuppressWarningsSubProcessor.addRemoveUnusedSuppressWarningProposals(context,
// problem, proposals);
// break;
// case IProblem.UnusedWarningToken:
// SuppressWarningsSubProcessor.addRemoveUnusedSuppressWarningProposals(context,
// problem, proposals);
// SuppressWarningsSubProcessor.addUnknownSuppressWarningProposals(context, problem, proposals);
// break;
case IProblem.ProblemNotAnalysed:
case IProblem.UnusedWarningToken:
SuppressWarningsSubProcessor.addRemoveUnusedSuppressWarningProposals(context, problem, proposals);
break;
case IProblem.MissingEnumConstantCase:
case IProblem.MissingEnumDefaultCase:
case IProblem.SwitchExpressionsYieldMissingEnumConstantCase:
Expand Down Expand Up @@ -681,12 +677,9 @@ private void process(CodeActionParams params, IInvocationContextCore context, IP
String str = problem.toString();
System.out.println(str);
}
// if
// (JavaModelUtil.is50OrHigher(context.getCompilationUnit().getJavaProject()))
// {
// SuppressWarningsSubProcessor.addSuppressWarningsProposals(context,
// problem, proposals);
// }
if (JavaModelUtil.is50OrHigher(context.getCompilationUnit().getJavaProject())) {
SuppressWarningsSubProcessor.addSuppressWarningsProposals(context, problem, proposals);
}
// ConfigureProblemSeveritySubProcessor.addConfigureProblemSeverityProposal(context,
// problem, proposals);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*******************************************************************************
* Copyright (c) 2024 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.corrections;

import java.util.Collection;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.internal.ui.text.correction.IInvocationContextCore;
import org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore;
import org.eclipse.jdt.internal.ui.text.correction.SuppressWarningsBaseSubProcessor;
import org.eclipse.jdt.internal.ui.text.correction.SuppressWarningsProposalCore;
import org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler;
import org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposalCore;
import org.eclipse.lsp4j.CodeActionKind;

public class SuppressWarningsSubProcessor extends SuppressWarningsBaseSubProcessor<ProposalKindWrapper> {

public static void addSuppressWarningsProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<ProposalKindWrapper> proposals) {
new SuppressWarningsSubProcessor().getSuppressWarningsProposals(context, problem, proposals);
}

public static void addUnknownSuppressWarningProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<ProposalKindWrapper> proposals) {
new SuppressWarningsSubProcessor().getUnknownSuppressWarningProposals(context, problem, proposals);
}

public static void addRemoveUnusedSuppressWarningProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<ProposalKindWrapper> proposals) {
new SuppressWarningsSubProcessor().getRemoveUnusedSuppressWarningProposals(context, problem, proposals);
}

@Override
protected ProposalKindWrapper createSuppressWarningsProposal(String warningToken, String label, ICompilationUnit cu, ASTNode node, ChildListPropertyDescriptor property, int relevance) {
return CodeActionHandler.wrap(new SuppressWarningsProposalCore(warningToken, label, cu, node, property, relevance), CodeActionKind.QuickFix);
}

@Override
protected ProposalKindWrapper createASTRewriteCorrectionProposal(String name, ICompilationUnit cu, ASTRewrite rewrite, int relevance) {
return CodeActionHandler.wrap(new ASTRewriteCorrectionProposalCore(name, cu, rewrite, relevance), CodeActionKind.QuickFix);
}

}
4 changes: 2 additions & 2 deletions org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
<unit id="org.eclipse.equinox.sdk.feature.group" version="0.0.0"/>
<unit id="org.mockito.mockito-core" version="0.0.0"/>
<unit id="org.apache.commons.commons-io" version="0.0.0"/>
<unit id="org.eclipse.jdt.apt.pluggable.core" version="0.0.0" />
<repository location="https://download.eclipse.org/eclipse/updates/4.31-I-builds/I20240128-1800/"/>
<unit id="org.eclipse.jdt.apt.pluggable.core" version="0.0.0"/>
<repository location="https://download.eclipse.org/eclipse/updates/4.31-I-builds/I20240129-1800/"/>
</location>
<location includeAllPlatforms="false" includeConfigurePhase="false" includeMode="planner" includeSource="true" type="InstallableUnit">
<unit id="org.eclipse.jdt.core.compiler.batch" version="0.0.0"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void testMissingEnumConstant() throws Exception {
Range range = new Range(new Position(5, 16), new Position(5, 17));
setIgnoredCommands(ActionMessages.GenerateConstructorsAction_ellipsisLabel, ActionMessages.GenerateConstructorsAction_label);
List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, range);
assertEquals(3, codeActions.size());
assertEquals(4, codeActions.size());
Either<Command, CodeAction> codeAction = codeActions.get(0);
CodeAction action = codeAction.getRight();
assertEquals(CodeActionKind.QuickFix, action.getKind());
Expand All @@ -84,6 +84,10 @@ public void testMissingEnumConstant() throws Exception {
assertEquals("Add missing case statements", action.getTitle());
edit = getTextEdit(codeAction);
assertEquals("\n case One:\n break;\n default:\n break;", edit.getNewText());
codeAction = codeActions.get(2);
action = codeAction.getRight();
assertEquals(CodeActionKind.QuickFix, action.getKind());
assertEquals("Add @SuppressWarnings 'incomplete-switch' to 'testing()'", action.getTitle());
}

@Test
Expand Down Expand Up @@ -112,7 +116,7 @@ public void testMissingEnumConstantDespiteDefault() throws Exception {
options.put(JavaCore.COMPILER_PB_MISSING_ENUM_CASE_DESPITE_DEFAULT, JavaCore.ENABLED);
fJProject.setOptions(options);
codeActions = evaluateCodeActions(cu, range);
assertEquals(3, codeActions.size());
assertEquals(4, codeActions.size());
Either<Command, CodeAction> codeAction = codeActions.get(0);
CodeAction action = codeAction.getRight();
assertEquals(CodeActionKind.QuickFix, action.getKind());
Expand All @@ -125,6 +129,10 @@ public void testMissingEnumConstantDespiteDefault() throws Exception {
assertEquals("Insert '//$CASES-OMITTED$'", action.getTitle());
edit = getTextEdit(codeAction);
assertEquals(" //$CASES-OMITTED$\n ", edit.getNewText());
codeAction = codeActions.get(2);
action = codeAction.getRight();
assertEquals(CodeActionKind.QuickFix, action.getKind());
assertEquals("Add @SuppressWarnings 'incomplete-switch' to 'testing()'", action.getTitle());
}

private TextEdit getTextEdit(Either<Command, CodeAction> codeAction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ public void testRemoveDeadCodeAfterIf() throws Exception {
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

List<Either<Command, CodeAction>> codeActions = getCodeActions(cu);
assertEquals(codeActions.size(), 1);
assertEquals(codeActions.get(0).getRight().getKind(), CodeActionKind.QuickFix);
boolean hasCodeAction = codeActions.stream().anyMatch(codeAction -> "Remove (including condition)".equals(codeAction.getRight().getTitle()));
assertTrue("Code action to remove dead code not found", hasCodeAction);
}

protected List<Either<Command, CodeAction>> getCodeActions(ICompilationUnit cu) throws JavaModelException {
Expand Down

0 comments on commit 0b409f5

Please sign in to comment.