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 library 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 library 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 Lesser General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this library; if not, write to the Free Software Foundation,
019: * Inc., 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.util.MethodLinker;
025: import proguard.evaluation.value.*;
026:
027: /**
028: * This class stores some optimization information that can be attached to
029: * a field.
030: *
031: * @author Eric Lafortune
032: */
033: public class FieldOptimizationInfo {
034: private static final SpecificValueFactory VALUE_FACTORY = new SpecificValueFactory();
035:
036: private boolean isWritten;
037: private boolean isRead;
038: private boolean canBeMadePrivate = true;
039: private ReferenceValue referencedClass;
040: private Value value;
041:
042: public FieldOptimizationInfo(Clazz clazz, Field field) {
043: isWritten = isRead = (field.getAccessFlags() & ClassConstants.INTERNAL_ACC_VOLATILE) != 0;
044: value = initialValue(field.getDescriptor(clazz));
045: }
046:
047: public void setWritten() {
048: isWritten = true;
049: }
050:
051: public boolean isWritten() {
052: return isWritten;
053: }
054:
055: public void setRead() {
056: isRead = true;
057: }
058:
059: public boolean isRead() {
060: return isRead;
061: }
062:
063: public void setCanNotBeMadePrivate() {
064: canBeMadePrivate = false;
065: }
066:
067: public boolean canBeMadePrivate() {
068: return canBeMadePrivate;
069: }
070:
071: public void generalizeReferencedClass(ReferenceValue referencedClass) {
072: this .referencedClass = this .referencedClass != null ? this .referencedClass
073: .generalize(referencedClass)
074: : referencedClass;
075: }
076:
077: public ReferenceValue getReferencedClass() {
078: return referencedClass;
079: }
080:
081: public void generalizeValue(Value value) {
082: this .value = this .value != null ? this .value.generalize(value)
083: : value;
084: }
085:
086: public Value getValue() {
087: return value;
088: }
089:
090: // Small utility methods.
091:
092: private Value initialValue(String type) {
093: switch (type.charAt(0)) {
094: case ClassConstants.INTERNAL_TYPE_BOOLEAN:
095: case ClassConstants.INTERNAL_TYPE_BYTE:
096: case ClassConstants.INTERNAL_TYPE_CHAR:
097: case ClassConstants.INTERNAL_TYPE_SHORT:
098: case ClassConstants.INTERNAL_TYPE_INT:
099: return VALUE_FACTORY.createIntegerValue(0);
100:
101: case ClassConstants.INTERNAL_TYPE_LONG:
102: return VALUE_FACTORY.createLongValue(0L);
103:
104: case ClassConstants.INTERNAL_TYPE_FLOAT:
105: return VALUE_FACTORY.createFloatValue(0.0f);
106:
107: case ClassConstants.INTERNAL_TYPE_DOUBLE:
108: return VALUE_FACTORY.createDoubleValue(0.0);
109:
110: case ClassConstants.INTERNAL_TYPE_CLASS_START:
111: case ClassConstants.INTERNAL_TYPE_ARRAY:
112: return VALUE_FACTORY.createReferenceValueNull();
113:
114: default:
115: throw new IllegalArgumentException("Invalid type [" + type
116: + "]");
117: }
118: }
119:
120: public static void setFieldOptimizationInfo(Clazz clazz, Field field) {
121: MethodLinker.lastMember(field).setVisitorInfo(
122: new FieldOptimizationInfo(clazz, field));
123: }
124:
125: public static FieldOptimizationInfo getFieldOptimizationInfo(
126: Field field) {
127: Object visitorInfo = MethodLinker.lastMember(field)
128: .getVisitorInfo();
129:
130: return visitorInfo instanceof FieldOptimizationInfo ? (FieldOptimizationInfo) visitorInfo
131: : null;
132: }
133: }
|