KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  * Number literal nodes.
23  *
24  * @since 2.0
25  */

26 public class NumberLiteral extends Expression {
27
28     /**
29      * The "token" structural property of this node type.
30      * @since 3.0
31      */

32     public static final SimplePropertyDescriptor TOKEN_PROPERTY =
33         new SimplePropertyDescriptor(NumberLiteral.class, "token", String JavaDoc.class, MANDATORY); //$NON-NLS-1$
34

35     /**
36      * A list of property descriptors (element type:
37      * {@link StructuralPropertyDescriptor}),
38      * or null if uninitialized.
39      */

40     private static final List JavaDoc PROPERTY_DESCRIPTORS;
41     
42     static {
43         List JavaDoc propertyList = new ArrayList JavaDoc(2);
44         createPropertyList(NumberLiteral.class, propertyList);
45         addProperty(TOKEN_PROPERTY, propertyList);
46         PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
47     }
48
49     /**
50      * Returns a list of structural property descriptors for this node type.
51      * Clients must not modify the result.
52      *
53      * @param apiLevel the API level; one of the
54      * <code>AST.JLS*</code> constants
55
56      * @return a list of property descriptors (element type:
57      * {@link StructuralPropertyDescriptor})
58      * @since 3.0
59      */

60     public static List JavaDoc propertyDescriptors(int apiLevel) {
61         return PROPERTY_DESCRIPTORS;
62     }
63             
64     /**
65      * The token string; defaults to the integer literal "0".
66      */

67     private String JavaDoc tokenValue = "0";//$NON-NLS-1$
68

69     /**
70      * Creates a new unparented number literal node owned by the given AST.
71      * By default, the number literal is the token "<code>0</code>".
72      * <p>
73      * N.B. This constructor is package-private.
74      * </p>
75      *
76      * @param ast the AST that is to own this node
77      */

78     NumberLiteral(AST ast) {
79         super(ast);
80     }
81
82     /* (omit javadoc for this method)
83      * Method declared on ASTNode.
84      */

85     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
86         return propertyDescriptors(apiLevel);
87     }
88     
89     /* (omit javadoc for this method)
90      * Method declared on ASTNode.
91      */

92     final Object JavaDoc internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object JavaDoc value) {
93         if (property == TOKEN_PROPERTY) {
94             if (get) {
95                 return getToken();
96             } else {
97                 setToken((String JavaDoc) value);
98                 return null;
99             }
100         }
101         // allow default implementation to flag the error
102
return super.internalGetSetObjectProperty(property, get, value);
103     }
104     
105     /* (omit javadoc for this method)
106      * Method declared on ASTNode.
107      */

108     final int getNodeType0() {
109         return NUMBER_LITERAL;
110     }
111
112     /* (omit javadoc for this method)
113      * Method declared on ASTNode.
114      */

115     ASTNode clone0(AST target) {
116         NumberLiteral result = new NumberLiteral(target);
117         result.setSourceRange(this.getStartPosition(), this.getLength());
118         result.setToken(getToken());
119         return result;
120     }
121
122     /* (omit javadoc for this method)
123      * Method declared on ASTNode.
124      */

125     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
126         // dispatch to correct overloaded match method
127
return matcher.match(this, other);
128     }
129
130     /* (omit javadoc for this method)
131      * Method declared on ASTNode.
132      */

133     void accept0(ASTVisitor visitor) {
134         visitor.visit(this);
135         visitor.endVisit(this);
136     }
137     
138     /**
139      * Returns the token of this number literal node. The value is the sequence
140      * of characters that would appear in the source program.
141      *
142      * @return the numeric literal token
143      */

144     public String JavaDoc getToken() {
145         return this.tokenValue;
146     }
147         
148     /**
149      * Sets the token of this number literal node. The value is the sequence
150      * of characters that would appear in the source program.
151      *
152      * @param token the numeric literal token
153      * @exception IllegalArgumentException if the argument is incorrect
154      */

155     public void setToken(String JavaDoc token) {
156         // update internalSetToken(String) if this is changed
157
if (token == null || token.length() == 0) {
158             throw new IllegalArgumentException JavaDoc();
159         }
160         Scanner scanner = this.ast.scanner;
161         char[] source = token.toCharArray();
162         scanner.setSource(source);
163         scanner.resetTo(0, source.length);
164         scanner.tokenizeComments = false;
165         scanner.tokenizeWhiteSpace = false;
166         try {
167             int tokenType = scanner.getNextToken();
168             switch(tokenType) {
169                 case TerminalTokens.TokenNameDoubleLiteral:
170                 case TerminalTokens.TokenNameIntegerLiteral:
171                 case TerminalTokens.TokenNameFloatingPointLiteral:
172                 case TerminalTokens.TokenNameLongLiteral:
173                     break;
174                 case TerminalTokens.TokenNameMINUS :
175                     tokenType = scanner.getNextToken();
176                     switch(tokenType) {
177                         case TerminalTokens.TokenNameDoubleLiteral:
178                         case TerminalTokens.TokenNameIntegerLiteral:
179                         case TerminalTokens.TokenNameFloatingPointLiteral:
180                         case TerminalTokens.TokenNameLongLiteral:
181                             break;
182                         default:
183                             throw new IllegalArgumentException JavaDoc("Invalid number literal : >" + token + "<"); //$NON-NLS-1$//$NON-NLS-2$
184
}
185                     break;
186                 default:
187                     throw new IllegalArgumentException JavaDoc("Invalid number literal : >" + token + "<");//$NON-NLS-1$//$NON-NLS-2$
188
}
189         } catch(InvalidInputException e) {
190             throw new IllegalArgumentException JavaDoc();
191         } finally {
192             scanner.tokenizeComments = true;
193             scanner.tokenizeWhiteSpace = true;
194         }
195         preValueChange(TOKEN_PROPERTY);
196         this.tokenValue = token;
197         postValueChange(TOKEN_PROPERTY);
198     }
199     
200     /* (omit javadoc for this method)
201      * This method is a copy of setToken(String) that doesn't do any validation.
202      */

203     void internalSetToken(String JavaDoc token) {
204         preValueChange(TOKEN_PROPERTY);
205         this.tokenValue = token;
206         postValueChange(TOKEN_PROPERTY);
207     }
208     /* (omit javadoc for this method)
209      * Method declared on ASTNode.
210      */

211     int memSize() {
212         int size = BASE_NODE_SIZE + 1 * 4 + stringSize(tokenValue);
213         return size;
214     }
215     
216     /* (omit javadoc for this method)
217      * Method declared on ASTNode.
218      */

219     int treeSize() {
220         return memSize();
221     }
222 }
223
Popular Tags