KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > dom > SimpleName


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.core.dom;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jdt.core.compiler.InvalidInputException;
18 import org.eclipse.jdt.internal.compiler.parser.Scanner;
19 import org.eclipse.jdt.internal.compiler.parser.TerminalTokens;
20
21 /**
22  * AST node for a simple name. A simple name is an identifier other than
23  * a keyword, boolean literal ("true", "false") or null literal ("null").
24  * <pre>
25  * SimpleName:
26  * Identifier
27  * </pre>
28  *
29  * @since 2.0
30  */

31 public class SimpleName extends Name {
32
33     /**
34      * The "identifier" structural property of this node type.
35      *
36      * @since 3.0
37      */

38     public static final SimplePropertyDescriptor IDENTIFIER_PROPERTY =
39         new SimplePropertyDescriptor(SimpleName.class, "identifier", String JavaDoc.class, MANDATORY); //$NON-NLS-1$
40

41     /**
42      * A list of property descriptors (element type:
43      * {@link StructuralPropertyDescriptor}),
44      * or null if uninitialized.
45      * @since 3.0
46      */

47     private static final List JavaDoc PROPERTY_DESCRIPTORS;
48     
49     static {
50         List JavaDoc propertyList = new ArrayList JavaDoc(2);
51         createPropertyList(SimpleName.class, propertyList);
52         addProperty(IDENTIFIER_PROPERTY, propertyList);
53         PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
54     }
55     
56     /**
57      * Returns a list of structural property descriptors for this node type.
58      * Clients must not modify the result.
59      *
60      * @param apiLevel the API level; one of the AST.JLS* constants
61      * @return a list of property descriptors (element type:
62      * {@link StructuralPropertyDescriptor})
63      * @since 3.0
64      */

65     public static List JavaDoc propertyDescriptors(int apiLevel) {
66         return PROPERTY_DESCRIPTORS;
67     }
68     
69     /**
70      * An unspecified (but externally observable) legal Java identifier.
71      */

72     private static final String JavaDoc MISSING_IDENTIFIER = "MISSING";//$NON-NLS-1$
73

74     /**
75      * The identifier; defaults to a unspecified, legal Java identifier.
76      */

77     private String JavaDoc identifier = MISSING_IDENTIFIER;
78     
79     /**
80      * Creates a new AST node for a simple name owned by the given AST.
81      * The new node has an unspecified, legal Java identifier.
82      * <p>
83      * N.B. This constructor is package-private; all subclasses must be
84      * declared in the same package; clients are unable to declare
85      * additional subclasses.
86      * </p>
87      *
88      * @param ast the AST that is to own this node
89      */

90     SimpleName(AST ast) {
91         super(ast);
92     }
93     
94     /* (omit javadoc for this method)
95      * Method declared on ASTNode.
96      * @since 3.0
97      */

98     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
99         return propertyDescriptors(apiLevel);
100     }
101     
102     /* (omit javadoc for this method)
103      * Method declared on ASTNode.
104      */

105     final Object JavaDoc internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object JavaDoc value) {
106         if (property == IDENTIFIER_PROPERTY) {
107             if (get) {
108                 return getIdentifier();
109             } else {
110                 setIdentifier((String JavaDoc) value);
111                 return null;
112             }
113         }
114         // allow default implementation to flag the error
115
return super.internalGetSetObjectProperty(property, get, value);
116     }
117
118     /* (omit javadoc for this method)
119      * Method declared on ASTNode.
120      */

121     final int getNodeType0() {
122         return SIMPLE_NAME;
123     }
124
125     /* (omit javadoc for this method)
126      * Method declared on ASTNode.
127      */

128     ASTNode clone0(AST target) {
129         SimpleName result = new SimpleName(target);
130         result.setSourceRange(this.getStartPosition(), this.getLength());
131         result.setIdentifier(getIdentifier());
132         return result;
133     }
134     
135     /* (omit javadoc for this method)
136      * Method declared on ASTNode.
137      */

138     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
139         // dispatch to correct overloaded match method
140
return matcher.match(this, other);
141     }
142
143     /* (omit javadoc for this method)
144      * Method declared on ASTNode.
145      */

146     void accept0(ASTVisitor visitor) {
147         visitor.visit(this);
148         visitor.endVisit(this);
149     }
150
151     /**
152      * Returns this node's identifier.
153      *
154      * @return the identifier of this node
155      */

156     public String JavaDoc getIdentifier() {
157         return this.identifier;
158     }
159     
160     /**
161      * Sets the identifier of this node to the given value.
162      * The identifier should be legal according to the rules
163      * of the Java language. Note that keywords are not legal
164      * identifiers.
165      * <p>
166      * Note that the list of keywords may depend on the version of the
167      * language (determined when the AST object was created).
168      * </p>
169      *
170      * @param identifier the identifier of this node
171      * @exception IllegalArgumentException if the identifier is invalid
172      */

173     public void setIdentifier(String JavaDoc identifier) {
174         // update internalSetIdentifier if this is changed
175
if (identifier == null) {
176             throw new IllegalArgumentException JavaDoc();
177         }
178         Scanner scanner = this.ast.scanner;
179         char[] source = identifier.toCharArray();
180         scanner.setSource(source);
181         final int length = source.length;
182         scanner.resetTo(0, length);
183         try {
184             int tokenType = scanner.getNextToken();
185             switch(tokenType) {
186                 case TerminalTokens.TokenNameIdentifier:
187                     if (scanner.getCurrentTokenEndPosition() != length - 1) {
188                         // this is the case when there is only one identifier see 87849
189
throw new IllegalArgumentException JavaDoc();
190                     }
191                     break;
192                 default:
193                     throw new IllegalArgumentException JavaDoc();
194             }
195         } catch(InvalidInputException e) {
196             throw new IllegalArgumentException JavaDoc();
197         }
198         preValueChange(IDENTIFIER_PROPERTY);
199         this.identifier = identifier;
200         postValueChange(IDENTIFIER_PROPERTY);
201     }
202
203     /* (omit javadoc for this method)
204      * This method is a copy of setIdentifier(String) that doesn't do any validation.
205      */

206     void internalSetIdentifier(String JavaDoc ident) {
207         preValueChange(IDENTIFIER_PROPERTY);
208         this.identifier = ident;
209         postValueChange(IDENTIFIER_PROPERTY);
210     }
211     
212     /**
213      * Returns whether this simple name represents a name that is being defined,
214      * as opposed to one being referenced. The following positions are considered
215      * ones where a name is defined:
216      * <ul>
217      * <li>The type name in a <code>TypeDeclaration</code> node.</li>
218      * <li>The method name in a <code>MethodDeclaration</code> node
219      * providing <code>isConstructor</code> is <code>false</code>.</li>
220      * <li>The variable name in any type of <code>VariableDeclaration</code>
221      * node.</li>
222      * <li>The enum type name in a <code>EnumDeclaration</code> node.</li>
223      * <li>The enum constant name in an <code>EnumConstantDeclaration</code>
224      * node.</li>
225      * <li>The variable name in an <code>EnhancedForStatement</code>
226      * node.</li>
227      * <li>The type variable name in a <code>TypeParameter</code>
228      * node.</li>
229      * <li>The type name in an <code>AnnotationTypeDeclaration</code> node.</li>
230      * <li>The member name in an <code>AnnotationTypeMemberDeclaration</code> node.</li>
231      * </ul>
232      * <p>
233      * Note that this is a convenience method that simply checks whether
234      * this node appears in the declaration position relative to its parent.
235      * It always returns <code>false</code> if this node is unparented.
236      * </p>
237      *
238      * @return <code>true</code> if this node declares a name, and
239      * <code>false</code> otherwise
240      */

241     public boolean isDeclaration() {
242         StructuralPropertyDescriptor d = getLocationInParent();
243         if (d == null) {
244             // unparented node
245
return false;
246         }
247         ASTNode parent = getParent();
248         if (parent instanceof TypeDeclaration) {
249             return (d == TypeDeclaration.NAME_PROPERTY);
250         }
251         if (parent instanceof MethodDeclaration) {
252             MethodDeclaration p = (MethodDeclaration) parent;
253             // could be the name of the method or constructor
254
return !p.isConstructor() && (d == MethodDeclaration.NAME_PROPERTY);
255         }
256         if (parent instanceof SingleVariableDeclaration) {
257             return (d == SingleVariableDeclaration.NAME_PROPERTY);
258         }
259         if (parent instanceof VariableDeclarationFragment) {
260             return (d == VariableDeclarationFragment.NAME_PROPERTY);
261         }
262         if (parent instanceof EnumDeclaration) {
263             return (d == EnumDeclaration.NAME_PROPERTY);
264         }
265         if (parent instanceof EnumConstantDeclaration) {
266             return (d == EnumConstantDeclaration.NAME_PROPERTY);
267         }
268         if (parent instanceof TypeParameter) {
269             return (d == TypeParameter.NAME_PROPERTY);
270         }
271         if (parent instanceof AnnotationTypeDeclaration) {
272             return (d == AnnotationTypeDeclaration.NAME_PROPERTY);
273         }
274         if (parent instanceof AnnotationTypeMemberDeclaration) {
275             return (d == AnnotationTypeMemberDeclaration.NAME_PROPERTY);
276         }
277         return false;
278     }
279         
280     /* (omit javadoc for this method)
281      * Method declared on Name.
282      */

283     void appendName(StringBuffer JavaDoc buffer) {
284         buffer.append(getIdentifier());
285     }
286
287     /* (omit javadoc for this method)
288      * Method declared on ASTNode.
289      */

290     int memSize() {
291         int size = BASE_NAME_NODE_SIZE + 2 * 4;
292         if (identifier != MISSING_IDENTIFIER) {
293             // everything but our missing id costs
294
size += stringSize(identifier);
295         }
296         return size;
297     }
298     
299     /* (omit javadoc for this method)
300      * Method declared on ASTNode.
301      */

302     int treeSize() {
303         return memSize();
304     }
305 }
306
307
Popular Tags