diff options
author | Markus Keller | 2012-10-25 18:09:44 +0000 |
---|---|---|
committer | Markus Keller | 2015-06-17 19:33:28 +0000 |
commit | a232bdec81b10f942e126db663f7a6e7c79065f1 (patch) | |
tree | 9154107b887c1e027ce12a1c75a25e8b28376405 | |
parent | bb698f95dfb81a9ab6abd0eff4f07c6ad18cf4e7 (diff) | |
download | eclipse.jdt.ui-R3_8_maintenance.tar.gz eclipse.jdt.ui-R3_8_maintenance.tar.xz eclipse.jdt.ui-R3_8_maintenance.zip |
Bug 470426: [override method] JAX RS interface annotations are duplicated in the implementing class (3.8.2+/4.2.2+)R3_8_maintenance
Change-Id: Ia26775f53cf028c859880822945ca662784cdbaa
4 files changed, 57 insertions, 31 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 ce747c1736..5161659537 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 @@ -2998,6 +2998,11 @@ public class LocalCorrectionsQuickFixTest extends QuickFixTest { } public void testUnimplementedMethodsWithAnnotations() throws Exception { + Hashtable hashtable= JavaCore.getOptions(); + hashtable.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "test.NonNull"); + hashtable.put(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, "test.Nullable"); + JavaCore.setOptions(hashtable); + IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null); StringBuffer buf= new StringBuffer(); buf.append("package test;\n"); @@ -3069,9 +3074,8 @@ public class LocalCorrectionsQuickFixTest extends QuickFixTest { buf.append("class B extends A {\n"); buf.append("\n"); buf.append(" @Override\n"); - buf.append(" @Sour(value = {-42, 13}, c = Integer[][].class, name = \"@hi\", policy = RetentionPolicy.CLASS, d = @Deprecated)\n"); buf.append(" public @NonNull\n"); - buf.append(" Object foo(@Sour(12) @Nullable Object input) {\n"); + buf.append(" Object foo(@Nullable Object input) {\n"); buf.append(" return null;\n"); buf.append(" }\n"); buf.append("}\n"); diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/StubUtility2.java b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/StubUtility2.java index 03cd56f2d9..85f72ddcaf 100644 --- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/StubUtility2.java +++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/StubUtility2.java @@ -503,7 +503,7 @@ public final class StubUtility2 { var.setName(ast.newSimpleName(paramNames[i])); IAnnotationBinding[] annotations= binding.getParameterAnnotations(i); for (IAnnotationBinding annotation : annotations) { - if (Bindings.isClassOrRuntimeAnnotation(annotation.getAnnotationType())) + if (StubUtility2.isCopyOnInheritAnnotation(annotation.getAnnotationType(), project)) var.modifiers().add(ASTNodeFactory.newAnnotation(ast, annotation, imports, context)); } parameters.add(var); @@ -626,6 +626,7 @@ public final class StubUtility2 { } private static List<IExtendedModifier> getImplementationModifiers(AST ast, IMethodBinding method, boolean inInterface, ImportRewrite importRewrite, ImportRewriteContext context) throws JavaModelException { + IJavaProject javaProject= importRewrite.getCompilationUnit().getJavaProject(); int modifiers= method.getModifiers() & ~Modifier.ABSTRACT & ~Modifier.NATIVE & ~Modifier.PRIVATE; if (inInterface) { modifiers= modifiers & ~Modifier.PROTECTED; @@ -659,7 +660,7 @@ public final class StubUtility2 { ITypeBinding otherAnnotationType= annotation.getAnnotationType(); String qn= otherAnnotationType.getQualifiedName(); if (qn.endsWith(n) && (qn.length() == n.length() || qn.charAt(qn.length() - n.length() - 1) == '.')) { - if (Bindings.isClassOrRuntimeAnnotation(otherAnnotationType)) + if (StubUtility2.isCopyOnInheritAnnotation(otherAnnotationType, javaProject)) result.add(ASTNodeFactory.newAnnotation(ast, annotation, importRewrite, context)); break; } @@ -674,7 +675,7 @@ public final class StubUtility2 { ArrayList<IExtendedModifier> result= new ArrayList<IExtendedModifier>(); for (IAnnotationBinding annotation : annotations) { - if (Bindings.isClassOrRuntimeAnnotation(annotation.getAnnotationType())) + if (StubUtility2.isCopyOnInheritAnnotation(annotation.getAnnotationType(), javaProject)) result.add(ASTNodeFactory.newAnnotation(ast, annotation, importRewrite, context)); } @@ -877,4 +878,12 @@ public final class StubUtility2 { private StubUtility2() { // Not for instantiation } + + public static boolean isCopyOnInheritAnnotation(ITypeBinding annotationType, IJavaProject project) { + // not supported in 3.8: +// if (JavaCore.ENABLED.equals(project.getOption(JavaCore.COMPILER_INHERIT_NULL_ANNOTATIONS, true))) +// return false; + + return Bindings.isNullAnnotation(annotationType, project); + } } diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/dom/Bindings.java b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/dom/Bindings.java index 1782f2bd48..bd2bcc04b8 100644 --- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/dom/Bindings.java +++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/dom/Bindings.java @@ -26,6 +26,7 @@ import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IMethod; import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.Signature; import org.eclipse.jdt.core.dom.AST; @@ -40,9 +41,7 @@ import org.eclipse.jdt.core.dom.ClassInstanceCreation; import org.eclipse.jdt.core.dom.EnumDeclaration; import org.eclipse.jdt.core.dom.Expression; import org.eclipse.jdt.core.dom.FieldAccess; -import org.eclipse.jdt.core.dom.IAnnotationBinding; import org.eclipse.jdt.core.dom.IBinding; -import org.eclipse.jdt.core.dom.IMemberValuePairBinding; import org.eclipse.jdt.core.dom.IMethodBinding; import org.eclipse.jdt.core.dom.IPackageBinding; import org.eclipse.jdt.core.dom.ITypeBinding; @@ -1427,22 +1426,10 @@ public class Bindings { } } - public static boolean isClassOrRuntimeAnnotation(ITypeBinding annotationType) { - IAnnotationBinding[] metaAnnotations= annotationType.getAnnotations(); - for (IAnnotationBinding metaAnnotation : metaAnnotations) { - if ("java.lang.annotation.Retention".equals(metaAnnotation.getAnnotationType().getQualifiedName())) { //$NON-NLS-1$ - IMemberValuePairBinding[] mvps= metaAnnotation.getDeclaredMemberValuePairs(); - if (mvps.length == 1) { - Object value= mvps[0].getValue(); - if (value instanceof IVariableBinding) { - String name= ((IVariableBinding) value).getName(); - if ("RUNTIME".equals(name) || "CLASS".equals(name)) //$NON-NLS-1$ //$NON-NLS-2$ - return true; - } - } - } - } - return false; + public static boolean isNullAnnotation(ITypeBinding annotationType, IJavaProject project) { + String qualifiedName= annotationType.getQualifiedName(); + return qualifiedName.equals(project.getOption(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, true)) + || qualifiedName.equals(project.getOption(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, true)); } } diff --git a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/structure/ExtractInterfaceProcessor.java b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/structure/ExtractInterfaceProcessor.java index 8d2050b1b9..4cb8b6048f 100644 --- a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/structure/ExtractInterfaceProcessor.java +++ b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/structure/ExtractInterfaceProcessor.java @@ -431,7 +431,7 @@ public final class ExtractInterfaceProcessor extends SuperTypeRefactoringProcess final Set<String> replacements= new HashSet<String>(); if (fReplace) rewriteTypeOccurrences(manager, sourceRewrite, copy, replacements, status, new SubProgressMonitor(monitor, 220)); - createMethodComments(sourceRewrite, replacements); + rewriteSourceMethods(sourceRewrite, replacements); manager.manage(fSubType.getCompilationUnit(), sourceRewrite.createChange(true)); } return manager; @@ -583,8 +583,8 @@ public final class ExtractInterfaceProcessor extends SuperTypeRefactoringProcess } /** - * Creates the method annotations and comments of the extracted methods in - * the source type. + * Rewrites the extracted methods in the source type. + * Adds/removes method annotations and adds method comments. * * @param sourceRewrite * the source compilation unit rewrite @@ -594,18 +594,43 @@ public final class ExtractInterfaceProcessor extends SuperTypeRefactoringProcess * @throws CoreException * if an error occurs */ - protected final void createMethodComments(final CompilationUnitRewrite sourceRewrite, final Set<String> replacements) throws CoreException { + private void rewriteSourceMethods(final CompilationUnitRewrite sourceRewrite, final Set<String> replacements) throws CoreException { Assert.isNotNull(sourceRewrite); Assert.isNotNull(replacements); - if (fMembers.length > 0 && (fAnnotations || fComments)) { + if (fMembers.length > 0) { IJavaProject project= fSubType.getJavaProject(); boolean annotations= fAnnotations && !JavaModelUtil.isVersionLessThan(project.getOption(JavaCore.COMPILER_SOURCE, true), JavaCore.VERSION_1_6); + boolean inheritNullAnnotations= false; // not supported in 3.8: JavaCore.ENABLED.equals(project.getOption(JavaCore.COMPILER_INHERIT_NULL_ANNOTATIONS, true)); boolean javadoc= project.getOption(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, true).equals(JavaCore.ENABLED); IMember member= null; for (int index= 0; index < fMembers.length; index++) { member= fMembers[index]; if (member instanceof IMethod) { MethodDeclaration declaration= ASTNodeSearchUtil.getMethodDeclarationNode((IMethod) member, sourceRewrite.getRoot()); + if (inheritNullAnnotations) { + for (IExtendedModifier extended : (List<IExtendedModifier>) declaration.modifiers()) { + if (extended.isAnnotation()) { + Annotation annotation= (Annotation) extended; + ITypeBinding binding= annotation.resolveTypeBinding(); + if (binding != null && Bindings.isNullAnnotation(binding, project)) { + ASTRewrite rewrite= sourceRewrite.getASTRewrite(); + rewrite.getListRewrite(declaration, MethodDeclaration.MODIFIERS2_PROPERTY).remove(annotation, null); + } + } + } + for (SingleVariableDeclaration parameter : (List<SingleVariableDeclaration>) declaration.parameters()) { + for (IExtendedModifier extended : (List<IExtendedModifier>) parameter.modifiers()) { + if (extended.isAnnotation()) { + Annotation annotation= (Annotation) extended; + ITypeBinding binding= annotation.resolveTypeBinding(); + if (binding != null && Bindings.isNullAnnotation(binding, project)) { + ASTRewrite rewrite= sourceRewrite.getASTRewrite(); + rewrite.getListRewrite(parameter, SingleVariableDeclaration.MODIFIERS2_PROPERTY).remove(annotation, null); + } + } + } + } + } if (annotations) { ASTRewrite rewrite= sourceRewrite.getASTRewrite(); AST ast= rewrite.getAST(); @@ -639,7 +664,8 @@ public final class ExtractInterfaceProcessor extends SuperTypeRefactoringProcess Assert.isNotNull(sourceRewrite); Assert.isNotNull(targetRewrite); Assert.isNotNull(declaration); - ImportRewriteUtil.collectImports(fSubType.getJavaProject(), declaration, fTypeBindings, fStaticBindings, true); + IJavaProject sourceProject= fSubType.getJavaProject(); + ImportRewriteUtil.collectImports(sourceProject, declaration, fTypeBindings, fStaticBindings, true); ASTRewrite rewrite= ASTRewrite.create(declaration.getAST()); ITrackedNodePosition position= rewrite.track(declaration); if (declaration.getBody() != null) @@ -663,7 +689,7 @@ public final class ExtractInterfaceProcessor extends SuperTypeRefactoringProcess } else if (extended.isAnnotation()) { annotation= (Annotation) extended; ITypeBinding binding= annotation.resolveTypeBinding(); - if (binding.getQualifiedName().equals("java.lang.Override") || ! Bindings.isClassOrRuntimeAnnotation(binding)) //$NON-NLS-1$ + if (binding != null && binding.getQualifiedName().equals("java.lang.Override") || ! Bindings.isNullAnnotation(binding, sourceProject)) //$NON-NLS-1$ list.remove(annotation, null); } } @@ -682,7 +708,7 @@ public final class ExtractInterfaceProcessor extends SuperTypeRefactoringProcess } else if (extended.isAnnotation()) { annotation= (Annotation) extended; ITypeBinding binding= annotation.resolveTypeBinding(); - if (! Bindings.isClassOrRuntimeAnnotation(binding)) + if (binding != null && ! Bindings.isNullAnnotation(binding, sourceProject)) modifierRewrite.remove(annotation, null); } } |