From b461019eafc77bca53408a92bef2699e63999f8b Mon Sep 17 00:00:00 2001 From: Snjezana Peco Date: Mon, 27 Jan 2020 16:34:51 +0100 Subject: [PATCH] Add quickfix for redundant interface Signed-off-by: Snjezana Peco --- .../corrections/QuickFixProcessor.java | 7 +- .../LocalCorrectionsSubProcessor.java | 14 +++ .../preferences/PreferenceManager.java | 1 + .../RedundantInterfaceQuickFixTest.java | 86 +++++++++++++++++++ 4 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/RedundantInterfaceQuickFixTest.java diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/QuickFixProcessor.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/QuickFixProcessor.java index 5768839012..e672a25828 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/QuickFixProcessor.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/QuickFixProcessor.java @@ -159,10 +159,9 @@ private void process(IInvocationContext context, IProblemLocationCore problem, C case IProblem.InvalidUnionTypeReferenceSequence: LocalCorrectionsSubProcessor.addUnreachableCatchProposals(context, problem, proposals); break; - // case IProblem.RedundantSuperinterface: - // LocalCorrectionsSubProcessor.addRedundantSuperInterfaceProposal(context, - // problem, proposals); - // break; + case IProblem.RedundantSuperinterface: + LocalCorrectionsSubProcessor.addRedundantSuperInterfaceProposal(context, problem, proposals); + break; case IProblem.VoidMethodReturnsValue: ReturnTypeSubProcessor.addVoidMethodReturnsProposals(context, problem, proposals); break; diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/proposals/LocalCorrectionsSubProcessor.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/proposals/LocalCorrectionsSubProcessor.java index e2f6447a50..debd2eeba3 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/proposals/LocalCorrectionsSubProcessor.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/proposals/LocalCorrectionsSubProcessor.java @@ -52,6 +52,7 @@ import org.eclipse.jdt.core.dom.MethodDeclaration; import org.eclipse.jdt.core.dom.MethodReference; import org.eclipse.jdt.core.dom.Modifier; +import org.eclipse.jdt.core.dom.Name; import org.eclipse.jdt.core.dom.ParenthesizedExpression; import org.eclipse.jdt.core.dom.SingleVariableDeclaration; import org.eclipse.jdt.core.dom.Statement; @@ -695,4 +696,17 @@ public static void addCorrectAccessToStaticProposals(IInvocationContext context, ModifierCorrectionSubProcessor.addNonAccessibleReferenceProposal(context, problem, proposals, ModifierCorrectionSubProcessor.TO_NON_STATIC, IProposalRelevance.REMOVE_STATIC_MODIFIER); } + public static void addRedundantSuperInterfaceProposal(IInvocationContext context, IProblemLocationCore problem, Collection proposals) { + ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot()); + if (!(selectedNode instanceof Name)) { + return; + } + ASTNode node = ASTNodes.getNormalizedNode(selectedNode); + ASTRewrite rewrite = ASTRewrite.create(node.getAST()); + rewrite.remove(node, null); + String label = CorrectionMessages.LocalCorrectionsSubProcessor_remove_redundant_superinterface; + ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_REDUNDANT_SUPER_INTERFACE); + proposals.add(proposal); + } + } diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/PreferenceManager.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/PreferenceManager.java index c0ba4b96b1..7192b9aae6 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/PreferenceManager.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/PreferenceManager.java @@ -92,6 +92,7 @@ public static void initialize() { javaCoreOptions.put(JavaCore.COMPILER_RELEASE, JavaCore.ENABLED); javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_USE_ON_OFF_TAGS, DefaultCodeFormatterConstants.TRUE); javaCoreOptions.put(JavaCore.COMPILER_PB_UNHANDLED_WARNING_TOKEN, JavaCore.IGNORE); + javaCoreOptions.put(JavaCore.COMPILER_PB_REDUNDANT_SUPERINTERFACE, JavaCore.WARNING); JavaCore.setOptions(javaCoreOptions); // Initialize default preferences diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/RedundantInterfaceQuickFixTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/RedundantInterfaceQuickFixTest.java new file mode 100644 index 0000000000..eff10ec24c --- /dev/null +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/RedundantInterfaceQuickFixTest.java @@ -0,0 +1,86 @@ +/******************************************************************************* + * Copyright (c) 2020 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 + * https://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.correction; + +import static org.junit.Assert.assertEquals; + +import java.util.List; +import java.util.Map; + +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IPackageFragment; +import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.lsp4j.CodeAction; +import org.eclipse.lsp4j.Command; +import org.eclipse.lsp4j.Position; +import org.eclipse.lsp4j.Range; +import org.eclipse.lsp4j.jsonrpc.messages.Either; +import org.junit.Before; +import org.junit.Test; + +/** + * @author snjeza + * + */ +public class RedundantInterfaceQuickFixTest extends AbstractQuickFixTest { + + private IJavaProject fJProject; + private IPackageFragmentRoot fSourceFolder; + + @Before + public void setup() throws Exception { + fJProject = newEmptyProject(); + Map testProjectOptions = TestOptions.getDefaultOptions(); + testProjectOptions.put(JavaCore.COMPILER_PB_REDUNDANT_SUPERINTERFACE, JavaCore.WARNING); + fJProject.setOptions(testProjectOptions); + fSourceFolder = fJProject.getPackageFragmentRoot(fJProject.getProject().getFolder("src")); + } + + @Test + public void testRedundantSuperinterface() throws Exception { + IPackageFragment pack = fSourceFolder.createPackageFragment("test", false, null); + StringBuilder buf = new StringBuilder(); + buf.append("package test;\n"); + buf.append("public class RedundantInterface implements Int1, Int2 {}\n"); + buf.append("interface Int1 {}\n"); + buf.append("interface Int2 extends Int1 {}\n"); + ICompilationUnit cu = pack.createCompilationUnit("RedundantInterface.java", buf.toString(), true, null); + buf = new StringBuilder(); + buf.append("package test;\n"); + buf.append("public class RedundantInterface implements Int2 {}\n"); + buf.append("interface Int1 {}\n"); + buf.append("interface Int2 extends Int1 {}\n"); + Expected e1 = new Expected("Remove super interface", buf.toString()); + Range selection = new Range(new Position(1, 45), new Position(1, 45)); + assertCodeActions(cu, selection, e1); + } + + @Test + public void testIgnoreRedundantSuperinterface() throws Exception { + Map testProjectOptions = fJProject.getOptions(false); + testProjectOptions.put(JavaCore.COMPILER_PB_REDUNDANT_SUPERINTERFACE, JavaCore.IGNORE); + fJProject.setOptions(testProjectOptions); + IPackageFragment pack = fSourceFolder.createPackageFragment("test", false, null); + StringBuilder buf = new StringBuilder(); + buf.append("package test;\n"); + buf.append("public class RedundantInterface implements Int1, Int2 {}\n"); + buf.append("interface Int1 {}\n"); + buf.append("interface Int2 extends Int1 {}\n"); + ICompilationUnit cu = pack.createCompilationUnit("RedundantInterface.java", buf.toString(), true, null); + Range selection = new Range(new Position(1, 45), new Position(1, 45)); + List> codeActions = evaluateCodeActions(cu, selection); + assertEquals(0, codeActions.size()); + } +}