diff options
author | Jayaprakash Arthanareeswaran | 2013-01-14 10:31:57 +0000 |
---|---|---|
committer | Jayaprakash Arthanareeswaran | 2013-01-14 10:33:46 +0000 |
commit | 01023454337af8ada2f5699e5de8fcd7a8d40840 (patch) | |
tree | 3d98a582a5a9f7ab08c3dba4e9552edf0358a4fc | |
parent | 7a73f29da5ea73cce41fc54b0fc3ad302149f476 (diff) | |
download | eclipse.jdt.core-01023454337af8ada2f5699e5de8fcd7a8d40840.tar.gz eclipse.jdt.core-01023454337af8ada2f5699e5de8fcd7a8d40840.tar.xz eclipse.jdt.core-01023454337af8ada2f5699e5de8fcd7a8d40840.zip |
Fix for bug 391890 - [1.8][compiler] DOM/AST API should be enhanced to
support type annotations in Type & TypeParameter nodes
9 files changed, 141 insertions, 66 deletions
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 864fc8aed5..d88686a175 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 @@ -806,12 +806,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 (type.isAnnotatable()) { + AnnotatableType annotatableType = (AnnotatableType) type; + if (this.ast.apiLevel() >= AST.JLS8 && !annotatableType.annotations().isEmpty()) { + Iterator annotations = annotatableType.annotations.iterator(); + while (annotations.hasNext()) { + Annotation annotation = (Annotation) annotations.next(); + annotation.setParent(null, null); + variableDecl.varargsAnnotations().add(annotation); + } } } if (extraDimensions != 0) { @@ -3066,7 +3069,7 @@ class ASTConverter { return variableDeclarationStatement; } - private void annotateType(Type type, org.eclipse.jdt.internal.compiler.ast.Annotation[] annotations) { + private void annotateType(AnnotatableType type, org.eclipse.jdt.internal.compiler.ast.Annotation[] annotations) { switch(this.ast.apiLevel) { case AST.JLS2_INTERNAL : case AST.JLS3_INTERNAL : @@ -3164,7 +3167,8 @@ class ASTConverter { length = typeReference.sourceEnd - typeReference.sourceStart + 1; // need to find out if this is an array type of primitive types or not if (isPrimitiveType(name)) { - int end = retrieveEndOfElementTypeNamePosition(sourceStart, sourceStart + length); + int[] positions = retrieveEndOfElementTypeNamePosition(sourceStart, sourceStart + length); + int end = positions[1]; if (end == -1) { end = sourceStart + length - 1; } @@ -3179,11 +3183,17 @@ class ASTConverter { ParameterizedSingleTypeReference parameterizedSingleTypeReference = (ParameterizedSingleTypeReference) typeReference; final SimpleName simpleName = new SimpleName(this.ast); simpleName.internalSetIdentifier(new String(name)); - int end = retrieveEndOfElementTypeNamePosition(sourceStart, sourceStart + length); + int[] positions = retrieveEndOfElementTypeNamePosition(sourceStart, sourceStart + length); + int end = positions[1]; if (end == -1) { end = sourceStart + length - 1; } - simpleName.setSourceRange(sourceStart, end - sourceStart + 1); + if (positions[0] != -1) { + simpleName.setSourceRange(positions[0], end - positions[0] + 1); + } else { + simpleName.setSourceRange(sourceStart, end - sourceStart + 1); + } + switch(this.ast.apiLevel) { case AST.JLS2_INTERNAL : SimpleType simpleType = new SimpleType(this.ast); @@ -3228,11 +3238,16 @@ class ASTConverter { simpleName.internalSetIdentifier(new String(name)); // we need to search for the starting position of the first brace in order to set the proper length // PR http://dev.eclipse.org/bugs/show_bug.cgi?id=10759 - int end = retrieveEndOfElementTypeNamePosition(sourceStart, sourceStart + length); + int[] positions = retrieveEndOfElementTypeNamePosition(sourceStart, sourceStart + length); + int end = positions[1]; if (end == -1) { end = sourceStart + length - 1; } - simpleName.setSourceRange(sourceStart, end - sourceStart + 1); + if (positions[0] != -1) { + simpleName.setSourceRange(positions[0], end - positions[0] + 1); + } else { + simpleName.setSourceRange(sourceStart, end - sourceStart + 1); + } final SimpleType simpleType = new SimpleType(this.ast); simpleType.setName(simpleName); type = simpleType; @@ -4179,16 +4194,26 @@ class ASTConverter { } /** - * This method is used to retrieve the position just before the left bracket. - * @return int the dimension found, -1 if none + * This method is used to retrieve the start and end position of a name. + * + * @return int[] a single dimensional array, with two elements, for the start and end positions of the name respectively */ - protected int retrieveEndOfElementTypeNamePosition(int start, int end) { + protected int[] retrieveEndOfElementTypeNamePosition(int start, int end) { this.scanner.resetTo(start, end); + boolean isAnnotation = false; try { int token; while ((token = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) { switch(token) { + case TerminalTokens.TokenNameAT: + isAnnotation = true; + break; case TerminalTokens.TokenNameIdentifier: + if (isAnnotation) { + isAnnotation = false; + break; + } + //$FALL-THROUGH$ case TerminalTokens.TokenNamebyte: case TerminalTokens.TokenNamechar: case TerminalTokens.TokenNamedouble: @@ -4197,13 +4222,13 @@ class ASTConverter { case TerminalTokens.TokenNamelong: case TerminalTokens.TokenNameshort: case TerminalTokens.TokenNameboolean: - return this.scanner.currentPosition - 1; + return new int[]{this.scanner.startPosition, this.scanner.currentPosition - 1}; } } } catch(InvalidInputException e) { // ignore } - return -1; + return new int[]{-1, -1}; } /** diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AnnotatableType.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AnnotatableType.java new file mode 100644 index 0000000000..3f3cb2d8da --- /dev/null +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AnnotatableType.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * 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.dom; + +import java.util.List; + +/** + * Type node for an annotatable type. + * <p> + * Introduced in JLS8, type references that can be annotated are represented by + * AnnotatableType. For the list of types extending AnnotatableType, see {@link Type}</p> + * + * @since 3.9 + */ +public abstract class AnnotatableType extends Type { + + /** + * Creates a new unparented node for an annotatable type owned by the given AST. + * <p> + * N.B. This constructor is package-private. + * </p> + * + * @param ast the AST that is to own this node + */ + AnnotatableType(AST ast) { + super(ast); + } + + ASTNode.NodeList annotations = null; + + /** + * Returns the live ordered list of annotations for this Type node. + * + * @return the live list of annotations (element type: {@link Annotation}) + * @exception UnsupportedOperationException if this operation is used + * in a JLS2, JLS3 or JLS4 AST + * @since 3.9 + */ + public List annotations() { + if (this.annotations == null) { + unsupportedIn2_3_4(); + } + return this.annotations; + } +} diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ArrayType.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ArrayType.java index 3e87e6fa22..d49ef5a7f3 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ArrayType.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ArrayType.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 @@ -37,7 +37,7 @@ import java.util.List; * @since 2.0 * @noinstantiate This class is not intended to be instantiated by clients. */ -public class ArrayType extends Type { +public class ArrayType extends AnnotatableType { /** * The "componentType" structural property of this node type (child type: {@link Type}). diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/PrimitiveType.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/PrimitiveType.java index 501e8695b7..2290c58fbe 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/PrimitiveType.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/PrimitiveType.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 @@ -44,7 +44,7 @@ import java.util.Map; * @since 2.0 * @noinstantiate This class is not intended to be instantiated by clients. */ -public class PrimitiveType extends Type { +public class PrimitiveType extends AnnotatableType { /** * Primitive type codes (typesafe enumeration). diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/QualifiedType.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/QualifiedType.java index 22e01e0cd6..967b5c2200 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/QualifiedType.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/QualifiedType.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2003, 2012 IBM Corporation and others. + * Copyright (c) 2003, 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 @@ -55,7 +55,7 @@ import java.util.List; * @since 3.1 * @noinstantiate This class is not intended to be instantiated by clients. */ -public class QualifiedType extends Type { +public class QualifiedType extends AnnotatableType { /** * This index represents the position inside a parameterized qualified type. */ diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SimpleType.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SimpleType.java index 819c9051d2..fe503aa44b 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SimpleType.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SimpleType.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 @@ -30,7 +30,7 @@ import java.util.List; * @since 2.0 * @noinstantiate This class is not intended to be instantiated by clients. */ -public class SimpleType extends Type { +public class SimpleType extends AnnotatableType { /** * The "name" structural property of this node type (child type: {@link Name}). diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/Type.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/Type.java index af18af89bb..e9860ee821 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/Type.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/Type.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 @@ -15,7 +15,6 @@ package org.eclipse.jdt.core.dom; -import java.util.List; /** * Abstract base class of all type AST node types. A type node represents a @@ -30,26 +29,27 @@ import java.util.List; * <p> * <pre> * Type: - * PrimitiveType - * ArrayType - * SimpleType - * QualifiedType + * AnnotatableType: + * PrimitiveType + * ArrayType + * SimpleType + * QualifiedType + * WildcardType * ParameterizedType - * WildcardType * UnionType * * PrimitiveType: - * <b>{Annotation} byte</b> - * <b>{Annotation} short</b> - * <b>{Annotation} char</b> - * <b>{Annotation} int</b> - * <b>{Annotation} long</b> - * <b>{Annotation} float</b> - * <b>{Annotation} double</b> - * <b>{Annotation} boolean</b> - * <b>{Annotation} void</b> + * {Annotation} <b>byte</b> + * {Annotation} <b>short</b> + * {Annotation} <b>char</b> + * {Annotation} <b>int</b> + * {Annotation} <b>long</b> + * {Annotation} <b>float</b> + * {Annotation} <b>double</b> + * {Annotation} <b>boolean</b> + * {Annotation} <b>void</b> * ArrayType: - * Type <b>{Annotation} [</b> <b>]</b> + * Type {Annotation} <b>'['</b> <b>']'</b> * SimpleType: * {Annotation} TypeName * ParameterizedType: @@ -64,12 +64,6 @@ import java.util.List; * @since 2.0 */ public abstract class Type extends ASTNode { - - /** - * The type annotations (element type: {@link Annotation}). - * @since 3.9 - */ - protected ASTNode.NodeList annotations = null; /** * Creates a new AST node for a type owned by the given AST. @@ -183,6 +177,19 @@ public abstract class Type extends ASTNode { public final boolean isWildcardType() { return (this instanceof WildcardType); } + + /** + * Returns whether this type can be annotated. All sub-classes of + * {@link AnnotatableType} can be annotated. + * + * @return <code>true</code> if this type is an instance of {@link AnnotatableType}, and + * <code>false</code> otherwise + * + * @since 3.9 + */ + public boolean isAnnotatable() { + return (this instanceof AnnotatableType); + } /** * Resolves and returns the binding for this type. @@ -197,19 +204,4 @@ public abstract class Type extends ASTNode { public final ITypeBinding resolveBinding() { return this.ast.getBindingResolver().resolveType(this); } - - /** - * Returns the live ordered list of annotations for this Type node. - * - * @return the live list of annotations (element type: {@link Annotation}) - * @exception UnsupportedOperationException if this operation is used - * in a JLS2, JLS3 or JLS4 AST - * @since 3.9 - */ - public List annotations() { - if (this.annotations == null) { - unsupportedIn2_3_4(); - } - return this.annotations; - } } diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/WildcardType.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/WildcardType.java index 8a4d92b439..ad8fe8cc1e 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/WildcardType.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/WildcardType.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2003, 2012 IBM Corporation and others. + * Copyright (c) 2003, 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 @@ -39,7 +39,7 @@ import java.util.List; * @since 3.1 * @noinstantiate This class is not intended to be instantiated by clients. */ -public class WildcardType extends Type { +public class WildcardType extends AnnotatableType { /** * The "bound" structural property of this node type (child type: {@link Type}). 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 a8ddab2e66..45bd69e776 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 @@ -21,6 +21,7 @@ import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.ASTVisitor; import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; +import org.eclipse.jdt.core.dom.AnnotatableType; import org.eclipse.jdt.core.dom.Annotation; import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration; import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration; @@ -1834,7 +1835,7 @@ public class NaiveASTFlattener extends ASTVisitor { } return false; } - private void visitTypeAnnotations(Type node) { + private void visitTypeAnnotations(AnnotatableType node) { if (node.getAST().apiLevel() >= AST.JLS8) { List annotations = node.annotations(); if (annotations != null) { |