1 | /* |
---|---|
2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
4 | * |
5 | * This file is part of JavaParser. |
6 | * |
7 | * JavaParser can be used either under the terms of |
8 | * a) the GNU Lesser General Public License as published by |
9 | * the Free Software Foundation, either version 3 of the License, or |
10 | * (at your option) any later version. |
11 | * b) the terms of the Apache License |
12 | * |
13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
14 | * LICENCE.APACHE. Please refer to those files for details. |
15 | * |
16 | * JavaParser is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU Lesser General Public License for more details. |
20 | */ |
21 | package com.github.javaparser.ast.expr; |
22 | |
23 | import com.github.javaparser.TokenRange; |
24 | import com.github.javaparser.ast.AllFieldsConstructor; |
25 | import com.github.javaparser.ast.Generated; |
26 | import com.github.javaparser.ast.Node; |
27 | import com.github.javaparser.ast.visitor.CloneVisitor; |
28 | import com.github.javaparser.ast.visitor.GenericVisitor; |
29 | import com.github.javaparser.ast.visitor.VoidVisitor; |
30 | import com.github.javaparser.metamodel.IntegerLiteralExprMetaModel; |
31 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
32 | import java.util.Objects; |
33 | import java.util.Optional; |
34 | import java.util.function.Consumer; |
35 | import static com.github.javaparser.utils.Utils.hasUnaryMinusAsParent; |
36 | |
37 | /** |
38 | * All ways to specify an int literal. |
39 | * |
40 | * <ul> |
41 | * <li>{@code 8934}</li> |
42 | * <li>{@code 0x01}</li> |
43 | * <li>{@code 022}</li> |
44 | * <li>{@code 0B10101010}</li> |
45 | * </ul> |
46 | * |
47 | * @author Julio Vilmar Gesser |
48 | */ |
49 | public class IntegerLiteralExpr extends LiteralStringValueExpr { |
50 | |
51 | public static final String MAX_31_BIT_UNSIGNED_VALUE_AS_STRING = "2147483648"; |
52 | |
53 | public static final long MAX_31_BIT_UNSIGNED_VALUE_AS_LONG = 2147483648L; |
54 | |
55 | public IntegerLiteralExpr() { |
56 | this(null, "0"); |
57 | } |
58 | |
59 | @AllFieldsConstructor |
60 | public IntegerLiteralExpr(final String value) { |
61 | this(null, value); |
62 | } |
63 | |
64 | /** |
65 | * This constructor is used by the parser and is considered private. |
66 | */ |
67 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
68 | public IntegerLiteralExpr(TokenRange tokenRange, String value) { |
69 | super(tokenRange, value); |
70 | customInitialization(); |
71 | } |
72 | |
73 | /** |
74 | * @deprecated This function is deprecated in favor of {@link #IntegerLiteralExpr(String)}. Please refer to the |
75 | * {@link #asNumber()} function for valid formats and how to construct literals holding negative values. |
76 | */ |
77 | @Deprecated |
78 | public IntegerLiteralExpr(final int value) { |
79 | this(null, String.valueOf(value)); |
80 | } |
81 | |
82 | @Override |
83 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
84 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
85 | return v.visit(this, arg); |
86 | } |
87 | |
88 | @Override |
89 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
90 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
91 | v.visit(this, arg); |
92 | } |
93 | |
94 | @Override |
95 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
96 | public boolean remove(Node node) { |
97 | if (node == null) |
98 | return false; |
99 | return super.remove(node); |
100 | } |
101 | |
102 | /** |
103 | * @return the literal value as an integer while respecting different number representations |
104 | * @deprecated This function has issues with corner cases, such as 2147483648, so please use {@link |
105 | * IntegerLiteralExpr#asNumber()}. It will be made private or merged with {@link IntegerLiteralExpr#asNumber()} in |
106 | * future releases |
107 | */ |
108 | @Deprecated |
109 | public int asInt() { |
110 | String result = value.replaceAll("_", ""); |
111 | if (result.startsWith("0x") || result.startsWith("0X")) { |
112 | return Integer.parseUnsignedInt(result.substring(2), 16); |
113 | } |
114 | if (result.startsWith("0b") || result.startsWith("0B")) { |
115 | return Integer.parseUnsignedInt(result.substring(2), 2); |
116 | } |
117 | if (result.length() > 1 && result.startsWith("0")) { |
118 | return Integer.parseUnsignedInt(result.substring(1), 8); |
119 | } |
120 | return Integer.parseInt(result); |
121 | } |
122 | |
123 | /** |
124 | * This function returns a representation of the literal value as a number. This will return an integer, except for |
125 | * the case when the literal has the value {@code 2147483648}. This special literal is only allowed in the |
126 | * expression {@code -2147483648} which represents <code>Integer.MIN_VALUE</code>). However 2147483648 (2^31) |
127 | * is out of range of int, which is -(2^31) to (2^31)-1 and thus a long must be returned. |
128 | * |
129 | * <p>Note, that this function will NOT return a negative number if the literal was specified in decimal, since |
130 | * according to the language specification (chapter 3.10.1) an expression such as {@code -1} is represented by |
131 | * a unary expression with a minus operator and the literal {@code 1}. It is however possible to represent |
132 | * negative numbers in a literal directly, i.e. by using the binary or hexadecimal representation. For example |
133 | * {@code 0xffff_ffff} represents the value <code> -1</code>. |
134 | * |
135 | * @return the literal value as a number while respecting different number representations |
136 | */ |
137 | public Number asNumber() { |
138 | /* |
139 | * we need to handle the special case for the literal 2147483648, which is used to |
140 | * represent Integer.MIN_VALUE (-2147483648) as a combination of a UnaryExpr and an |
141 | * IntegerLiteralExpr. However 2147483648 cannot be represented in an integer, so we |
142 | * need to return a long |
143 | */ |
144 | if (Objects.equals(value, MAX_31_BIT_UNSIGNED_VALUE_AS_STRING) && hasUnaryMinusAsParent(this)) { |
145 | return MAX_31_BIT_UNSIGNED_VALUE_AS_LONG; |
146 | } else { |
147 | return asInt(); |
148 | } |
149 | } |
150 | |
151 | /** |
152 | * @deprecated This function is deprecated in favor of {@link #setValue(String)}. Please refer to the {@link |
153 | * #asNumber()} function for valid formats and how to construct literals holding negative values. |
154 | */ |
155 | @Deprecated |
156 | public IntegerLiteralExpr setInt(int value) { |
157 | this.value = String.valueOf(value); |
158 | return this; |
159 | } |
160 | |
161 | @Override |
162 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
163 | public IntegerLiteralExpr clone() { |
164 | return (IntegerLiteralExpr) accept(new CloneVisitor(), null); |
165 | } |
166 | |
167 | @Override |
168 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
169 | public IntegerLiteralExprMetaModel getMetaModel() { |
170 | return JavaParserMetaModel.integerLiteralExprMetaModel; |
171 | } |
172 | |
173 | @Override |
174 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
175 | public boolean replace(Node node, Node replacementNode) { |
176 | if (node == null) |
177 | return false; |
178 | return super.replace(node, replacementNode); |
179 | } |
180 | |
181 | @Override |
182 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
183 | public boolean isIntegerLiteralExpr() { |
184 | return true; |
185 | } |
186 | |
187 | @Override |
188 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
189 | public IntegerLiteralExpr asIntegerLiteralExpr() { |
190 | return this; |
191 | } |
192 | |
193 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
194 | public void ifIntegerLiteralExpr(Consumer<IntegerLiteralExpr> action) { |
195 | action.accept(this); |
196 | } |
197 | |
198 | @Override |
199 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
200 | public Optional<IntegerLiteralExpr> toIntegerLiteralExpr() { |
201 | return Optional.of(this); |
202 | } |
203 | } |
204 |
Members