| author | Manju Mathew | 2013-01-10 14:30:33 (EST) |
|---|---|---|
| committer | Markus Keller | 2013-01-10 14:30:33 (EST) |
| commit | b56753a7cc02eaccb8de11f721340572b0ac7be4 (patch) (side-by-side diff) | |
| tree | 8444228c47f69551852a0b3be0fc4a9eda63046f | |
| parent | 1d67bcb40c63b0f2a68e3ed3b0a91fcdf0885738 (diff) | |
| download | eclipse.jdt.ui-b56753a7cc02eaccb8de11f721340572b0ac7be4.zip eclipse.jdt.ui-b56753a7cc02eaccb8de11f721340572b0ac7be4.tar.gz eclipse.jdt.ui-b56753a7cc02eaccb8de11f721340572b0ac7be4.tar.bz2 | |
Bug 392946: [quick fix] for unused type parameterv20130110-193033
9 files changed, 216 insertions, 17 deletions
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/LocalCorrectionsQuickFixTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/LocalCorrectionsQuickFixTest.java index cd37bef..1bf7ba3 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/LocalCorrectionsQuickFixTest.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/LocalCorrectionsQuickFixTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2012 IBM Corporation and others. + * Copyright (c) 2000, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -62,6 +62,7 @@ public class LocalCorrectionsQuickFixTest extends QuickFixTest { } + @Override protected void setUp() throws Exception { Hashtable options= TestOptions.getDefaultOptions(); options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.SPACE); @@ -86,6 +87,7 @@ public class LocalCorrectionsQuickFixTest extends QuickFixTest { } + @Override protected void tearDown() throws Exception { JavaProjectHelper.clear(fJProject1, ProjectTestSetup.getDefaultClasspath()); } @@ -9227,4 +9229,135 @@ public class LocalCorrectionsQuickFixTest extends QuickFixTest { assertEquals(0, astRoot.getProblems().length); // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=38751#c7 } + public void testUnusedTypeParameter1() throws Exception { + Hashtable hashtable= JavaCore.getOptions(); + hashtable.put(JavaCore.COMPILER_PB_UNUSED_TYPE_PARAMETER, JavaCore.ERROR); + JavaCore.setOptions(hashtable); + + IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); + StringBuffer buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("public interface E<T extends Exception> {\n"); + buf.append(" public void foo(Object str);\n"); + buf.append("}\n"); + ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); + + CompilationUnit astRoot= getASTRoot(cu); + ArrayList proposals= collectCorrections(cu, astRoot); + assertNumberOfProposals(proposals, 2); + assertCorrectLabels(proposals); + + String[] expected= new String[2]; + + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("public interface E {\n"); + buf.append(" public void foo(Object str);\n"); + buf.append("}\n"); + expected[0]= buf.toString(); + + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("/**\n"); + buf.append(" * @param <T> \n"); + buf.append(" */\n"); + buf.append("public interface E<T extends Exception> {\n"); + buf.append(" public void foo(Object str);\n"); + buf.append("}\n"); + expected[1]= buf.toString(); + + assertExpectedExistInProposals(proposals, expected); + } + + public void testUnusedTypeParameter2() throws Exception { + + Hashtable hashtable= JavaCore.getOptions(); + hashtable.put(JavaCore.COMPILER_PB_UNUSED_TYPE_PARAMETER, JavaCore.ERROR); + JavaCore.setOptions(hashtable); + + IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); + StringBuffer buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append(" /**\n"); + buf.append(" * @param <X> \n"); + buf.append(" * @see E\n"); + buf.append(" */\n"); + buf.append("public interface E<X, T> {\n"); + buf.append(" public void foo(Object str);\n"); + buf.append("}\n"); + ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); + + CompilationUnit astRoot= getASTRoot(cu); + ArrayList proposals= collectCorrections(cu, astRoot); + assertNumberOfProposals(proposals, 2); + assertCorrectLabels(proposals); + + String[] expected= new String[2]; + + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append(" /**\n"); + buf.append(" * @param <X> \n"); + buf.append(" * @see E\n"); + buf.append(" */\n"); + buf.append("public interface E<X> {\n"); + buf.append(" public void foo(Object str);\n"); + buf.append("}\n"); + expected[0]= buf.toString(); + + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append(" /**\n"); + buf.append(" * @param <X> \n"); + buf.append(" * @param <T> \n"); + buf.append(" * @see E\n"); + buf.append(" */\n"); + buf.append("public interface E<X, T> {\n"); + buf.append(" public void foo(Object str);\n"); + buf.append("}\n"); + expected[1]= buf.toString(); + + assertExpectedExistInProposals(proposals, expected); + } + + public void testUnusedTypeParameter3() throws Exception { + Hashtable hashtable= JavaCore.getOptions(); + hashtable.put(JavaCore.COMPILER_PB_UNUSED_TYPE_PARAMETER, JavaCore.ERROR); + JavaCore.setOptions(hashtable); + + IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); + StringBuffer buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("public class E {\n"); + buf.append(" public <T> void foo(Object str){}\n"); + buf.append("}\n"); + ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); + + CompilationUnit astRoot= getASTRoot(cu); + ArrayList proposals= collectCorrections(cu, astRoot); + assertNumberOfProposals(proposals, 2); + assertCorrectLabels(proposals); + + String[] expected= new String[2]; + + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("public class E {\n"); + buf.append(" public void foo(Object str){}\n"); + buf.append("}\n"); + expected[0]= buf.toString(); + + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("public class E {\n"); + buf.append(" /**\n"); + buf.append(" * @param <T> \n"); + buf.append(" */\n"); + buf.append(" public <T> void foo(Object str){}\n"); + buf.append("}\n"); + expected[1]= buf.toString(); + + assertExpectedExistInProposals(proposals, expected); + } + } diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/FixMessages.java b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/FixMessages.java index 1e5659a..a87c8bc 100644 --- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/FixMessages.java +++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/FixMessages.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2011 IBM Corporation and others. + * Copyright (c) 2000, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -72,6 +72,7 @@ public final class FixMessages extends NLS { public static String UnusedCodeFix_RemoveImport_description; public static String UnusedCodeFix_RemoveCast_description; public static String UnusedCodeFix_RemoveUnusedType_description; + public static String UnusedCodeFix_RemoveUnusedTypeParameter_description; public static String UnusedCodeFix_RemoveUnusedConstructor_description; public static String UnusedCodeFix_RemoveUnusedPrivateMethod_description; public static String UnusedCodeFix_RemoveUnusedField_description; diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/FixMessages.properties b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/FixMessages.properties index 2457ae9..0429628 100644 --- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/FixMessages.properties +++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/FixMessages.properties @@ -1,5 +1,5 @@ ############################################################################### -# Copyright (c) 2005, 2012 IBM Corporation and others. +# Copyright (c) 2005, 2013 IBM Corporation and others. # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Public License v1.0 # which accompanies this distribution, and is available at @@ -35,6 +35,7 @@ UnusedCodeFix_RemoveUnusedVariabl_description=Remove unused local variable UnusedCodeFix_RemoveMethod_description=Remove method ''{0}'' UnusedCodeFix_RemoveConstructor_description=Remove constructor ''{0}'' UnusedCodeFix_RemoveUnusedType_description=Remove unused private type +UnusedCodeFix_RemoveUnusedTypeParameter_description=Remove unused type parameter UnusedCodeFix_RemoveUnusedField_description=Remove unused private field UnusedCodeFix_RemoveType_description=Remove type ''{0}'' UnusedCodeFix_RemoveCast_description=Remove cast diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/UnusedCodeFix.java b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/UnusedCodeFix.java index aa0e678..7f76eb5 100644 --- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/UnusedCodeFix.java +++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/UnusedCodeFix.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2012 IBM Corporation and others. + * Copyright (c) 2000, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -150,6 +150,36 @@ public class UnusedCodeFix extends CompilationUnitRewriteOperationsFix { } + /** + * Removes the unused type parameter. + * + */ + private static class RemoveUnusedTypeParameterOperation extends CompilationUnitRewriteOperation { + private final SimpleName fUnusedName; + + public RemoveUnusedTypeParameterOperation(SimpleName unusedName) { + fUnusedName= unusedName; + } + + @Override + public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel linkedModel) throws CoreException { + ASTRewrite rewrite= cuRewrite.getASTRewrite(); + IBinding binding= fUnusedName.resolveBinding(); + CompilationUnit root= (CompilationUnit) fUnusedName.getRoot(); + String displayString= FixMessages.UnusedCodeFix_RemoveUnusedTypeParameter_description; + TextEditGroup group= createTextEditGroup(displayString, cuRewrite); + + if (binding.getKind() == IBinding.TYPE) { + ITypeBinding decl= ((ITypeBinding) binding).getTypeDeclaration(); + ASTNode declaration= root.findDeclaringNode(decl); + if (declaration.getParent() instanceof TypeDeclarationStatement) { + declaration= declaration.getParent(); + } + rewrite.remove(declaration, group); + } + } + } + private static class RemoveUnusedMemberOperation extends CompilationUnitRewriteOperation { private final SimpleName[] fUnusedNames; @@ -548,6 +578,21 @@ public class UnusedCodeFix extends CompilationUnitRewriteOperationsFix { return null; } + public static UnusedCodeFix createUnusedTypeParameterFix(CompilationUnit compilationUnit, IProblemLocation problemLoc) { + if (problemLoc.getProblemId() == IProblem.UnusedTypeParameter) { + SimpleName name= getUnusedName(compilationUnit, problemLoc); + if (name != null) { + IBinding binding= name.resolveBinding(); + if (binding != null) { + String label= FixMessages.UnusedCodeFix_RemoveUnusedTypeParameter_description; + RemoveUnusedTypeParameterOperation operation= new RemoveUnusedTypeParameterOperation(name); + return new UnusedCodeFix(label, compilationUnit, new CompilationUnitRewriteOperation[] { operation }, getCleanUpOptions(binding, false)); + } + } + } + return null; + } + public static boolean isUnusedMember(IProblemLocation problem) { int id= problem.getProblemId(); return id == IProblem.UnusedPrivateMethod || id == IProblem.UnusedPrivateConstructor || id == IProblem.UnusedPrivateField || id == IProblem.UnusedPrivateType diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.java index f93ed83..a83f608 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2012 IBM Corporation and others. + * Copyright (c) 2000, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -28,6 +28,7 @@ public final class CorrectionMessages extends NLS { public static String FixCorrectionProposal_WarningAdditionalProposalInfo; public static String JavadocTagsSubProcessor_document_exception_description; public static String JavadocTagsSubProcessor_document_parameter_description; + public static String JavadocTagsSubProcessor_document_type_parameter_description; public static String LocalCorrectionsSubProcessor_renaming_duplicate_method; public static String LocalCorrectionsSubProcessor_replacefieldaccesswithmethod_description; public static String ModifierCorrectionSubProcessor_addstatic_description; diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.properties b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.properties index 76f1e1c..7c4652d 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.properties +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.properties @@ -1,5 +1,5 @@ ############################################################################### -# Copyright (c) 2000, 2012 IBM Corporation and others. +# Copyright (c) 2000, 2013 IBM Corporation and others. # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Public License v1.0 # which accompanies this distribution, and is available at @@ -248,6 +248,7 @@ JavadocTagsSubProcessor_addjavadoc_field_description=Add Javadoc comment JavadocTagsSubProcessor_addjavadoc_paramtag_description=Add '@param' tag JavadocTagsSubProcessor_document_exception_description=Document thrown exception to avoid 'unused' warning JavadocTagsSubProcessor_document_parameter_description=Document parameter to avoid 'unused' warning +JavadocTagsSubProcessor_document_type_parameter_description=Document type parameter to avoid 'unused' warning JavadocTagsSubProcessor_addjavadoc_throwstag_description=Add '@throws' tag JavadocTagsSubProcessor_addjavadoc_returntag_description=Add '@return' tag JavadocTagsSubProcessor_addjavadoc_enumconst_description=Add Javadoc comment diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/JavadocTagsSubProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/JavadocTagsSubProcessor.java index 5a137c0..77c70e8 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/JavadocTagsSubProcessor.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/JavadocTagsSubProcessor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2012 IBM Corporation and others. + * Copyright (c) 2000, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -126,9 +126,9 @@ public class JavadocTagsSubProcessor { private final BodyDeclaration fBodyDecl; // MethodDecl or TypeDecl private final ASTNode fMissingNode; - public AddMissingJavadocTagProposal(String label, ICompilationUnit cu, BodyDeclaration methodDecl, ASTNode missingNode, int relevance) { + public AddMissingJavadocTagProposal(String label, ICompilationUnit cu, BodyDeclaration bodyDecl, ASTNode missingNode, int relevance) { super(label, cu, null, relevance, JavaPluginImages.get(JavaPluginImages.IMG_OBJS_JAVADOCTAG)); - fBodyDecl= methodDecl; + fBodyDecl= bodyDecl; fMissingNode= missingNode; } @@ -392,7 +392,9 @@ public class JavadocTagsSubProcessor { return; } - boolean isUnusedParam= problem.getProblemId() == IProblem.ArgumentIsNeverUsed; + int problemId= problem.getProblemId(); + boolean isUnusedTypeParam= problemId == IProblem.UnusedTypeParameter; + boolean isUnusedParam= problemId == IProblem.ArgumentIsNeverUsed || isUnusedTypeParam; String key= isUnusedParam ? JavaCore.COMPILER_PB_UNUSED_PARAMETER_INCLUDE_DOC_COMMENT_REFERENCE : JavaCore.COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION_INCLUDE_DOC_COMMENT_REFERENCE; if (!JavaCore.ENABLED.equals(project.getOption(key, true))) { @@ -404,19 +406,21 @@ public class JavadocTagsSubProcessor { return; } - MethodDeclaration methodDecl= (MethodDeclaration) ASTNodes.getParent(node, ASTNode.METHOD_DECLARATION); - if (methodDecl == null || methodDecl.resolveBinding() == null) { + BodyDeclaration bodyDecl= ASTResolving.findParentBodyDeclaration(node); + if (bodyDecl == null || ASTResolving.getParentMethodOrTypeBinding(bodyDecl) == null) { return; } String label; - if (isUnusedParam) { + if (isUnusedTypeParam) { + label= CorrectionMessages.JavadocTagsSubProcessor_document_type_parameter_description; + } else if (isUnusedParam) { label= CorrectionMessages.JavadocTagsSubProcessor_document_parameter_description; } else { label= CorrectionMessages.JavadocTagsSubProcessor_document_exception_description; } - ASTRewriteCorrectionProposal proposal= new AddMissingJavadocTagProposal(label, context.getCompilationUnit(), methodDecl, node, IProposalRelevance.DOCUMENT_UNUSED_ITEM); - proposals.add(proposal); + ASTRewriteCorrectionProposal proposal= new AddMissingJavadocTagProposal(label, context.getCompilationUnit(), bodyDecl, node, IProposalRelevance.DOCUMENT_UNUSED_ITEM); + proposals.add(proposal); } public static void getMissingJavadocCommentProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException { diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java index 55480da..2cf0228 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2012 IBM Corporation and others. + * Copyright (c) 2000, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -661,6 +661,15 @@ public class LocalCorrectionsSubProcessor { } + public static void addUnusedTypeParameterProposal(IInvocationContext context, IProblemLocation problemLoc, Collection<ICommandAccess> proposals) { + UnusedCodeFix fix= UnusedCodeFix.createUnusedTypeParameterFix(context.getASTRoot(), problemLoc); + if (fix != null) { + addProposal(context, proposals, fix); + } + + JavadocTagsSubProcessor.getUnusedAndUndocumentedParameterOrExceptionProposals(context, problemLoc, proposals); + } + public static void addRedundantSuperInterfaceProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) { ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot()); if (!(selectedNode instanceof Name)) { diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickFixProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickFixProcessor.java index 8902295..8c06127 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickFixProcessor.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickFixProcessor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2012 IBM Corporation and others. + * Copyright (c) 2000, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -249,6 +249,7 @@ public class QuickFixProcessor implements IQuickFixProcessor { case IProblem.NonNullLocalVariableComparisonYieldsFalse: case IProblem.RedundantNullCheckOnNonNullLocalVariable: case IProblem.RedundantNullAnnotation: + case IProblem.UnusedTypeParameter: return true; default: return SuppressWarningsSubProcessor.hasSuppressWarningsProposal(cu.getJavaProject(), problemId); @@ -701,6 +702,9 @@ public class QuickFixProcessor implements IQuickFixProcessor { case IProblem.RedundantNullDefaultAnnotationMethod: NullAnnotationsCorrectionProcessor.addRemoveRedundantAnnotationProposal(context, problem, proposals); break; + case IProblem.UnusedTypeParameter: + LocalCorrectionsSubProcessor.addUnusedTypeParameterProposal(context, problem, proposals); + break; default: } if (JavaModelUtil.is50OrHigher(context.getCompilationUnit().getJavaProject())) { |

