001: /*
002: * ProGuard -- shrinking, optimization, obfuscation, and preverification
003: * of Java bytecode.
004: *
005: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the Free
009: * Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful, but WITHOUT
013: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
015: * more details.
016: *
017: * You should have received a copy of the GNU General Public License along
018: * with this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: package proguard.optimize.info;
022:
023: import proguard.classfile.*;
024: import proguard.classfile.attribute.CodeAttribute;
025: import proguard.classfile.constant.*;
026: import proguard.classfile.constant.visitor.ConstantVisitor;
027: import proguard.classfile.instruction.*;
028: import proguard.classfile.instruction.visitor.InstructionVisitor;
029: import proguard.classfile.util.SimplifiedVisitor;
030: import proguard.classfile.visitor.MemberVisitor;
031:
032: /**
033: * This InstructionVisitor marks all fields that are write-only.
034: *
035: * @author Eric Lafortune
036: */
037: public class ReadWriteFieldMarker extends SimplifiedVisitor implements
038: InstructionVisitor, ConstantVisitor, MemberVisitor {
039: // Parameters for the visitor methods.
040: private boolean reading;
041: private boolean writing;
042:
043: // Implementations for InstructionVisitor.
044:
045: public void visitAnyInstruction(Clazz clazz, Method method,
046: CodeAttribute codeAttribute, int offset,
047: Instruction instruction) {
048: }
049:
050: public void visitConstantInstruction(Clazz clazz, Method method,
051: CodeAttribute codeAttribute, int offset,
052: ConstantInstruction constantInstruction) {
053: byte opcode = constantInstruction.opcode;
054:
055: // Check for instructions that involve fields.
056: switch (opcode) {
057: case InstructionConstants.OP_LDC:
058: case InstructionConstants.OP_LDC_W:
059: // Mark the field, if any, as being read from and written to.
060: reading = true;
061: writing = true;
062: clazz.constantPoolEntryAccept(
063: constantInstruction.constantIndex, this );
064: break;
065:
066: case InstructionConstants.OP_GETSTATIC:
067: case InstructionConstants.OP_GETFIELD:
068: // Mark the field as being read from.
069: reading = true;
070: writing = false;
071: clazz.constantPoolEntryAccept(
072: constantInstruction.constantIndex, this );
073: break;
074:
075: case InstructionConstants.OP_PUTSTATIC:
076: case InstructionConstants.OP_PUTFIELD:
077: // Mark the field as being written to.
078: reading = false;
079: writing = true;
080: clazz.constantPoolEntryAccept(
081: constantInstruction.constantIndex, this );
082: break;
083: }
084: }
085:
086: // Implementations for ConstantVisitor.
087:
088: public void visitAnyConstant(Clazz clazz, Constant constant) {
089: }
090:
091: public void visitStringConstant(Clazz clazz,
092: StringConstant stringConstant) {
093: // Mark the referenced field, if any.
094: stringConstant.referencedMemberAccept(this );
095: }
096:
097: public void visitFieldrefConstant(Clazz clazz,
098: FieldrefConstant fieldrefConstant) {
099: // Mark the referenced field.
100: fieldrefConstant.referencedMemberAccept(this );
101: }
102:
103: // Implementations for MemberVisitor.
104:
105: public void visitAnyMember(Clazz Clazz, Member member) {
106: }
107:
108: public void visitProgramField(ProgramClass programClass,
109: ProgramField programField) {
110: // Mark the field if it is being read from.
111: if (reading) {
112: markAsRead(programField);
113: }
114:
115: // Mark the field if it is being written to.
116: if (writing) {
117: markAsWritten(programField);
118: }
119: }
120:
121: // Small utility methods.
122:
123: private static void markAsRead(Field field) {
124: FieldOptimizationInfo info = FieldOptimizationInfo
125: .getFieldOptimizationInfo(field);
126: if (info != null) {
127: info.setRead();
128: }
129: }
130:
131: public static boolean isRead(Field field) {
132: FieldOptimizationInfo info = FieldOptimizationInfo
133: .getFieldOptimizationInfo(field);
134: return info == null || info.isRead();
135: }
136:
137: private static void markAsWritten(Field field) {
138: FieldOptimizationInfo info = FieldOptimizationInfo
139: .getFieldOptimizationInfo(field);
140: if (info != null) {
141: info.setWritten();
142: }
143: }
144:
145: public static boolean isWritten(Field field) {
146: FieldOptimizationInfo info = FieldOptimizationInfo
147: .getFieldOptimizationInfo(field);
148: return info == null || info.isWritten();
149: }
150: }
|