| 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.JavaParserMetaModel; |
| 31 | import com.github.javaparser.metamodel.LongLiteralExprMetaModel; |
| 32 | import java.math.BigInteger; |
| 33 | import java.util.Objects; |
| 34 | import java.util.Optional; |
| 35 | import java.util.function.Consumer; |
| 36 | import static com.github.javaparser.utils.Utils.hasUnaryMinusAsParent; |
| 37 | |
| 38 | /** |
| 39 | * All ways to specify a long literal. |
| 40 | * |
| 41 | * <ul> |
| 42 | * <li>{@code 8934l}</li> |
| 43 | * <li>{@code 0x01L}</li> |
| 44 | * <li>{@code 022l}</li> |
| 45 | * <li>{@code 0B10101010L}</li> |
| 46 | * <li>{@code 99999999L}</li> |
| 47 | * </ul> |
| 48 | * |
| 49 | * @author Julio Vilmar Gesser |
| 50 | */ |
| 51 | public class LongLiteralExpr extends LiteralStringValueExpr { |
| 52 | |
| 53 | public static final String MAX_63_BIT_UNSIGNED_VALUE_AS_STRING = "9223372036854775808L"; |
| 54 | |
| 55 | public static final BigInteger MAX_63_BIT_UNSIGNED_VALUE_AS_BIG_INTEGER = new BigInteger("9223372036854775808"); |
| 56 | |
| 57 | public LongLiteralExpr() { |
| 58 | this(null, "0"); |
| 59 | } |
| 60 | |
| 61 | @AllFieldsConstructor |
| 62 | public LongLiteralExpr(final String value) { |
| 63 | this(null, value); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * This constructor is used by the parser and is considered private. |
| 68 | */ |
| 69 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
| 70 | public LongLiteralExpr(TokenRange tokenRange, String value) { |
| 71 | super(tokenRange, value); |
| 72 | customInitialization(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @deprecated This function is deprecated in favor of {@link #LongLiteralExpr(String)}. Please refer to the {@link |
| 77 | * #asNumber()} function for valid formats and how to construct literals holding negative values. |
| 78 | */ |
| 79 | @Deprecated |
| 80 | public LongLiteralExpr(final long value) { |
| 81 | this(null, String.valueOf(value)); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 86 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
| 87 | return v.visit(this, arg); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 92 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
| 93 | v.visit(this, arg); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 98 | public boolean remove(Node node) { |
| 99 | if (node == null) |
| 100 | return false; |
| 101 | return super.remove(node); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @return the literal value as an long while respecting different number representations |
| 106 | * @deprecated This function has issues with corner cases, such as 9223372036854775808L, so please use {@link |
| 107 | * LongLiteralExpr#asNumber()}. It will be made private or merged with {@link LongLiteralExpr#asNumber()} in future |
| 108 | * releases |
| 109 | */ |
| 110 | @Deprecated |
| 111 | public long asLong() { |
| 112 | String result = value.replaceAll("_", ""); |
| 113 | char lastChar = result.charAt(result.length() - 1); |
| 114 | if (lastChar == 'l' || lastChar == 'L') { |
| 115 | result = result.substring(0, result.length() - 1); |
| 116 | } |
| 117 | if (result.startsWith("0x") || result.startsWith("0X")) { |
| 118 | return Long.parseUnsignedLong(result.substring(2), 16); |
| 119 | } |
| 120 | if (result.startsWith("0b") || result.startsWith("0B")) { |
| 121 | return Long.parseUnsignedLong(result.substring(2), 2); |
| 122 | } |
| 123 | if (result.length() > 1 && result.startsWith("0")) { |
| 124 | return Long.parseUnsignedLong(result.substring(1), 8); |
| 125 | } |
| 126 | return Long.parseLong(result); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * This function returns a representation of the literal value as a number. This will return a long, except for the |
| 131 | * case when the literal has the value {@code 9223372036854775808L}. This special literal is only allowed in |
| 132 | * the expression {@code -9223372036854775808L} which represents <code>Long.MIN_VALUE</code>). However |
| 133 | * 9223372036854775808 (2^63) is out of range of long, which is -(2^63) to (2^63)-1 and thus a BigInteger must be |
| 134 | * returned. |
| 135 | * |
| 136 | * <p>Note, that this function will NOT return a negative number if the literal was specified in decimal, since |
| 137 | * according to the language specification (chapter 3.10.1) an expression such as {@code -1L} is represented by |
| 138 | * a unary * expression with a minus operator and the literal {@code 1L}. It is however possible to represent |
| 139 | * negative * numbers in a literal directly, i.e. by using the binary or hexadecimal representation. For example |
| 140 | * {@code 0xffff_ffff_ffff_ffffL} represents the value <code>-1L</code>. |
| 141 | * |
| 142 | * @return the literal value as a number while respecting different number representations |
| 143 | */ |
| 144 | public Number asNumber() { |
| 145 | /* we need to handle the special case for the literal 9223372036854775808L, which is used to |
| 146 | * represent Integer.MIN_VALUE (-9223372036854775808L) as a combination of a UnaryExpr and a |
| 147 | * LongLiteralExpr. However 9223372036854775808L cannot be represented in a long, so we need |
| 148 | * to return a BigInteger |
| 149 | */ |
| 150 | if (Objects.equals(value, MAX_63_BIT_UNSIGNED_VALUE_AS_STRING) && hasUnaryMinusAsParent(this)) { |
| 151 | return MAX_63_BIT_UNSIGNED_VALUE_AS_BIG_INTEGER; |
| 152 | } else { |
| 153 | return asLong(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @deprecated This function is deprecated in favor of {@link #setValue(String)}. Please refer to the {@link |
| 159 | * #asNumber()} function for valid formats and how to construct literals holding negative values. |
| 160 | */ |
| 161 | @Deprecated |
| 162 | public LongLiteralExpr setLong(long value) { |
| 163 | this.value = String.valueOf(value); |
| 164 | return this; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
| 169 | public LongLiteralExpr clone() { |
| 170 | return (LongLiteralExpr) accept(new CloneVisitor(), null); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
| 175 | public LongLiteralExprMetaModel getMetaModel() { |
| 176 | return JavaParserMetaModel.longLiteralExprMetaModel; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
| 181 | public boolean replace(Node node, Node replacementNode) { |
| 182 | if (node == null) |
| 183 | return false; |
| 184 | return super.replace(node, replacementNode); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 189 | public boolean isLongLiteralExpr() { |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 195 | public LongLiteralExpr asLongLiteralExpr() { |
| 196 | return this; |
| 197 | } |
| 198 | |
| 199 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 200 | public void ifLongLiteralExpr(Consumer<LongLiteralExpr> action) { |
| 201 | action.accept(this); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 206 | public Optional<LongLiteralExpr> toLongLiteralExpr() { |
| 207 | return Optional.of(this); |
| 208 | } |
| 209 | } |
| 210 |
Members