1/* 2 * ProGuard -- shrinking, optimization, obfuscation, and preverification 3 * of Java bytecode. 4 * 5 * Copyright (c) 2002-2014 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.*; 25import proguard.classfile.attribute.visitor.*; 26import proguard.classfile.util.SimplifiedVisitor; 27import proguard.evaluation.ConstantValueFactory; 28import proguard.evaluation.value.*; 29 30/** 31 * This class stores some optimization information that can be attached to 32 * a field. 33 * 34 * @author Eric Lafortune 35 */ 36public class FieldOptimizationInfo 37extends SimplifiedVisitor 38implements AttributeVisitor 39{ 40 private static final ParticularValueFactory VALUE_FACTORY = new ParticularValueFactory(); 41 private static final ConstantValueFactory CONSTANT_VALUE_FACTORY = new ConstantValueFactory(VALUE_FACTORY); 42 private static final InitialValueFactory INITIAL_VALUE_FACTORY = new InitialValueFactory(VALUE_FACTORY); 43 44 private boolean isWritten; 45 private boolean isRead; 46 private boolean canBeMadePrivate = true; 47 private ReferenceValue referencedClass; 48 private Value value; 49 50 51 public FieldOptimizationInfo(Clazz clazz, Field field) 52 { 53 int accessFlags = field.getAccessFlags(); 54 55 isWritten = 56 isRead = (accessFlags & ClassConstants.ACC_VOLATILE) != 0; 57 58 resetValue(clazz, field); 59 } 60 61 62 public FieldOptimizationInfo(FieldOptimizationInfo FieldOptimizationInfo) 63 { 64 this.isWritten = FieldOptimizationInfo.isWritten; 65 this.isRead = FieldOptimizationInfo.isRead; 66 this.canBeMadePrivate = FieldOptimizationInfo.canBeMadePrivate; 67 this.referencedClass = FieldOptimizationInfo.referencedClass; 68 this.value = FieldOptimizationInfo.value; 69 } 70 71 72 public void setWritten() 73 { 74 isWritten = true; 75 } 76 77 78 public boolean isWritten() 79 { 80 return isWritten; 81 } 82 83 84 public void setRead() 85 { 86 isRead = true; 87 } 88 89 90 public boolean isRead() 91 { 92 return isRead; 93 } 94 95 96 public void setCanNotBeMadePrivate() 97 { 98 canBeMadePrivate = false; 99 } 100 101 102 public boolean canBeMadePrivate() 103 { 104 return canBeMadePrivate; 105 } 106 107 108 public void generalizeReferencedClass(ReferenceValue referencedClass) 109 { 110 this.referencedClass = this.referencedClass != null ? 111 this.referencedClass.generalize(referencedClass) : 112 referencedClass; 113 } 114 115 116 public ReferenceValue getReferencedClass() 117 { 118 return referencedClass; 119 } 120 121 122 public void resetValue(Clazz clazz, Field field) 123 { 124 int accessFlags = field.getAccessFlags(); 125 126 value = null; 127 128 // See if we can initialize a static field with a constant value. 129 if ((accessFlags & ClassConstants.ACC_STATIC) != 0) 130 { 131 field.accept(clazz, new AllAttributeVisitor(this)); 132 } 133 134 // Otherwise initialize a non-final field with the default value. 135 // Conservatively, even a final field needs to be initialized with the 136 // default value, because it may be accessed before it is set. 137 if (value == null && 138 (SideEffectInstructionChecker.OPTIMIZE_CONSERVATIVELY || 139 (accessFlags & ClassConstants.ACC_FINAL) == 0)) 140 { 141 value = INITIAL_VALUE_FACTORY.createValue(field.getDescriptor(clazz)); 142 } 143 } 144 145 146 public void generalizeValue(Value value) 147 { 148 this.value = this.value != null ? 149 this.value.generalize(value) : 150 value; 151 } 152 153 154 public Value getValue() 155 { 156 return value; 157 } 158 159 160 // Implementations for AttributeVisitor. 161 162 public void visitAnyAttribute(Clazz clazz, Attribute attribute) {} 163 164 165 public void visitConstantValueAttribute(Clazz clazz, Field field, ConstantValueAttribute constantValueAttribute) 166 { 167 // Retrieve the initial static field value. 168 value = CONSTANT_VALUE_FACTORY.constantValue(clazz, constantValueAttribute.u2constantValueIndex); 169 } 170 171 172 // Small utility methods. 173 174 public static void setFieldOptimizationInfo(Clazz clazz, Field field) 175 { 176 field.setVisitorInfo(new FieldOptimizationInfo(clazz, field)); 177 } 178 179 180 public static FieldOptimizationInfo getFieldOptimizationInfo(Field field) 181 { 182 Object visitorInfo = field.getVisitorInfo(); 183 184 return visitorInfo instanceof FieldOptimizationInfo ? 185 (FieldOptimizationInfo)visitorInfo : 186 null; 187 } 188} 189