1 /* 2 * ProGuard -- shrinking, optimization, obfuscation, and preverification 3 * of Java bytecode. 4 * 5 * Copyright (c) 2002-2013 Eric Lafortune (eric@graphics.cornell.edu) 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License as published by the Free 9 * Software Foundation; either version 2 of the License, or (at your option) 10 * any later version. 11 * 12 * This program is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 * more details. 16 * 17 * You should have received a copy of the GNU General Public License along 18 * with this program; if not, write to the Free Software Foundation, Inc., 19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 package proguard.evaluation.value; 22 23 /** 24 * This class provides methods to create and reuse IntegerValue objects. 25 * 26 * @author Eric Lafortune 27 */ 28 public class SpecificValueFactory 29 extends ValueFactory 30 { 31 // Shared copies of Value objects, to avoid creating a lot of objects. 32 static final IntegerValue INTEGER_VALUE_M1 = new ParticularIntegerValue(-1); 33 static final IntegerValue INTEGER_VALUE_0 = new ParticularIntegerValue(0); 34 static final IntegerValue INTEGER_VALUE_1 = new ParticularIntegerValue(1); 35 static final IntegerValue INTEGER_VALUE_2 = new ParticularIntegerValue(2); 36 static final IntegerValue INTEGER_VALUE_3 = new ParticularIntegerValue(3); 37 static final IntegerValue INTEGER_VALUE_4 = new ParticularIntegerValue(4); 38 static final IntegerValue INTEGER_VALUE_5 = new ParticularIntegerValue(5); 39 static final LongValue LONG_VALUE_0 = new ParticularLongValue(0); 40 static final LongValue LONG_VALUE_1 = new ParticularLongValue(1); 41 static final FloatValue FLOAT_VALUE_0 = new ParticularFloatValue(0.0f); 42 static final FloatValue FLOAT_VALUE_1 = new ParticularFloatValue(1.0f); 43 static final FloatValue FLOAT_VALUE_2 = new ParticularFloatValue(2.0f); 44 static final DoubleValue DOUBLE_VALUE_0 = new ParticularDoubleValue(0.0); 45 static final DoubleValue DOUBLE_VALUE_1 = new ParticularDoubleValue(1.0); 46 47 48 private static int POS_ZERO_FLOAT_BITS = Float.floatToIntBits(0.0f); 49 private static long POS_ZERO_DOUBLE_BITS = Double.doubleToLongBits(0.0); 50 51 52 // Implementations for ValueFactory. 53 createIntegerValue(int value)54 public IntegerValue createIntegerValue(int value) 55 { 56 switch (value) 57 { 58 case -1: return INTEGER_VALUE_M1; 59 case 0: return INTEGER_VALUE_0; 60 case 1: return INTEGER_VALUE_1; 61 case 2: return INTEGER_VALUE_2; 62 case 3: return INTEGER_VALUE_3; 63 case 4: return INTEGER_VALUE_4; 64 case 5: return INTEGER_VALUE_5; 65 default: return new ParticularIntegerValue(value); 66 } 67 } 68 69 createLongValue(long value)70 public LongValue createLongValue(long value) 71 { 72 return value == 0 ? LONG_VALUE_0 : 73 value == 1 ? LONG_VALUE_1 : 74 new ParticularLongValue(value); 75 } 76 77 createFloatValue(float value)78 public FloatValue createFloatValue(float value) 79 { 80 // Make sure to distinguish between +0.0 and -0.0. 81 return value == 0.0f && Float.floatToIntBits(value) == POS_ZERO_FLOAT_BITS 82 ? FLOAT_VALUE_0 : 83 value == 1.0f ? FLOAT_VALUE_1 : 84 value == 2.0f ? FLOAT_VALUE_2 : 85 new ParticularFloatValue(value); 86 } 87 88 createDoubleValue(double value)89 public DoubleValue createDoubleValue(double value) 90 { 91 // Make sure to distinguish between +0.0 and -0.0. 92 return value == 0.0 && Double.doubleToLongBits(value) == POS_ZERO_DOUBLE_BITS 93 ? DOUBLE_VALUE_0 : 94 value == 1.0 ? DOUBLE_VALUE_1 : 95 new ParticularDoubleValue(value); 96 } 97 } 98