diff options
author | Deepak Azad | 2012-07-21 02:03:44 +0000 |
---|---|---|
committer | Deepak Azad | 2012-07-21 02:03:44 +0000 |
commit | 7bb0fd23fd2aab330efc99f68a43869e09fc81c4 (patch) | |
tree | e4f2ac658d23828cddb1d58bd48a8d56de3d3f21 | |
parent | 35d9730f62138a820e6ba6509a54247827c91f77 (diff) | |
download | eclipse.jdt.ui-7bb0fd23fd2aab330efc99f68a43869e09fc81c4.tar.gz eclipse.jdt.ui-7bb0fd23fd2aab330efc99f68a43869e09fc81c4.tar.xz eclipse.jdt.ui-7bb0fd23fd2aab330efc99f68a43869e09fc81c4.zip |
Bug 337977: [quick fix] Add quick fixes for null annotationsv20120721-020344
- Quick fix to remove redundant default nullness annotations
5 files changed, 204 insertions, 25 deletions
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/NullAnnotationsQuickFixTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/NullAnnotationsQuickFixTest.java index def4b8c9cc..8a5fe956bf 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/NullAnnotationsQuickFixTest.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/NullAnnotationsQuickFixTest.java @@ -600,4 +600,160 @@ public class NullAnnotationsQuickFixTest extends QuickFixTest { buf.append("}\n"); assertEqualString(preview, buf.toString()); } + + public void testRemoveRedundantAnnotation4() throws Exception { + IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); + StringBuffer buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("import org.eclipse.jdt.annotation.*;\n"); + buf.append("@NonNullByDefault\n"); + buf.append("public class E {\n"); + buf.append(" @NonNullByDefault\n"); + buf.append(" void foo(Object o) {\n"); + buf.append(" }\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); + CUCorrectionProposal proposal= (CUCorrectionProposal)proposals.get(0); + String preview= getPreviewContent(proposal); + + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("import org.eclipse.jdt.annotation.*;\n"); + buf.append("@NonNullByDefault\n"); + buf.append("public class E {\n"); + buf.append(" void foo(Object o) {\n"); + buf.append(" }\n"); + buf.append("}\n"); + assertEqualString(preview, buf.toString()); + } + + public void testRemoveRedundantAnnotation5() throws Exception { + IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); + StringBuffer buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("import org.eclipse.jdt.annotation.*;\n"); + buf.append("@NonNullByDefault\n"); + buf.append("public class E {\n"); + buf.append(" @NonNullByDefault\n"); + buf.append(" class E1 {\n"); + buf.append(" }\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); + CUCorrectionProposal proposal= (CUCorrectionProposal)proposals.get(0); + String preview= getPreviewContent(proposal); + + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("import org.eclipse.jdt.annotation.*;\n"); + buf.append("@NonNullByDefault\n"); + buf.append("public class E {\n"); + buf.append(" class E1 {\n"); + buf.append(" }\n"); + buf.append("}\n"); + assertEqualString(preview, buf.toString()); + } + + public void testRemoveRedundantAnnotation6() throws Exception { + IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); + StringBuffer buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("import org.eclipse.jdt.annotation.*;\n"); + buf.append("public class E {\n"); + buf.append(" @NonNullByDefault\n"); + buf.append(" void foo(Object o) {\n"); + buf.append(" @NonNullByDefault\n"); + buf.append(" class E1 {\n"); + buf.append(" }\n"); + buf.append(" }\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); + CUCorrectionProposal proposal= (CUCorrectionProposal)proposals.get(0); + String preview= getPreviewContent(proposal); + + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("import org.eclipse.jdt.annotation.*;\n"); + buf.append("public class E {\n"); + buf.append(" @NonNullByDefault\n"); + buf.append(" void foo(Object o) {\n"); + buf.append(" class E1 {\n"); + buf.append(" }\n"); + buf.append(" }\n"); + buf.append("}\n"); + assertEqualString(preview, buf.toString()); + } + + public void testRemoveRedundantAnnotation7() throws Exception { + IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); + StringBuffer buf= new StringBuffer(); + buf.append("@org.eclipse.jdt.annotation.NonNullByDefault\n"); + buf.append("package test1;\n"); + pack1.createCompilationUnit("package-info.java", buf.toString(), false, null); + + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("import org.eclipse.jdt.annotation.*;\n"); + buf.append("@NonNullByDefault\n"); + buf.append("public class E {\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); + CUCorrectionProposal proposal= (CUCorrectionProposal)proposals.get(0); + String preview= getPreviewContent(proposal); + + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("import org.eclipse.jdt.annotation.*;\n"); + buf.append("public class E {\n"); + buf.append("}\n"); + assertEqualString(preview, buf.toString()); + } + + public void testRemoveRedundantAnnotation8() throws Exception { + IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); + StringBuffer buf= new StringBuffer(); + buf.append("@org.eclipse.jdt.annotation.NonNullByDefault\n"); + buf.append("package test1;\n"); + pack1.createCompilationUnit("package-info.java", buf.toString(), false, null); + + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("import org.eclipse.jdt.annotation.*;\n"); + buf.append("public class E {\n"); + buf.append(" @NonNullByDefault\n"); + buf.append(" void foo(Object o) {\n"); + buf.append(" }\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); + CUCorrectionProposal proposal= (CUCorrectionProposal)proposals.get(0); + String preview= getPreviewContent(proposal); + + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("import org.eclipse.jdt.annotation.*;\n"); + buf.append("public class E {\n"); + buf.append(" void foo(Object o) {\n"); + buf.append(" }\n"); + buf.append("}\n"); + assertEqualString(preview, buf.toString()); + } } diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/NullAnnotationsFix.java b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/NullAnnotationsFix.java index a5a04cd3c4..37e70e551b 100644 --- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/NullAnnotationsFix.java +++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/NullAnnotationsFix.java @@ -185,7 +185,9 @@ public class NullAnnotationsFix extends CompilationUnitRewriteOperationsFix { if (problem == null) continue; // problem was filtered out by createCleanUp() - if (problem.getProblemId() == IProblem.RedundantNullAnnotation) { + int problemId= problem.getProblemId(); + if (problemId == IProblem.RedundantNullAnnotation || problemId == IProblem.RedundantNullDefaultAnnotationPackage || problemId == IProblem.RedundantNullDefaultAnnotationType + || problemId == IProblem.RedundantNullDefaultAnnotationMethod) { RemoveRedundantAnnotationRewriteOperation operation= new RemoveRedundantAnnotationRewriteOperation(compilationUnit, problem); result.add(operation); } @@ -223,15 +225,19 @@ public class NullAnnotationsFix extends CompilationUnitRewriteOperationsFix { } public static String getNullableAnnotationName(IJavaElement javaElement, boolean makeSimple) { - String qualifiedName= javaElement.getJavaProject().getOption(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, true); - int lastDot; - if (makeSimple && qualifiedName != null && (lastDot= qualifiedName.lastIndexOf('.')) != -1) - return qualifiedName.substring(lastDot + 1); - return qualifiedName; + return getAnnotationName(javaElement, makeSimple, JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME); } public static String getNonNullAnnotationName(IJavaElement javaElement, boolean makeSimple) { - String qualifiedName= javaElement.getJavaProject().getOption(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, true); + return getAnnotationName(javaElement, makeSimple, JavaCore.COMPILER_NONNULL_ANNOTATION_NAME); + } + + public static String getNonNullByDefaultAnnotationName(IJavaElement javaElement, boolean makeSimple) { + return getAnnotationName(javaElement, makeSimple, JavaCore.COMPILER_NONNULL_BY_DEFAULT_ANNOTATION_NAME); + } + + private static String getAnnotationName(IJavaElement javaElement, boolean makeSimple, String annotation) { + String qualifiedName= javaElement.getJavaProject().getOption(annotation, true); int lastDot; if (makeSimple && qualifiedName != null && (lastDot= qualifiedName.lastIndexOf('.')) != -1) return qualifiedName.substring(lastDot + 1); diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/NullAnnotationsRewriteOperations.java b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/NullAnnotationsRewriteOperations.java index 0156a356d7..adb1fe7b63 100644 --- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/NullAnnotationsRewriteOperations.java +++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/NullAnnotationsRewriteOperations.java @@ -203,27 +203,38 @@ public class NullAnnotationsRewriteOperations { CompilationUnit astRoot= fCompilationUnit; ASTNode selectedNode= fProblem.getCoveringNode(astRoot); - List<IExtendedModifier> modifiers; - if (selectedNode instanceof SingleVariableDeclaration) { - SingleVariableDeclaration singleVariableDeclaration= (SingleVariableDeclaration) selectedNode; - modifiers= singleVariableDeclaration.modifiers(); - } else if (selectedNode instanceof MethodDeclaration) { - MethodDeclaration methodDeclaration= (MethodDeclaration) selectedNode; - modifiers= methodDeclaration.modifiers(); - } else { - return; - } + if (fProblem.getProblemId() == IProblem.RedundantNullAnnotation) { + List<IExtendedModifier> modifiers; + if (selectedNode instanceof SingleVariableDeclaration) { + SingleVariableDeclaration singleVariableDeclaration= (SingleVariableDeclaration) selectedNode; + modifiers= singleVariableDeclaration.modifiers(); + } else if (selectedNode instanceof MethodDeclaration) { + MethodDeclaration methodDeclaration= (MethodDeclaration) selectedNode; + modifiers= methodDeclaration.modifiers(); + } else { + return; + } - for (Iterator<IExtendedModifier> iterator= modifiers.iterator(); iterator.hasNext();) { - IExtendedModifier modifier= iterator.next(); - if (modifier instanceof MarkerAnnotation) { - MarkerAnnotation annotation= (MarkerAnnotation) modifier; - IAnnotationBinding annotationBinding= annotation.resolveAnnotationBinding(); - String name= annotationBinding.getName(); - if (name.equals(NullAnnotationsFix.getNonNullAnnotationName(fCompilationUnit.getJavaElement(), true))) { - astRewrite.remove((ASTNode) modifier, group); + for (Iterator<IExtendedModifier> iterator= modifiers.iterator(); iterator.hasNext();) { + IExtendedModifier modifier= iterator.next(); + if (modifier instanceof MarkerAnnotation) { + MarkerAnnotation annotation= (MarkerAnnotation) modifier; + IAnnotationBinding annotationBinding= annotation.resolveAnnotationBinding(); + String name= annotationBinding.getName(); + if (name.equals(NullAnnotationsFix.getNonNullAnnotationName(fCompilationUnit.getJavaElement(), true))) { + astRewrite.remove(annotation, group); + } } } + } else { + if (!(selectedNode instanceof MarkerAnnotation)) + return; + MarkerAnnotation annotation= (MarkerAnnotation) selectedNode; + IAnnotationBinding annotationBinding= annotation.resolveAnnotationBinding(); + String name= annotationBinding.getName(); + if (name.equals(NullAnnotationsFix.getNonNullByDefaultAnnotationName(fCompilationUnit.getJavaElement(), true))) { + astRewrite.remove(annotation, group); + } } } } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/NullAnnotationsCleanUp.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/NullAnnotationsCleanUp.java index befb36caf8..a08c22d5c9 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/NullAnnotationsCleanUp.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/NullAnnotationsCleanUp.java @@ -114,6 +114,9 @@ public class NullAnnotationsCleanUp extends AbstractMultiFix { result.add(MultiFixMessages.NullAnnotationsCleanUp_add_nonnull_annotation); break; case IProblem.RedundantNullAnnotation: + case IProblem.RedundantNullDefaultAnnotationPackage: + case IProblem.RedundantNullDefaultAnnotationType: + case IProblem.RedundantNullDefaultAnnotationMethod: result.add(MultiFixMessages.NullAnnotationsCleanUp_remove_redundant_nullness_annotation); break; } 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 60a9c81b41..8902295101 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 @@ -696,6 +696,9 @@ public class QuickFixProcessor implements IQuickFixProcessor { NullAnnotationsCorrectionProcessor.addReturnAndArgumentTypeProposal(context, problem, proposals); break; case IProblem.RedundantNullAnnotation: + case IProblem.RedundantNullDefaultAnnotationPackage: + case IProblem.RedundantNullDefaultAnnotationType: + case IProblem.RedundantNullDefaultAnnotationMethod: NullAnnotationsCorrectionProcessor.addRemoveRedundantAnnotationProposal(context, problem, proposals); break; default: |