EclipseJDT Source Viewer

Home|eclipse_jdt/src/org/eclipse/jdt/core/dom/NumberLiteral.java
1/*******************************************************************************
2 * Copyright (c) 2000, 2013 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 *     IBM Corporation - initial API and implementation
13 *******************************************************************************/
14
15package org.eclipse.jdt.core.dom;
16
17import java.util.ArrayList;
18import java.util.List;
19
20import org.eclipse.jdt.core.compiler.InvalidInputException;
21import org.eclipse.jdt.internal.compiler.parser.Scanner;
22import org.eclipse.jdt.internal.compiler.parser.TerminalTokens;
23
24/**
25 * Number literal nodes.
26 *
27 * @since 2.0
28 * @noinstantiate This class is not intended to be instantiated by clients.
29 */
30@SuppressWarnings("rawtypes")
31public class NumberLiteral extends Expression {
32
33    /**
34     * The "token" structural property of this node type (type: {@link String}).
35     * @since 3.0
36     */
37    public static final SimplePropertyDescriptor TOKEN_PROPERTY =
38        new SimplePropertyDescriptor(NumberLiteral.class"token"String.classMANDATORY); //$NON-NLS-1$
39
40    /**
41     * A list of property descriptors (element type:
42     * {@link StructuralPropertyDescriptor}),
43     * or null if uninitialized.
44     */
45    private static final List PROPERTY_DESCRIPTORS;
46
47    static {
48        List propertyList = new ArrayList(2);
49        createPropertyList(NumberLiteral.classpropertyList);
50        addProperty(TOKEN_PROPERTYpropertyList);
51        PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
52    }
53
54    /**
55     * Returns a list of structural property descriptors for this node type.
56     * Clients must not modify the result.
57     *
58     * @param apiLevel the API level; one of the
59     * <code>AST.JLS*</code> constants
60
61     * @return a list of property descriptors (element type:
62     * {@link StructuralPropertyDescriptor})
63     * @since 3.0
64     */
65    public static List propertyDescriptors(int apiLevel) {
66        return PROPERTY_DESCRIPTORS;
67    }
68
69    /**
70     * The token string; defaults to the integer literal "0".
71     */
72    private String tokenValue = "0";//$NON-NLS-1$
73
74    /**
75     * Creates a new unparented number literal node owned by the given AST.
76     * By default, the number literal is the token "<code>0</code>".
77     * <p>
78     * N.B. This constructor is package-private.
79     * </p>
80     *
81     * @param ast the AST that is to own this node
82     */
83    NumberLiteral(AST ast) {
84        super(ast);
85    }
86
87    @Override
88    final List internalStructuralPropertiesForType(int apiLevel) {
89        return propertyDescriptors(apiLevel);
90    }
91
92    @Override
93    final Object internalGetSetObjectProperty(SimplePropertyDescriptor propertyboolean getObject value) {
94        if (property == TOKEN_PROPERTY) {
95            if (get) {
96                return getToken();
97            } else {
98                setToken((Stringvalue);
99                return null;
100            }
101        }
102        // allow default implementation to flag the error
103        return super.internalGetSetObjectProperty(propertygetvalue);
104    }
105
106    @Override
107    final int getNodeType0() {
108        return NUMBER_LITERAL;
109    }
110
111    @Override
112    ASTNode clone0(AST target) {
113        NumberLiteral result = new NumberLiteral(target);
114        result.setSourceRange(getStartPosition(), getLength());
115        result.setToken(getToken());
116        return result;
117    }
118
119    @Override
120    final boolean subtreeMatch0(ASTMatcher matcherObject other) {
121        // dispatch to correct overloaded match method
122        return matcher.match(this, other);
123    }
124
125    @Override
126    void accept0(ASTVisitor visitor) {
127        visitor.visit(this);
128        visitor.endVisit(this);
129    }
130
131    /**
132     * Returns the token of this number literal node. The value is the sequence
133     * of characters that would appear in the source program.
134     *
135     * @return the numeric literal token
136     */
137    public String getToken() {
138        return this.tokenValue;
139    }
140
141    /**
142     * Sets the token of this number literal node. The value is the sequence
143     * of characters that would appear in the source program.
144     *
145     * @param token the numeric literal token
146     * @exception IllegalArgumentException if the argument is incorrect
147     */
148    public void setToken(String token) {
149        // update internalSetToken(String) if this is changed
150        if (token == null || token.length() == 0) {
151            throw new IllegalArgumentException();
152        }
153        Scanner scanner = this.ast.scanner;
154        char[] source = token.toCharArray();
155        scanner.setSource(source);
156        scanner.resetTo(0source.length);
157        scanner.tokenizeComments = false;
158        scanner.tokenizeWhiteSpace = false;
159        try {
160            int tokenType = scanner.getNextToken();
161            switch(tokenType) {
162                case TerminalTokens.TokenNameDoubleLiteral:
163                case TerminalTokens.TokenNameIntegerLiteral:
164                case TerminalTokens.TokenNameFloatingPointLiteral:
165                case TerminalTokens.TokenNameLongLiteral:
166                    break;
167                case TerminalTokens.TokenNameMINUS :
168                    tokenType = scanner.getNextToken();
169                    switch(tokenType) {
170                        case TerminalTokens.TokenNameDoubleLiteral:
171                        case TerminalTokens.TokenNameIntegerLiteral:
172                        case TerminalTokens.TokenNameFloatingPointLiteral:
173                        case TerminalTokens.TokenNameLongLiteral:
174                            break;
175                        default:
176                            throw new IllegalArgumentException("Invalid number literal : >" + token + "<"); //$NON-NLS-1$//$NON-NLS-2$
177                    }
178                    break;
179                default:
180                    throw new IllegalArgumentException("Invalid number literal : >" + token + "<");//$NON-NLS-1$//$NON-NLS-2$
181            }
182        } catch(InvalidInputException e) {
183            throw new IllegalArgumentException();
184        } finally {
185            scanner.tokenizeComments = true;
186            scanner.tokenizeWhiteSpace = true;
187        }
188        preValueChange(TOKEN_PROPERTY);
189        this.tokenValue = token;
190        postValueChange(TOKEN_PROPERTY);
191    }
192
193    /* (omit javadoc for this method)
194     * This method is a copy of setToken(String) that doesn't do any validation.
195     */
196    void internalSetToken(String token) {
197        preValueChange(TOKEN_PROPERTY);
198        this.tokenValue = token;
199        postValueChange(TOKEN_PROPERTY);
200    }
201
202    @Override
203    int memSize() {
204        int size = BASE_NODE_SIZE + 1 * 4 + stringSize(this.tokenValue);
205        return size;
206    }
207
208    @Override
209    int treeSize() {
210        return memSize();
211    }
212}
213
MembersX
NumberLiteral:PROPERTY_DESCRIPTORS
NumberLiteral:clone0
NumberLiteral:clone0:Block:result
NumberLiteral:setToken:Block:Block:tokenType
NumberLiteral:propertyDescriptors
NumberLiteral:memSize
NumberLiteral:getToken
NumberLiteral:internalGetSetObjectProperty
NumberLiteral:TOKEN_PROPERTY
NumberLiteral:accept0
NumberLiteral:internalSetToken
NumberLiteral:memSize:Block:size
NumberLiteral:internalStructuralPropertiesForType
NumberLiteral:getNodeType0
NumberLiteral:setToken:Block:scanner
NumberLiteral:Block:propertyList
NumberLiteral:setToken
NumberLiteral:setToken:Block:source
NumberLiteral:tokenValue
NumberLiteral:NumberLiteral
NumberLiteral:subtreeMatch0
NumberLiteral:treeSize
Members
X