aboutsummaryrefslogtreecommitdiffstats
path: root/src/proguard/evaluation/value/ValueFactory.java
blob: 8415381735bee7dcb8cb697ae3f79503e924e4bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * ProGuard -- shrinking, optimization, obfuscation, and preverification
 *             of Java bytecode.
 *
 * Copyright (c) 2002-2009 Eric Lafortune (eric@graphics.cornell.edu)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package proguard.evaluation.value;

import proguard.classfile.*;
import proguard.classfile.util.ClassUtil;

/**
 * This class provides methods to create and reuse Value objects.
 *
 * @author Eric Lafortune
 */
public class ValueFactory
{
    // Shared copies of Value objects, to avoid creating a lot of objects.
    static final IntegerValue INTEGER_VALUE = new UnknownIntegerValue();
    static final LongValue    LONG_VALUE    = new UnknownLongValue();
    static final FloatValue   FLOAT_VALUE   = new UnknownFloatValue();
    static final DoubleValue  DOUBLE_VALUE  = new UnknownDoubleValue();

    static final ReferenceValue REFERENCE_VALUE_NULL                        = new ReferenceValue(null, null, true);
    static final ReferenceValue REFERENCE_VALUE_JAVA_LANG_OBJECT_MAYBE_NULL = new ReferenceValue(ClassConstants.INTERNAL_NAME_JAVA_LANG_OBJECT, null, true);
    static final ReferenceValue REFERENCE_VALUE_JAVA_LANG_OBJECT_NOT_NULL   = new ReferenceValue(ClassConstants.INTERNAL_NAME_JAVA_LANG_OBJECT, null, false);


    /**
     * Creates a new Value of the given type.
     * The type must be a fully specified internal type for primitives, classes,
     * or arrays.
     */
    public Value createValue(String type, Clazz referencedClass, boolean mayBeNull)
    {
        switch (type.charAt(0))
        {
            case ClassConstants.INTERNAL_TYPE_VOID:    return null;
            case ClassConstants.INTERNAL_TYPE_BOOLEAN:
            case ClassConstants.INTERNAL_TYPE_BYTE:
            case ClassConstants.INTERNAL_TYPE_CHAR:
            case ClassConstants.INTERNAL_TYPE_SHORT:
            case ClassConstants.INTERNAL_TYPE_INT:     return createIntegerValue();
            case ClassConstants.INTERNAL_TYPE_LONG:    return createLongValue();
            case ClassConstants.INTERNAL_TYPE_FLOAT:   return createFloatValue();
            case ClassConstants.INTERNAL_TYPE_DOUBLE:  return createDoubleValue();
            default:                                   return createReferenceValue(ClassUtil.isInternalArrayType(type) ?
                                                                                       type :
                                                                                       ClassUtil.internalClassNameFromClassType(type),
                                                                                   referencedClass,
                                                                                   mayBeNull);
        }
    }

    /**
     * Creates a new IntegerValue with an undefined value.
     */
    public IntegerValue createIntegerValue()
    {
        return INTEGER_VALUE;
    }

    /**
     * Creates a new IntegerValue with a given particular value.
     */
    public IntegerValue createIntegerValue(int value)
    {
        return createIntegerValue();
    }


    /**
     * Creates a new LongValue with an undefined value.
     */
    public LongValue createLongValue()
    {
        return LONG_VALUE;
    }

    /**
     * Creates a new LongValue with a given particular value.
     */
    public LongValue createLongValue(long value)
    {
        return createLongValue();
    }


    /**
     * Creates a new FloatValue with an undefined value.
     */
    public FloatValue createFloatValue()
    {
        return FLOAT_VALUE;
    }

    /**
     * Creates a new FloatValue with a given particular value.
     */
    public FloatValue createFloatValue(float value)
    {
        return createFloatValue();
    }


    /**
     * Creates a new DoubleValue with an undefined value.
     */
    public DoubleValue createDoubleValue()
    {
        return DOUBLE_VALUE;
    }

    /**
     * Creates a new DoubleValue with a given particular value.
     */
    public DoubleValue createDoubleValue(double value)
    {
        return createDoubleValue();
    }


    /**
     * Creates a new ReferenceValue that represents <code>null</code>.
     */
    public ReferenceValue createReferenceValueNull()
    {
        return REFERENCE_VALUE_NULL;
    }


    /**
     * Creates a new ReferenceValue of the given type. The type must be an
     * internal class name or an array type. If the type is <code>null</code>,
     * the ReferenceValue represents <code>null</code>.
     */
    public ReferenceValue createReferenceValue(String  type,
                                               Clazz   referencedClass,
                                               boolean mayBeNull)
    {
        return type == null                                                ? REFERENCE_VALUE_NULL                                 :
               !type.equals(ClassConstants.INTERNAL_NAME_JAVA_LANG_OBJECT) ? new ReferenceValue(type, referencedClass, mayBeNull) :
               mayBeNull                                                   ? REFERENCE_VALUE_JAVA_LANG_OBJECT_MAYBE_NULL          :
                                                                             REFERENCE_VALUE_JAVA_LANG_OBJECT_NOT_NULL;
    }


    /**
     * Creates a new ReferenceValue for arrays of the given type and length.
     * The type must be a fully specified internal type for primitives, classes,
     * or arrays.
     */
    public ReferenceValue createArrayReferenceValue(String       type,
                                                    Clazz        referencedClass,
                                                    IntegerValue arrayLength)
    {
        return createArrayReferenceValue(type,
                                         referencedClass,
                                         arrayLength,
                                         createValue(type, referencedClass, false));
    }


    /**
     * Creates a new ReferenceValue for arrays of the given type and length,
     * containing the given element. The type must be a fully specified internal
     * type for primitives, classes, or arrays.
     */
    public ReferenceValue createArrayReferenceValue(String       type,
                                                    Clazz        referencedClass,
                                                    IntegerValue arrayLength,
                                                    Value        elementValue)
    {
        return createReferenceValue(ClassConstants.INTERNAL_TYPE_ARRAY + type,
                                    referencedClass,
                                    false);
    }
}