|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| LongLiteral.java | 91.7% | 95.8% | 100% | 94.9% |
|
||||||||||||||
| 1 | /* | |
| 2 | * DynamicJava - Copyright (C) 1999-2001 | |
| 3 | * | |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a | |
| 5 | * copy of this software and associated documentation files | |
| 6 | * (the "Software"), to deal in the Software without restriction, including | |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, | |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to permit | |
| 9 | * persons to whom the Software is furnished to do so, subject to the | |
| 10 | * following conditions: | |
| 11 | * The above copyright notice and this permission notice shall be included | |
| 12 | * in all copies or substantial portions of the Software. | |
| 13 | * | |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
| 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
| 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
| 17 | * IN NO EVENT SHALL DYADE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
| 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
| 20 | * DEALINGS IN THE SOFTWARE. | |
| 21 | * | |
| 22 | * Except as contained in this notice, the name of Dyade shall not be | |
| 23 | * used in advertising or otherwise to promote the sale, use or other | |
| 24 | * dealings in this Software without prior written authorization from | |
| 25 | * Dyade. | |
| 26 | * | |
| 27 | */ | |
| 28 | ||
| 29 | package koala.dynamicjava.tree; | |
| 30 | ||
| 31 | import java.math.BigInteger; | |
| 32 | ||
| 33 | /** | |
| 34 | * This class represents the long literal nodes of the syntax tree | |
| 35 | * | |
| 36 | * @author Stephane Hillion | |
| 37 | * @version 1.0 - 1999/04/24 | |
| 38 | */ | |
| 39 | ||
| 40 | public class LongLiteral extends Literal { | |
| 41 | /** | |
| 42 | * Initializes a literal | |
| 43 | * @param rep the representation of the literal | |
| 44 | */ | |
| 45 | 21 | public LongLiteral(String rep) throws NumberFormatException { |
| 46 | 21 | this(rep, SourceInfo.NONE); |
| 47 | } | |
| 48 | ||
| 49 | /** | |
| 50 | * Initializes a literal | |
| 51 | * @param rep the representation of the literal | |
| 52 | */ | |
| 53 | 26 | public LongLiteral(String rep, SourceInfo si) throws NumberFormatException { |
| 54 | 26 | super(rep, |
| 55 | parse(rep.substring(0, rep.length())), | |
| 56 | long.class, | |
| 57 | si); | |
| 58 | } | |
| 59 | ||
| 60 | /** | |
| 61 | * Parse the representation of an integer | |
| 62 | */ | |
| 63 | 26 | private static Long parse(String s) throws NumberFormatException { |
| 64 | 26 | int radix = 10; |
| 65 | 26 | int start = 0; |
| 66 | 26 | boolean negate = false; |
| 67 | 26 | int end = s.length(); |
| 68 | 13 | if (s.endsWith("l") || s.endsWith("L")) { end--; } |
| 69 | // only consider 0x or 0 or - if this doesn't make the string empty | |
| 70 | 1 | if ((end-start>1) && (s.startsWith("-"))) { start++; negate = true; } |
| 71 | 7 | if ((end-start>2) && (s.startsWith("0x",start))) { radix = 16; start += 2; } |
| 72 | 6 | else if ((end-start>1) && (s.startsWith("0",start)) && (s.length() > 1)) { radix = 8; start++; } |
| 73 | // BigInteger can parse hex numbers representing negative longs; Long can't | |
| 74 | 26 | BigInteger val = new BigInteger(s.substring(start, end), radix); |
| 75 | 1 | if (negate) { val = val.negate(); } |
| 76 | 26 | long result = val.longValue(); |
| 77 | 26 | if (val.bitLength() > 64 || (radix == 10 && !val.equals(BigInteger.valueOf(result)))) { |
| 78 | 0 | throw new NumberFormatException("Literal is out of range: "+s); |
| 79 | } | |
| 80 | 26 | return result; |
| 81 | } | |
| 82 | ||
| 83 | } |
|
||||||||||