Bug 431810 - Annotations on constructors are always interpreted as type
use annotations
Signed-off-by: shankha banerjee <shankhba@in.ibm.com>
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/TypeBindingTests308.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/TypeBindingTests308.java
index be19863..6a0ed06 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/TypeBindingTests308.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/TypeBindingTests308.java
@@ -2479,4 +2479,93 @@
deleteFile("/Converter18/src/X.java");
}
}
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=431810
+ public void testBug431810() throws Exception {
+ try {
+ String contents =
+ "import java.lang.annotation.ElementType; \n" +
+ "import java.lang.annotation.Target; \n" +
+ "@interface A {}\n" +
+ "@Target(ElementType.TYPE_USE)\n" +
+ "@interface B {} \n" +
+ "class X {\n" +
+ " @A \n" +
+ " X() {}\n" +
+ " @B \n" +
+ " X(int x) {}\n" +
+ "}\n";
+
+ this.workingCopy = getWorkingCopy("/Converter18/src/X.java", true/*resolve*/);
+ ASTNode node = buildAST(contents, this.workingCopy, false);
+ assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
+ CompilationUnit compilationUnit = (CompilationUnit) node;
+ assertProblemsSize(compilationUnit, 0);
+
+ List types = compilationUnit.types();
+ TypeDeclaration typeDecl = (TypeDeclaration) types.get(2);
+
+ // X()
+ MethodDeclaration method = typeDecl.getMethods()[0];
+ assertTrue("Should be a constructor", method.isConstructor());
+ IMethodBinding methodBinding = method.resolveBinding();
+ IAnnotationBinding[] annots = methodBinding.getAnnotations();
+
+ assertEquals("Incorrect no of annotations", 1, annots.length);
+ assertEquals("Incorrect annotations attached","@A()", annots[0].toString());
+
+ // X(int)
+ method = typeDecl.getMethods()[1];
+ assertTrue("Should be a constructor", method.isConstructor());
+ methodBinding = method.resolveBinding();
+ annots = methodBinding.getAnnotations();
+
+ assertEquals("Incorrect no of annotations", 0, annots.length);
+ } finally {
+ deleteFile("/Converter18/src/X.java");
+ }
+ }
+
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=431810
+ // Incorrect use of annotations on constructors
+ public void testBug431810a() throws Exception {
+ try {
+ String contents =
+ "import java.lang.annotation.ElementType; \n" +
+ "import java.lang.annotation.Target; \n" +
+ "@Target({}) \n" +
+ "@interface A {} \n" +
+ "@Target(ElementType.TYPE)\n" +
+ "@interface B {} \n" +
+ "class X {\n" +
+ " @A \n" +
+ " X() {}\n" +
+ " @B \n" +
+ " X(int x) {}\n" +
+ "}\n";
+
+ this.workingCopy = getWorkingCopy("/Converter18/src/X.java", true/*resolve*/);
+ ASTNode node = buildAST(contents, this.workingCopy, false);
+ assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
+ CompilationUnit compilationUnit = (CompilationUnit) node;
+
+ List types = compilationUnit.types();
+ TypeDeclaration typeDecl = (TypeDeclaration) types.get(2);
+
+ // X()
+ MethodDeclaration method = typeDecl.getMethods()[0];
+ assertTrue("Should be a constructor", method.isConstructor());
+ IMethodBinding methodBinding = method.resolveBinding();
+ IAnnotationBinding[] annots = methodBinding.getAnnotations();
+ assertEquals("Incorrect no of annotations", 0, annots.length);
+
+ // X(int)
+ method = typeDecl.getMethods()[1];
+ assertTrue("Should be a constructor", method.isConstructor());
+ methodBinding = method.resolveBinding();
+ annots = methodBinding.getAnnotations();
+ assertEquals("Incorrect no of annotations", 0, annots.length);
+ } finally {
+ deleteFile("/Converter18/src/X.java");
+ }
+ }
}
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/MethodBinding.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/MethodBinding.java
index c2324da..3a0f5d5 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/MethodBinding.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/MethodBinding.java
@@ -205,8 +205,11 @@
final ReferenceBinding annotationType = internalAnnotation.getAnnotationType();
long metaTagBits = annotationType.getAnnotationTagBits();
- if (isConstructor && (metaTagBits & TagBits.AnnotationForConstructor) == 0)
- continue; // must be type use.
+ // Exclude all other targets including TYPE_USE, even though TYPE_USE is accepted.
+ if (isConstructor && (metaTagBits & TagBits.AnnotationForConstructor) == 0 &&
+ ((metaTagBits & TagBits.AnnotationTargetMASK) != 0)) {
+ continue;
+ }
final IAnnotationBinding annotationInstance = this.resolver.getAnnotationInstance(internalAnnotation);
if (annotationInstance == null) {