Skip to content

Commit

Permalink
Add quickfix for redundant interface
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza committed Jan 28, 2020
1 parent b4aa67d commit b461019
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ChangeCorrectionProposal> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> 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<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, selection);
assertEquals(0, codeActions.size());
}
}

0 comments on commit b461019

Please sign in to comment.