diff options
| author | Manoj Palat | 2013-01-03 06:17:33 +0000 |
|---|---|---|
| committer | Jayaprakash Arthanareeswaran | 2013-01-03 06:17:33 +0000 |
| commit | 3388256fa80bf40daffa9f485d2674986c875b82 (patch) | |
| tree | a08d208f26e50be63f5404fbc564ce7877cedad2 | |
| parent | 1e8305535ff304200778a08eb6eb4025791a09c1 (diff) | |
| download | eclipse.jdt.core-3388256fa80bf40daffa9f485d2674986c875b82.tar.gz eclipse.jdt.core-3388256fa80bf40daffa9f485d2674986c875b82.tar.xz eclipse.jdt.core-3388256fa80bf40daffa9f485d2674986c875b82.zip | |
Fix for bug 391898 - [1.8][compiler] Add DOM/AST support for annotations
on varargs
7 files changed, 229 insertions, 12 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 new file mode 100644 index 0000000000..3a35e57411 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter18Test.java @@ -0,0 +1,101 @@ +/******************************************************************************* + * 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.jdt.core.tests.dom; + +import java.util.List; + +import junit.framework.Test; + +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.MethodDeclaration; +import org.eclipse.jdt.core.dom.SingleVariableDeclaration; + +public class ASTConverter18Test extends ConverterTestSetup { + + ICompilationUnit workingCopy; + + public void setUpSuite() throws Exception { + super.setUpSuite(); + this.ast = AST.newAST(AST.JLS8); + } + + public ASTConverter18Test(String name) { + super(name); + } + + static { +// TESTS_NUMBERS = new int[] { 19 }; +// TESTS_RANGE = new int[] { 1, -1 }; +// TESTS_NAMES = new String[] {"test0001"}; + } + public static Test suite() { + return buildModelTestSuite(ASTConverter18Test.class); + } + + protected void tearDown() throws Exception { + super.tearDown(); + if (this.workingCopy != null) { + this.workingCopy.discardWorkingCopy(); + this.workingCopy = null; + } + } + + /* + * Type Annotations on Variable Arguments + */ + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=391898 + public void test0001() throws JavaModelException { + String contents = + " @java.lang.annotation.Target (java.lang.annotation.ElementType.TYPE_USE)\n" + + " @interface Marker {\n" + + " }\n" + + "public class X {\n" + + " public void foo(int @Marker... args) {\n" + + " }\n" + + " public void bar(@Marker int @Marker... args) {\n" + + " }\n" + + "}"; + this.workingCopy = getWorkingCopy("/Converter18/src/X.java", true/*resolve*/); + ASTNode node = buildAST( + contents, + this.workingCopy); + assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType()); + CompilationUnit compilationUnit = (CompilationUnit) node; + assertProblemsSize(compilationUnit, 0); + node = getASTNode(compilationUnit, 1, 0); + assertTrue("Not a method declaration", node.getNodeType() == ASTNode.METHOD_DECLARATION); + MethodDeclaration methodDeclaration = (MethodDeclaration) node; + List parameters = methodDeclaration.parameters(); + assertEquals("wrong size", 1, parameters.size()); + SingleVariableDeclaration parameter = (SingleVariableDeclaration) parameters.get(0); + List annotations = parameter.varargsAnnotations(); + assertEquals("Wrong number of annotations", 1, annotations.size()); + ASTNode annotation = (ASTNode) annotations.get(0); + checkSourceRange(annotation, "@Marker", contents); + node = getASTNode(compilationUnit,1,1); + assertTrue("Not a method declaration", node.getNodeType() == ASTNode.METHOD_DECLARATION); + parameters = methodDeclaration.parameters(); + assertEquals("Wrong number of parameters", 1, parameters.size()); + parameter = (SingleVariableDeclaration) parameters.get(0); + annotations = parameter.varargsAnnotations(); + assertEquals("Wrong number of annotations", 1, annotations.size()); + annotation = (ASTNode) annotations.get(0); + checkSourceRange(annotation, "@Marker", contents); + } +} 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 32a444755c..61994f2105 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 @@ -1,10 +1,14 @@ /******************************************************************************* - * 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 * http://www.eclipse.org/legal/epl-v10.html * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -39,6 +43,8 @@ public class ASTMatcherTest extends org.eclipse.jdt.core.tests.junit.extension.T if (methods[i].getName().startsWith("test")) { //$NON-NLS-1$ suite.addTest(new ASTMatcherTest(methods[i].getName(), AST.JLS2)); suite.addTest(new ASTMatcherTest(methods[i].getName(), JLS3_INTERNAL)); + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=391898 + suite.addTest(new ASTMatcherTest(methods[i].getName(), AST.JLS8)); } } return suite; @@ -1320,4 +1326,16 @@ public class ASTMatcherTest extends org.eclipse.jdt.core.tests.junit.extension.T basicMatch(x1); } + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=391898 + public void testSingleVariableDeclarationVarargsAnnotation() { + if (this.ast.apiLevel() < AST.JLS8) { + return; + } + SingleVariableDeclaration x1 = this.ast.newSingleVariableDeclaration(); + x1.setType(this.T1); + x1.setName(this.N1); + x1.setVarargs(true); + x1.varargsAnnotations().add(this.ANO1); + basicMatch(x1); + } } diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/RunConverterTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/RunConverterTests.java index e0e11c12d8..ecc8530292 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/RunConverterTests.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/RunConverterTests.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 @@ -46,6 +46,7 @@ public static Class[] getAllTestClasses() { ASTConverter15JLS4Test.class, ASTConverter15JLS8Test.class, TypeAnnotationsConverterTest.class, + ASTConverter18Test.class, }; } public static Test suite() { diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java index eb257d22ea..f8136b2d8d 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.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 @@ -795,6 +795,15 @@ class ASTConverter { */ if (isVarArgs) { setTypeForSingleVariableDeclaration(variableDecl, type, extraDimensions + 1); + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=391898 + if (this.ast.apiLevel() >= AST.JLS8 && !type.annotations().isEmpty()) { + Iterator annotations = type.annotations.iterator(); + while (annotations.hasNext()) { + Annotation annotation = (Annotation) annotations.next(); + annotation.setParent(null, null); + variableDecl.varargsAnnotations().add(annotation); + } + } if (extraDimensions != 0) { variableDecl.setFlags(variableDecl.getFlags() | ASTNode.MALFORMED); } diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTMatcher.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTMatcher.java index 64186bb657..34149c2d0d 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTMatcher.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTMatcher.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 @@ -1885,7 +1885,8 @@ public class ASTMatcher { safeSubtreeMatch(node.getType(), o.getType()) && safeSubtreeMatch(node.getName(), o.getName()) && node.getExtraDimensions() == o.getExtraDimensions() - && safeSubtreeMatch(node.getInitializer(), o.getInitializer()); + && safeSubtreeMatch(node.getInitializer(), o.getInitializer()) + && (level >= AST.JLS8 && node.isVarargs()) ? safeSubtreeListMatch(node.varargsAnnotations(), o.varargsAnnotations()) : true; } /** diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SingleVariableDeclaration.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SingleVariableDeclaration.java index 436af21c43..5c28b0828c 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SingleVariableDeclaration.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SingleVariableDeclaration.java @@ -1,10 +1,14 @@ /******************************************************************************* - * 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 * http://www.eclipse.org/legal/epl-v10.html * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -32,6 +36,11 @@ import java.util.List; * SingleVariableDeclaration: * { ExtendedModifier } Type [ <b>...</b> ] Identifier { <b>[</b><b>]</b> } [ <b>=</b> Expression ] * </pre> + * For JLS8, variable arguments are allowed to have optional annotations: + * <pre> + * SingleVariableDeclaration: + * { ExtendedModifier } Type {Annotation} [ <b>...</b> ] Identifier { <b>[</b><b>]</b> } [ <b>=</b> Expression ] + * </pre> * * @since 2.0 * @noinstantiate This class is not intended to be instantiated by clients. @@ -72,6 +81,13 @@ public class SingleVariableDeclaration extends VariableDeclaration { */ public static final SimplePropertyDescriptor VARARGS_PROPERTY = new SimplePropertyDescriptor(SingleVariableDeclaration.class, "varargs", boolean.class, MANDATORY); //$NON-NLS-1$ + + /** + * The "varargsAnnotations" structural property of variable arguments of this node type (child type: {@link Annotation}). + * @since 3.9 + */ + public static final ChildListPropertyDescriptor VARARGS_ANNOTATIONS_PROPERTY = + new ChildListPropertyDescriptor(SingleVariableDeclaration.class, "varargsAnnotations", Annotation.class, NO_CYCLE_RISK); //$NON-NLS-1$ /** * The "extraDimensions" structural property of this node type (type: {@link Integer}). @@ -102,7 +118,15 @@ public class SingleVariableDeclaration extends VariableDeclaration { * @since 3.1 */ private static final List PROPERTY_DESCRIPTORS_3_0; - + + /** + * A list of property descriptors (element type: + * {@link StructuralPropertyDescriptor}), + * or null if uninitialized. + * @since 3.9 + */ + private static final List PROPERTY_DESCRIPTORS_8_0; + static { List propertyList = new ArrayList(6); createPropertyList(SingleVariableDeclaration.class, propertyList); @@ -122,7 +146,20 @@ public class SingleVariableDeclaration extends VariableDeclaration { addProperty(EXTRA_DIMENSIONS_PROPERTY, propertyList); addProperty(INITIALIZER_PROPERTY, propertyList); PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(propertyList); - } + + + propertyList = new ArrayList(8); + createPropertyList(SingleVariableDeclaration.class, propertyList); + addProperty(MODIFIERS2_PROPERTY, propertyList); + addProperty(TYPE_PROPERTY, propertyList); + addProperty(VARARGS_ANNOTATIONS_PROPERTY, propertyList); + addProperty(VARARGS_PROPERTY, propertyList); + addProperty(NAME_PROPERTY, propertyList); + addProperty(EXTRA_DIMENSIONS_PROPERTY, propertyList); + addProperty(INITIALIZER_PROPERTY, propertyList); + PROPERTY_DESCRIPTORS_8_0 = reapPropertyList(propertyList); + + } /** * Returns a list of structural property descriptors for this node type. @@ -137,8 +174,10 @@ public class SingleVariableDeclaration extends VariableDeclaration { public static List propertyDescriptors(int apiLevel) { if (apiLevel == AST.JLS2_INTERNAL) { return PROPERTY_DESCRIPTORS_2_0; - } else { + } else if (apiLevel < AST.JLS8) { return PROPERTY_DESCRIPTORS_3_0; + } else { + return PROPERTY_DESCRIPTORS_8_0; } } @@ -192,6 +231,12 @@ public class SingleVariableDeclaration extends VariableDeclaration { private Expression optionalInitializer = null; /** + * The type annotations (element type: {@link Annotation}). + * @since 3.9 + */ + private ASTNode.NodeList varargsAnnotations = null; + + /** * Creates a new AST node for a variable declaration owned by the given * AST. By default, the variable declaration has: no modifiers, an * unspecified (but legal) type, an unspecified (but legal) variable name, @@ -206,6 +251,9 @@ public class SingleVariableDeclaration extends VariableDeclaration { super(ast); if (ast.apiLevel >= AST.JLS3_INTERNAL) { this.modifiers = new ASTNode.NodeList(MODIFIERS2_PROPERTY); + if (ast.apiLevel >= AST.JLS8) { + this.varargsAnnotations = new ASTNode.NodeList(VARARGS_ANNOTATIONS_PROPERTY); + } } } @@ -319,6 +367,9 @@ public class SingleVariableDeclaration extends VariableDeclaration { if (property == MODIFIERS2_PROPERTY) { return modifiers(); } + if (property == VARARGS_ANNOTATIONS_PROPERTY) { + return varargsAnnotations(); + } // allow default implementation to flag the error return super.internalGetChildListProperty(property); } @@ -347,6 +398,11 @@ public class SingleVariableDeclaration extends VariableDeclaration { result.setName((SimpleName) getName().clone(target)); result.setInitializer( (Expression) ASTNode.copySubtree(target, getInitializer())); + if (this.ast.apiLevel >= AST.JLS8) { + result.varargsAnnotations = new ASTNode.NodeList(VARARGS_ANNOTATIONS_PROPERTY); + result.varargsAnnotations().addAll( + ASTNode.copySubtrees(target, varargsAnnotations())); + } return result; } @@ -369,6 +425,9 @@ public class SingleVariableDeclaration extends VariableDeclaration { acceptChildren(visitor, this.modifiers); } acceptChild(visitor, getType()); + if (this.ast.apiLevel >= AST.JLS8 && isVarargs()) { + acceptChildren(visitor, this.varargsAnnotations); + } acceptChild(visitor, getName()); acceptChild(visitor, getInitializer()); } @@ -613,13 +672,28 @@ public class SingleVariableDeclaration extends VariableDeclaration { this.optionalInitializer = initializer; postReplaceChild(oldChild, initializer,INITIALIZER_PROPERTY); } - + + /** + * Returns the ordered list of annotations of variable arguments. + * + * @return the list of annotations of the varargs (element type: {@link Annotation}) + * @exception UnsupportedOperationException if this operation is used + * in a JLS2, JLS3 or JLS4 AST + * @since 3.9 + */ + public List varargsAnnotations() { + if (this.varargsAnnotations == null) { + unsupportedIn2_3_4(); + } + return this.varargsAnnotations; + } + /* (omit javadoc for this method) * Method declared on ASTNode. */ int memSize() { // treat Operator as free - return BASE_NODE_SIZE + 7 * 4; + return BASE_NODE_SIZE + 8 * 4; } /* (omit javadoc for this method) @@ -628,6 +702,7 @@ public class SingleVariableDeclaration extends VariableDeclaration { int treeSize() { return memSize() + + (this.varargsAnnotations == null ? 0 : this.varargsAnnotations.listSize()) + (this.modifiers == null ? 0 : this.modifiers.listSize()) + (this.type == null ? 0 : getType().treeSize()) + (this.variableName == null ? 0 : getName().treeSize()) 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 1d0955b1ab..19a3bae162 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 @@ -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 @@ -1318,6 +1318,18 @@ public class NaiveASTFlattener extends ASTVisitor { node.getType().accept(this); if (node.getAST().apiLevel() >= JLS3) { if (node.isVarargs()) { + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=391898 + if (node.getAST().apiLevel() >= AST.JLS8) { + List annotations = node.varargsAnnotations(); + if (annotations != null) { + this.buffer.append(' '); + for (Iterator it = annotations.iterator(); it.hasNext(); ) { + Annotation annotation = (Annotation) it.next(); + annotation.accept(this); + this.buffer.append(' '); + } + } + } this.buffer.append("...");//$NON-NLS-1$ } } |
