diff options
| author | Manoj Palat | 2013-02-06 11:46:21 +0000 |
|---|---|---|
| committer | ssankaran | 2013-02-06 11:46:21 +0000 |
| commit | ee2bef98c3349fd0bb45c7d7646779b8eeb7f5da (patch) | |
| tree | a86a95ce65deea476b7b2aacd6c3ca5e2b7bdddf | |
| parent | de459e09ccca6c2f4f843cc26e4ef2324f96a78f (diff) | |
| download | eclipse.jdt.core-ee2bef98c3349fd0bb45c7d7646779b8eeb7f5da.tar.gz eclipse.jdt.core-ee2bef98c3349fd0bb45c7d7646779b8eeb7f5da.tar.xz eclipse.jdt.core-ee2bef98c3349fd0bb45c7d7646779b8eeb7f5da.zip | |
Fixed Bug 399768 - [1.8] DOM AST Type Annotation Tests to be added.
3 files changed, 455 insertions, 0 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter18Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter18Test.java index 6030a9d019..b90caf3b7f 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter18Test.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter18Test.java @@ -1246,4 +1246,280 @@ public class ASTConverter18Test extends ConverterTestSetup { assertEquals("Incorrect type", true, type.isArrayType()); assertEquals("Type should be malformed", ASTNode.MALFORMED, (type.getFlags() & ASTNode.MALFORMED)); } + /** + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=399768 + * + * @throws JavaModelException + */ + public void test0013() throws JavaModelException { + this.workingCopy = getWorkingCopy("/Converter18/src/test0010/X.java", + true/* resolve */); + String contents = "package test0010;" + + "import java.lang.annotation.Target;\n" + + "public class X implements One<@Marker1 Integer, @Marker2 Boolean> {\n" + + "}\n" + + "class Y implements One<@Marker1 @Marker2 Integer, @Marker2 @Marker1 Double> {\n" + + "}\n" + + "interface One<T, U> {}\n" + + "@Target (java.lang.annotation.ElementType.TYPE_USE)\n" + + "@interface Marker1 {}\n" + + "@Target (java.lang.annotation.ElementType.TYPE_USE)\n" + + "@interface Marker2 {}\n"; + CompilationUnit cu = (CompilationUnit) buildAST(contents, this.workingCopy); + // simple types for generic type arguments to parameterized classes + TypeDeclaration typedeclaration = (TypeDeclaration) getASTNode(cu, 0); + Type type = (Type)((ParameterizedType) typedeclaration.superInterfaceTypes().get(0)).typeArguments().get(0); + assertTrue(type.isSimpleType()); + SimpleType simpleType = (SimpleType) type; + List annotations = simpleType.annotations(); + assertEquals("wrong number of annotations", 1, annotations.size()); + assertEquals("@Marker1", annotations.get(0).toString()); + assertNotNull("No annotation", type); + typedeclaration = (TypeDeclaration) getASTNode(cu, 1); + type = (Type)((ParameterizedType) typedeclaration.superInterfaceTypes().get(0)).typeArguments().get(0); + assertTrue(type.isSimpleType()); + simpleType = (SimpleType) type; + annotations = simpleType.annotations(); + assertEquals("wrong number of annotations", 2, annotations.size()); + assertEquals("@Marker2", annotations.get(1).toString()); + assertNotNull("No annotation", type); + type = (Type)((ParameterizedType) typedeclaration.superInterfaceTypes().get(0)).typeArguments().get(1); + assertTrue(type.isSimpleType()); + simpleType = (SimpleType) type; + annotations = simpleType.annotations(); + assertEquals("wrong number of annotations", 2, annotations.size()); + assertEquals("@Marker1", annotations.get(1).toString()); + assertNotNull("No annotation", type); + } + + /** + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=399768 + * + * @throws JavaModelException + */ + public void test0014() throws JavaModelException { + this.workingCopy = getWorkingCopy("/Converter18/src/test0011/X.java", + true/* resolve */); + String contents = "package test0011;" + + "import java.lang.annotation.Target;\n" + + "public class X {\n" + + " public void foo() {\n" + + " Y y = new <@Marker2 @Marker1 String> Y(new String(\"Hello\"));\n" + + " len = y.<@Marker1 @Marker2 String> bar(new String(\"World\"));\n" + + " }\n" + + " public int len;\n" + + "}\n" + + "class Y {\n" + + " public <T> Y(T t) {\n" + + " len = t instanceof String ? ((String)t).length() : 0;\n" + + " }\n" + + " public <T> int bar(T t) {\n" + + " return t instanceof String ? ((String)t).length() : len;\n" + + " }\n" + + " private int len;\n" + + "}\n" + + "@Target (java.lang.annotation.ElementType.TYPE_USE)\n" + + "@interface Marker1 {}\n" + + "@Target (java.lang.annotation.ElementType.TYPE_USE)\n" + + "@interface Marker2 {}\n"; + CompilationUnit cu = (CompilationUnit) buildAST(contents, this.workingCopy); + // simple tye for generic type arguments in a generic method or constructor invocation + MethodDeclaration methodDeclaration = (MethodDeclaration) getASTNode(cu, 0, 0); + List statements = methodDeclaration.getBody().statements(); + Statement statement = (Statement)statements.get(0); + VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement) statement; + VariableDeclarationFragment variableDeclarationFragment = (VariableDeclarationFragment) variableDeclarationStatement.fragments().get(0); + Expression expression = variableDeclarationFragment.getInitializer(); + ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) expression; + Type type = (Type) classInstanceCreation.typeArguments().get(0); + assertEquals("@Marker2 @Marker1 String", type.toString()); + assertTrue(type.isSimpleType()); + SimpleType simpleType = (SimpleType) type; + List annotations = simpleType.annotations(); + assertEquals("wrong number of annotations", 2, annotations.size()); + assertEquals("@Marker2", annotations.get(0).toString()); + statement = (Statement) statements.get(1); + Assignment assignment = (Assignment) ((ExpressionStatement)statement).getExpression(); + expression = assignment.getRightHandSide(); + MethodInvocation methodInvocation = (MethodInvocation) expression; + simpleType = (SimpleType)methodInvocation.typeArguments().get(0); + annotations = simpleType.annotations(); + assertEquals("wrong number of annotations", 2, annotations.size()); + assertEquals("@Marker1", annotations.get(0).toString()); + } + + /** + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=399768 + * + * @throws JavaModelException + */ + public void test0015() throws JavaModelException { + this.workingCopy = getWorkingCopy("/Converter18/src/test0012/X.java", + true/* resolve */); + String contents = "package test0012;" + + "import java.lang.annotation.Target;\n" + + "import java.io.File;\n" + + "public class X <@Marker1 @Marker3 F extends @Marker1 @Marker2 File> {\n" + + " public int foo(F f) {\n" + + " Y <@Marker2 @Marker3 ? super @Marker1 @Marker2 File> y = new @Marker2 @Marker1 Y<File>();\n" + + " Outer o = new @Marker1 @Marker2 Outer();\n" + + " Outer.Inner inner = o.new @Marker1 @Marker2 Inner();\n" + + " ZZ zz = new <String> @Marker1 @Marker2 ZZ();\n" + + " return f.getName().length() + y.hashCode() + inner.hashCode();\n" + + " }\n" + + "}\n" + + "class Y<@Marker3 T> {\n" + + " public int bar(T t) {\n" + + " return t instanceof @Marker1 @Marker2 File ? t.toString().length() : 0;\n" + + " }\n" + + "}\n" + + "class Outer {\n" + + " public class Inner {\n" + + " public class Deeper {\n" + + " }\n" + + " }\n" + + "}\n" + + "class ZZ {\n" + + "public @Marker1 @Marker2 <T> ZZ() {\n" + + " T t = null;\n" + + " len = t instanceof String ? t.hashCode() : 0;\n" + + "}\n" + + "public @Marker1 int getint(@Marker2 @Marker1 ZZ this) {return len;}\n" + + "public int len;\n" + + "}\n" + + "@Target (java.lang.annotation.ElementType.TYPE_USE)\n" + + "@interface Marker1 {}\n" + + "@Target (java.lang.annotation.ElementType.TYPE_USE)\n" + + "@interface Marker2 {}\n" + + "@Target (java.lang.annotation.ElementType.TYPE_PARAMETER)\n" + + "@interface Marker3 {}\n"; + CompilationUnit cu = (CompilationUnit) buildAST(contents, this.workingCopy); + + TypeDeclaration typedeclaration = (TypeDeclaration) getASTNode(cu, 0); + TypeParameter typeParameter = (TypeParameter) typedeclaration.typeParameters().get(0); + + // TypeParameter with TYPE_USE and TYPE_PARAMETER annotation combination. + assertEquals("@Marker1 @Marker3 F extends @Marker1 @Marker2 File", typeParameter.toString()); + assertTrue(typeParameter.annotations().size() == 2); + Annotation annotation = (Annotation)typeParameter.annotations().get(1); + assertEquals("@Marker3", annotation.toString()); + IAnnotationBinding abinding = annotation.resolveAnnotationBinding(); + assertEquals("@Marker3()", abinding.toString()); + + // simpletype for type parameter bounds + SimpleType simpleType = (SimpleType) typeParameter.typeBounds().get(0); + annotation = (Annotation) simpleType.annotations().get(1); + assertEquals("@Marker2", annotation.toString()); + abinding = annotation.resolveAnnotationBinding(); + assertEquals("@Marker2()", abinding.toString()); + + MethodDeclaration methodDeclaration = (MethodDeclaration) getASTNode(cu, 0, 0); + List statements = methodDeclaration.getBody().statements(); + Statement statement = (Statement)statements.get(0); + VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement)statement; + Type type = variableDeclarationStatement.getType(); + assertTrue(type.isParameterizedType()); + type = (Type)((ParameterizedType)type).typeArguments().get(0); + assertTrue(type.isWildcardType()); + + // for constructor invocation results 1/4 + VariableDeclarationFragment fragment = (VariableDeclarationFragment) variableDeclarationStatement.fragments().get(0); + Expression expression = fragment.getInitializer(); + assertEquals("new @Marker2 @Marker1 Y<File>()", expression.toString()); + simpleType = (SimpleType) ((ParameterizedType)((ClassInstanceCreation)expression).getType()).getType(); + assertEquals("@Marker2 @Marker1 Y", simpleType.toString()); + annotation = (Annotation) simpleType.annotations().get(1); + assertEquals("@Marker1", annotation.toString()); + abinding = annotation.resolveAnnotationBinding(); + assertEquals("@Marker1()", abinding.toString()); + + // for constructor invocation results 2/4 + variableDeclarationStatement = (VariableDeclarationStatement) statements.get(1); + fragment = (VariableDeclarationFragment) variableDeclarationStatement.fragments().get(0); + expression = fragment.getInitializer(); + assertEquals("new @Marker1 @Marker2 Outer()", expression.toString()); + simpleType = (SimpleType) ((ClassInstanceCreation)expression).getType(); + assertEquals("@Marker1 @Marker2 Outer", simpleType.toString()); + annotation = (Annotation) simpleType.annotations().get(1); + assertEquals("@Marker2", annotation.toString()); + abinding = annotation.resolveAnnotationBinding(); + assertEquals("@Marker2()", abinding.toString()); + + // for constructor invocation results 3/4 + variableDeclarationStatement = (VariableDeclarationStatement) statements.get(2); + fragment = (VariableDeclarationFragment) variableDeclarationStatement.fragments().get(0); + expression = fragment.getInitializer(); + assertEquals("o.new @Marker1 @Marker2 Inner()", expression.toString()); + simpleType = (SimpleType) ((ClassInstanceCreation)expression).getType(); + assertEquals("@Marker1 @Marker2 Inner", simpleType.toString()); + annotation = (Annotation) simpleType.annotations().get(1); + assertEquals("@Marker2", annotation.toString()); + abinding = annotation.resolveAnnotationBinding(); + assertEquals("@Marker2()", abinding.toString()); + + // for constructor invocation results 4/4 + variableDeclarationStatement = (VariableDeclarationStatement) statements.get(3); + fragment = (VariableDeclarationFragment) variableDeclarationStatement.fragments().get(0); + expression = fragment.getInitializer(); + assertEquals("new <String>@Marker1 @Marker2 ZZ()", expression.toString()); + simpleType = (SimpleType) ((ClassInstanceCreation)expression).getType(); + assertEquals("@Marker1 @Marker2 ZZ", simpleType.toString()); + annotation = (Annotation) simpleType.annotations().get(1); + assertEquals("@Marker2", annotation.toString()); + abinding = annotation.resolveAnnotationBinding(); + assertEquals("@Marker2()", abinding.toString()); + + // annotations on wildcardtypes with TYPE_USE and TYPE_PARAMETER combination. + WildcardType wildCardType = (WildcardType) type; + assertEquals("@Marker2 @Marker3 ? super @Marker1 @Marker2 File", wildCardType.toString()); + assertTrue(wildCardType.annotations().size() == 2); + annotation = (Annotation) wildCardType.annotations().get(1); + assertEquals("@Marker3", annotation.toString()); + abinding = annotation.resolveAnnotationBinding(); + assertEquals("@Marker3()", abinding.toString()); + + // simpleType for wildcard bounds + simpleType = (SimpleType) wildCardType.getBound(); + annotation = (Annotation) simpleType.annotations().get(1); + assertEquals("@Marker2", annotation.toString()); + abinding = annotation.resolveAnnotationBinding(); + assertEquals("@Marker2()", abinding.toString()); + + // class declaration with TYPE_PARAMETER annotation + typedeclaration = (TypeDeclaration) getASTNode(cu, 1); + typeParameter = (TypeParameter) typedeclaration.typeParameters().get(0); + assertEquals("@Marker3 T", typeParameter.toString()); + assertTrue(typeParameter.annotations().size() == 1); + annotation = (Annotation)typeParameter.annotations().get(0); + assertEquals("@Marker3", annotation.toString()); + abinding = annotation.resolveAnnotationBinding(); + assertEquals("@Marker3()", abinding.toString()); + + // for type tests + methodDeclaration = (MethodDeclaration) getASTNode(cu, 1, 0); + statements = methodDeclaration.getBody().statements(); + statement = (Statement)statements.get(0); + ConditionalExpression conditionalExpression = (ConditionalExpression)((ReturnStatement)statement).getExpression(); + simpleType = (SimpleType) ((InstanceofExpression)conditionalExpression.getExpression()).getRightOperand(); + assertEquals("@Marker1 @Marker2 File", simpleType.toString()); + assertTrue(simpleType.annotations().size() == 2); + annotation = (Annotation) simpleType.annotations().get(1); + assertEquals("@Marker2", annotation.toString()); + + // type annotation in front of a constructor declaration + methodDeclaration = (MethodDeclaration) getASTNode(cu, 3, 0); + annotation = (Annotation) methodDeclaration.modifiers().get(2); + assertEquals("@Marker2", annotation.toString()); + abinding = annotation.resolveAnnotationBinding(); + assertEquals("@Marker2()", abinding.toString()); + + // type annotation on "this" + methodDeclaration = (MethodDeclaration) getASTNode(cu, 3, 1); + simpleType = (SimpleType) methodDeclaration.getReceiverType(); + assertEquals("@Marker2 @Marker1 ZZ", simpleType.toString()); + annotation = (Annotation) simpleType.annotations().get(1); + assertEquals("@Marker1", annotation.toString()); + abinding = annotation.resolveAnnotationBinding(); + assertEquals("@Marker1()", abinding.toString()); + } } diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTMatcherTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTMatcherTest.java index 1d41acb272..e5311ddc4c 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTMatcherTest.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTMatcherTest.java @@ -545,6 +545,12 @@ public class ASTMatcherTest extends org.eclipse.jdt.core.tests.junit.extension.T public boolean match(WildcardType node, Object other) { return standardBody(node, other, this.superMatch ? super.match(node, other) : false); } + public boolean match(ExtraDimension node, Object other) { + return standardBody(node, other, this.superMatch ? super.match(node, other) : false); + } + public boolean match(InstanceofExpression node, Object other) { + return standardBody(node, other, this.superMatch ? super.match(node, other) : false); + } } /** @@ -1385,4 +1391,176 @@ public class ASTMatcherTest extends org.eclipse.jdt.core.tests.junit.extension.T x1.typeArguments().add(simpleType); basicMatch(x1); } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=399768 + public void testTypeAnnotations1() { + if (this.ast.apiLevel() < AST.JLS8) { + return; + } + // simpleType with Annotations + SimpleType x1 = this.ast.newSimpleType(this.N1); + x1.annotations().add(this.ANO1); + basicMatch(x1); + } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=399768 + public void testTypeAnnotations2() { + if (this.ast.apiLevel() < AST.JLS8) { + return; + } + // Type arguments at parameterized types + SimpleType x1 = this.ast.newSimpleType(this.N2); + ParameterizedType x2 = this.ast.newParameterizedType(x1); + x1 = this.ast.newSimpleType(this.ast.newSimpleName("SN1")); + x1.annotations().add(this.ANO1); + x1.annotations().add(this.ANO2); + x2.typeArguments().add(x1); + basicMatch(x2); + } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=399768 + public void testTypeAnnotations3() { + if (this.ast.apiLevel() < AST.JLS8) { + return; + } + // type arguments in constructor invocation + ConstructorInvocation x1 = this.ast.newConstructorInvocation(); + SimpleType x2 = this.ast.newSimpleType(this.N1); + x2.annotations().add(this.ANO1); + x1.typeArguments().add(x2); + basicMatch(x1); + } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=399768 + public void testTypeAnnotations4() { + if (this.ast.apiLevel() < AST.JLS8) { + return; + } + // annotated simple type at class inheritance + TypeDeclaration x1 = this.ast.newTypeDeclaration(); + SimpleType x2 = this.ast.newSimpleType(this.N1); + x2.annotations().add(this.ANO1); + x1.setSuperclassType(x2); + x1.setName(this.N2); + basicMatch(x1); + } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=399768 + public void testTypeAnnotations5() { + if (this.ast.apiLevel() < AST.JLS8) { + return; + } + // constructor invocation results + ClassInstanceCreation x1 = this.ast.newClassInstanceCreation(); + SimpleType x2 = this.ast.newSimpleType(this.N1); + x2.annotations().add(this.ANO1); + x1.setType(x2); + basicMatch(x1); + } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=399768 + public void testTypeAnnotations6() { + if (this.ast.apiLevel() < AST.JLS8) { + return; + } + // simple type in cast expression + CastExpression x1 = this.ast.newCastExpression(); + SimpleType x2 = this.ast.newSimpleType(this.N1); + x2.annotations().add(this.ANO1); + x1.setType(x2); + basicMatch(x1); + } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=399768 + public void testTypeAnnotations7() { + if (this.ast.apiLevel() < AST.JLS8) { + return; + } + // simple type type tests + InstanceofExpression x1 = this.ast.newInstanceofExpression(); + SimpleType x2 = this.ast.newSimpleType(this.N1); + x2.annotations().add(this.ANO1); + x1.setRightOperand(x2); + x1.setLeftOperand(this.E1); + basicMatch(x1); + } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=399768 + public void testTypeAnnotations8() { + if (this.ast.apiLevel() < AST.JLS8) { + return; + } + // annotations on wildcard type + WildcardType x1 = this.ast.newWildcardType(); + SimpleType x2 = this.ast.newSimpleType(this.N1); + x1.setBound(x2); + x1.annotations().add(this.ANO1); + basicMatch(x1); + } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=399768 + public void testTypeAnnotations9() { + if (this.ast.apiLevel() < AST.JLS8) { + return; + } + // annotations on constructor declaration - implying on the object returned. + MethodDeclaration x1 = this.ast.newMethodDeclaration(); + x1.setConstructor(true); + x1.setName(this.N1); + x1.modifiers().add(this.ANO1); + basicMatch(x1); + } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=399768 + public void testTypeAnnotations10() { + if (this.ast.apiLevel() < AST.JLS8) { + return; + } + // annotations on constructor declaration - implying on the object returned. + MethodDeclaration x1 = this.ast.newMethodDeclaration(); + x1.setConstructor(true); + x1.setName(this.N1); + SimpleType x2 = this.ast.newSimpleType(this.N2); + x2.annotations().add(this.ANO1); + x1.setReceiverType(x2); + basicMatch(x1); + } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=399768 + public void testTypeAnnotations11() { + if (this.ast.apiLevel() < AST.JLS8) { + return; + } + // annotated simple type at class inheritance + TypeDeclaration x1 = this.ast.newTypeDeclaration(); + TypeParameter x2 = this.ast.newTypeParameter(); + x2.setName(this.ast.newSimpleName("T")); + x2.annotations().add(this.ANO1); + x1.typeParameters().add(x2); + x1.setName(this.N2); + basicMatch(x1); + } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=399768 + public void testTypeAnnotations12() { + if (this.ast.apiLevel() < AST.JLS8) { + return; + } + VariableDeclarationFragment x1 = this.ast.newVariableDeclarationFragment(); + x1.setName(this.N1); + ExtraDimension x2 = this.ast.newExtraDimension(); + Annotation Annot = this.ast.newMarkerAnnotation(); + Annot.setTypeName(this.ast.newSimpleName("NewAnnot1")); + x2.annotations().add(Annot); + x1.extraDimensionInfos().add(x2); + x2 = this.ast.newExtraDimension(); + Annot = this.ast.newMarkerAnnotation(); + Annot.setTypeName(this.ast.newSimpleName("NewAnnot2")); + x2.annotations().add(Annot); + Annot = this.ast.newMarkerAnnotation(); + Annot.setTypeName(this.ast.newSimpleName("NewAnnot3")); + x2.annotations().add(Annot); + x1.extraDimensionInfos().add(x2); + basicMatch(x1); + } } diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java index 0280a8ee2d..b270142e4a 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java @@ -418,6 +418,7 @@ public class NaiveASTFlattener extends ASTVisitor { } public boolean visit(ExtraDimension node) { + this.buffer.append(" ");//$NON-NLS-1$ visitAnnotationsList(node.annotations()); this.buffer.append("[]"); //$NON-NLS-1$ return false; |
