summaryrefslogtreecommitdiff
path: root/src/proguard/optimize/evaluation/StoringInvocationUnit.java
blob: 271b65424948da8dcc10fc9fae32227c3089cb14 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * ProGuard -- shrinking, optimization, obfuscation, and preverification
 *             of Java bytecode.
 *
 * Copyright (c) 2002-2014 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.optimize.evaluation;

import proguard.classfile.*;
import proguard.classfile.constant.RefConstant;
import proguard.evaluation.BasicInvocationUnit;
import proguard.evaluation.value.*;
import proguard.optimize.info.*;

/**
 * This InvocationUnit stores parameter values and return values with the
 * methods that are invoked.
 *
 * @see LoadingInvocationUnit
 * @author Eric Lafortune
 */
public class StoringInvocationUnit
extends      BasicInvocationUnit
{
    private boolean storeFieldValues;
    private boolean storeMethodParameterValues;
    private boolean storeMethodReturnValues;


    /**
     * Creates a new StoringInvocationUnit with the given value factory.
     */
    public StoringInvocationUnit(ValueFactory valueFactory)
    {
        this(valueFactory, true, true, true);
    }


    /**
     * Creates a new StoringInvocationUnit with the given value factory, for
     * storing the specified values.
     */
    public StoringInvocationUnit(ValueFactory valueFactory,
                                 boolean      storeFieldValues,
                                 boolean      storeMethodParameterValues,
                                 boolean      storeMethodReturnValues)
    {
        super(valueFactory);

        this.storeFieldValues           = storeFieldValues;
        this.storeMethodParameterValues = storeMethodParameterValues;
        this.storeMethodReturnValues    = storeMethodReturnValues;
    }


    // Implementations for BasicInvocationUnit.

    protected void setFieldClassValue(Clazz          clazz,
                                      RefConstant    refConstant,
                                      ReferenceValue value)
    {
        if (storeFieldValues)
        {
            Member referencedMember = refConstant.referencedMember;
            if (referencedMember != null)
            {
                generalizeFieldClassValue((Field)referencedMember, value);
            }
        }
    }


    protected void setFieldValue(Clazz       clazz,
                                 RefConstant refConstant,
                                 Value       value)
    {
        if (storeFieldValues)
        {
            Member referencedMember = refConstant.referencedMember;
            if (referencedMember != null)
            {
                generalizeFieldValue((Field)referencedMember, value);
            }
        }
    }


    protected void setMethodParameterValue(Clazz       clazz,
                                           RefConstant refConstant,
                                           int         parameterIndex,
                                           Value       value)
    {
        if (storeMethodParameterValues)
        {
            Member referencedMember = refConstant.referencedMember;
            if (referencedMember != null)
            {
                generalizeMethodParameterValue((Method)referencedMember,
                                               parameterIndex,
                                               value);
            }
        }
    }


    protected void setMethodReturnValue(Clazz  clazz,
                                        Method method,
                                        Value  value)
    {
        if (storeMethodReturnValues)
        {
            generalizeMethodReturnValue(method, value);
        }
    }


    // Small utility methods.

    private static void generalizeFieldClassValue(Field field, ReferenceValue value)
    {
        FieldOptimizationInfo info = FieldOptimizationInfo.getFieldOptimizationInfo(field);
        if (info != null)
        {
            info.generalizeReferencedClass(value);
        }
    }


    public static ReferenceValue getFieldClassValue(Field field)
    {
        FieldOptimizationInfo info = FieldOptimizationInfo.getFieldOptimizationInfo(field);
        return info != null ?
            info.getReferencedClass() :
            null;
    }


    private static void generalizeFieldValue(Field field, Value value)
    {
        FieldOptimizationInfo info = FieldOptimizationInfo.getFieldOptimizationInfo(field);
        if (info != null)
        {
            info.generalizeValue(value);
        }
    }


    public static Value getFieldValue(Field field)
    {
        FieldOptimizationInfo info = FieldOptimizationInfo.getFieldOptimizationInfo(field);
        return info != null ?
            info.getValue() :
            null;
    }


    private static void generalizeMethodParameterValue(Method method, int parameterIndex, Value value)
    {
        MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
        if (info != null)
        {
            info.generalizeParameter(parameterIndex, value);
        }
    }


    public static Value getMethodParameterValue(Method method, int parameterIndex)
    {
        MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
        return info != null ?
            info.getParameter(parameterIndex) :
            null;
    }


    private static void generalizeMethodReturnValue(Method method, Value value)
    {
        MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
        if (info != null)
        {
            info.generalizeReturnValue(value);
        }
    }


    public static Value getMethodReturnValue(Method method)
    {
        MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
        return info != null ?
            info.getReturnValue() :
            null;
    }
}