• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  *             of Java bytecode.
4  *
5  * Copyright (c) 2002-2014 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 import proguard.classfile.*;
24 
25 /**
26  * This value factory creates particular values.
27  *
28  * @author Eric Lafortune
29  */
30 public class ParticularValueFactory
31 extends      ValueFactory
32 {
33     // Shared copies of Value objects, to avoid creating a lot of objects.
34     static final IntegerValue INTEGER_VALUE_M1 = new ParticularIntegerValue(-1);
35     static final IntegerValue INTEGER_VALUE_0  = new ParticularIntegerValue(0);
36     static final IntegerValue INTEGER_VALUE_1  = new ParticularIntegerValue(1);
37     static final IntegerValue INTEGER_VALUE_2  = new ParticularIntegerValue(2);
38     static final IntegerValue INTEGER_VALUE_3  = new ParticularIntegerValue(3);
39     static final IntegerValue INTEGER_VALUE_4  = new ParticularIntegerValue(4);
40     static final IntegerValue INTEGER_VALUE_5  = new ParticularIntegerValue(5);
41     static final LongValue    LONG_VALUE_0     = new ParticularLongValue(0);
42     static final LongValue    LONG_VALUE_1     = new ParticularLongValue(1);
43     static final FloatValue   FLOAT_VALUE_0    = new ParticularFloatValue(0.0f);
44     static final FloatValue   FLOAT_VALUE_1    = new ParticularFloatValue(1.0f);
45     static final FloatValue   FLOAT_VALUE_2    = new ParticularFloatValue(2.0f);
46     static final DoubleValue  DOUBLE_VALUE_0   = new ParticularDoubleValue(0.0);
47     static final DoubleValue  DOUBLE_VALUE_1   = new ParticularDoubleValue(1.0);
48 
49 
50     private static int  POS_ZERO_FLOAT_BITS  = Float.floatToIntBits(0.0f);
51     private static long POS_ZERO_DOUBLE_BITS = Double.doubleToLongBits(0.0);
52 
53 
54     // Implementations for ValueFactory.
55 
createIntegerValue(int value)56     public IntegerValue createIntegerValue(int value)
57     {
58         switch (value)
59         {
60             case -1: return INTEGER_VALUE_M1;
61             case  0: return INTEGER_VALUE_0;
62             case  1: return INTEGER_VALUE_1;
63             case  2: return INTEGER_VALUE_2;
64             case  3: return INTEGER_VALUE_3;
65             case  4: return INTEGER_VALUE_4;
66             case  5: return INTEGER_VALUE_5;
67             default: return new ParticularIntegerValue(value);
68         }
69     }
70 
71 
createLongValue(long value)72     public LongValue createLongValue(long value)
73     {
74         return value == 0L ? LONG_VALUE_0 :
75                value == 1L ? LONG_VALUE_1 :
76                              new ParticularLongValue(value);
77     }
78 
79 
createFloatValue(float value)80     public FloatValue createFloatValue(float value)
81     {
82         // Make sure to distinguish between +0.0 and -0.0.
83         return value == 0.0f && Float.floatToIntBits(value) == POS_ZERO_FLOAT_BITS
84                              ? FLOAT_VALUE_0 :
85                value == 1.0f ? FLOAT_VALUE_1 :
86                value == 2.0f ? FLOAT_VALUE_2 :
87                                new ParticularFloatValue(value);
88     }
89 
90 
createDoubleValue(double value)91     public DoubleValue createDoubleValue(double value)
92     {
93         // Make sure to distinguish between +0.0 and -0.0.
94         return value == 0.0 && Double.doubleToLongBits(value) == POS_ZERO_DOUBLE_BITS
95                             ? DOUBLE_VALUE_0 :
96                value == 1.0 ? DOUBLE_VALUE_1 :
97                               new ParticularDoubleValue(value);
98     }
99 
100 
createArrayReferenceValue(String type, Clazz referencedClass, IntegerValue arrayLength)101     public ReferenceValue createArrayReferenceValue(String       type,
102                                                     Clazz        referencedClass,
103                                                     IntegerValue arrayLength)
104     {
105         return type == null ?
106             REFERENCE_VALUE_NULL :
107             new ArrayReferenceValue(ClassConstants.TYPE_ARRAY + type,
108                                     referencedClass,
109                                     arrayLength);
110     }
111 }
112