ReadWriteFieldMarker.java revision db267bc191f906f55eaef21a27110cce2ec57fdf
1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2009 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 */
21package proguard.optimize.info;
22
23import proguard.classfile.*;
24import proguard.classfile.attribute.CodeAttribute;
25import proguard.classfile.constant.*;
26import proguard.classfile.constant.visitor.ConstantVisitor;
27import proguard.classfile.instruction.*;
28import proguard.classfile.instruction.visitor.InstructionVisitor;
29import proguard.classfile.util.SimplifiedVisitor;
30import proguard.classfile.visitor.MemberVisitor;
31
32/**
33 * This InstructionVisitor marks all fields that are write-only.
34 *
35 * @author Eric Lafortune
36 */
37public class ReadWriteFieldMarker
38extends      SimplifiedVisitor
39implements   InstructionVisitor,
40             ConstantVisitor,
41             MemberVisitor
42{
43    // Parameters for the visitor methods.
44    private boolean reading = true;
45    private boolean writing = true;
46
47
48    // Implementations for InstructionVisitor.
49
50    public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) {}
51
52
53    public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
54    {
55        byte opcode = constantInstruction.opcode;
56
57        // Check for instructions that involve fields.
58        switch (opcode)
59        {
60            case InstructionConstants.OP_LDC:
61            case InstructionConstants.OP_LDC_W:
62                // Mark the field, if any, as being read from and written to.
63                reading = true;
64                writing = true;
65                clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
66                break;
67
68            case InstructionConstants.OP_GETSTATIC:
69            case InstructionConstants.OP_GETFIELD:
70                // Mark the field as being read from.
71                reading = true;
72                writing = false;
73                clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
74                break;
75
76            case InstructionConstants.OP_PUTSTATIC:
77            case InstructionConstants.OP_PUTFIELD:
78                // Mark the field as being written to.
79                reading = false;
80                writing = true;
81                clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
82                break;
83        }
84    }
85
86
87    // Implementations for ConstantVisitor.
88
89    public void visitAnyConstant(Clazz clazz, Constant constant) {}
90
91
92    public void visitStringConstant(Clazz clazz, StringConstant stringConstant)
93    {
94        // Mark the referenced field, if any.
95        stringConstant.referencedMemberAccept(this);
96    }
97
98
99    public void visitFieldrefConstant(Clazz clazz, FieldrefConstant fieldrefConstant)
100    {
101        // Mark the referenced field.
102        fieldrefConstant.referencedMemberAccept(this);
103    }
104
105
106    // Implementations for MemberVisitor.
107
108    public void visitAnyMember(Clazz Clazz, Member member) {}
109
110
111    public void visitProgramField(ProgramClass programClass, ProgramField programField)
112    {
113        // Mark the field if it is being read from.
114        if (reading)
115        {
116            markAsRead(programField);
117        }
118
119        // Mark the field if it is being written to.
120        if (writing)
121        {
122            markAsWritten(programField);
123        }
124    }
125
126
127    // Small utility methods.
128
129    private static void markAsRead(Field field)
130    {
131        FieldOptimizationInfo info = FieldOptimizationInfo.getFieldOptimizationInfo(field);
132        if (info != null)
133        {
134            info.setRead();
135        }
136    }
137
138
139    public static boolean isRead(Field field)
140    {
141        FieldOptimizationInfo info = FieldOptimizationInfo.getFieldOptimizationInfo(field);
142        return info == null ||
143               info.isRead();
144    }
145
146
147    private static void markAsWritten(Field field)
148    {
149        FieldOptimizationInfo info = FieldOptimizationInfo.getFieldOptimizationInfo(field);
150        if (info != null)
151        {
152            info.setWritten();
153        }
154    }
155
156
157    public static boolean isWritten(Field field)
158    {
159        FieldOptimizationInfo info = FieldOptimizationInfo.getFieldOptimizationInfo(field);
160        return info == null ||
161               info.isWritten();
162    }
163}
164