diff options
Diffstat (limited to 'src/proguard/optimize/info/FieldOptimizationInfo.java')
-rw-r--r-- | src/proguard/optimize/info/FieldOptimizationInfo.java | 82 |
1 files changed, 33 insertions, 49 deletions
diff --git a/src/proguard/optimize/info/FieldOptimizationInfo.java b/src/proguard/optimize/info/FieldOptimizationInfo.java index 0fa9167..730eead 100644 --- a/src/proguard/optimize/info/FieldOptimizationInfo.java +++ b/src/proguard/optimize/info/FieldOptimizationInfo.java @@ -2,7 +2,7 @@ * ProGuard -- shrinking, optimization, obfuscation, and preverification * of Java bytecode. * - * Copyright (c) 2002-2013 Eric Lafortune (eric@graphics.cornell.edu) + * 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 @@ -21,11 +21,11 @@ package proguard.optimize.info; import proguard.classfile.*; -import proguard.classfile.attribute.visitor.*; import proguard.classfile.attribute.*; -import proguard.classfile.util.*; -import proguard.evaluation.value.*; +import proguard.classfile.attribute.visitor.*; +import proguard.classfile.util.SimplifiedVisitor; import proguard.evaluation.ConstantValueFactory; +import proguard.evaluation.value.*; /** * This class stores some optimization information that can be attached to @@ -37,8 +37,9 @@ public class FieldOptimizationInfo extends SimplifiedVisitor implements AttributeVisitor { - private static final SpecificValueFactory VALUE_FACTORY = new SpecificValueFactory(); - private static final ConstantValueFactory CONSTANT_VALUE_FACTORY = new ConstantValueFactory(VALUE_FACTORY); + private static final ParticularValueFactory VALUE_FACTORY = new ParticularValueFactory(); + private static final ConstantValueFactory CONSTANT_VALUE_FACTORY = new ConstantValueFactory(VALUE_FACTORY); + private static final InitialValueFactory INITIAL_VALUE_FACTORY = new InitialValueFactory(VALUE_FACTORY); private boolean isWritten; private boolean isRead; @@ -52,20 +53,9 @@ implements AttributeVisitor int accessFlags = field.getAccessFlags(); isWritten = - isRead = (accessFlags & ClassConstants.INTERNAL_ACC_VOLATILE) != 0; + isRead = (accessFlags & ClassConstants.ACC_VOLATILE) != 0; - if ((accessFlags & ClassConstants.INTERNAL_ACC_STATIC) != 0) - { - // See if we can initialize the static field with a constant value. - field.accept(clazz, new AllAttributeVisitor(this)); - } - - if ((accessFlags & ClassConstants.INTERNAL_ACC_FINAL) == 0 && - value == null) - { - // Otherwise initialize the non-final field with the default value. - value = initialValue(field.getDescriptor(clazz)); - } + resetValue(clazz, field); } @@ -129,6 +119,30 @@ implements AttributeVisitor } + public void resetValue(Clazz clazz, Field field) + { + int accessFlags = field.getAccessFlags(); + + value = null; + + // See if we can initialize a static field with a constant value. + if ((accessFlags & ClassConstants.ACC_STATIC) != 0) + { + field.accept(clazz, new AllAttributeVisitor(this)); + } + + // Otherwise initialize a non-final field with the default value. + // Conservatively, even a final field needs to be initialized with the + // default value, because it may be accessed before it is set. + if (value == null && + (SideEffectInstructionChecker.OPTIMIZE_CONSERVATIVELY || + (accessFlags & ClassConstants.ACC_FINAL) == 0)) + { + value = INITIAL_VALUE_FACTORY.createValue(field.getDescriptor(clazz)); + } + } + + public void generalizeValue(Value value) { this.value = this.value != null ? @@ -157,36 +171,6 @@ implements AttributeVisitor // Small utility methods. - private Value initialValue(String type) - { - switch (type.charAt(0)) - { - case ClassConstants.INTERNAL_TYPE_BOOLEAN: - case ClassConstants.INTERNAL_TYPE_BYTE: - case ClassConstants.INTERNAL_TYPE_CHAR: - case ClassConstants.INTERNAL_TYPE_SHORT: - case ClassConstants.INTERNAL_TYPE_INT: - return VALUE_FACTORY.createIntegerValue(0); - - case ClassConstants.INTERNAL_TYPE_LONG: - return VALUE_FACTORY.createLongValue(0L); - - case ClassConstants.INTERNAL_TYPE_FLOAT: - return VALUE_FACTORY.createFloatValue(0.0f); - - case ClassConstants.INTERNAL_TYPE_DOUBLE: - return VALUE_FACTORY.createDoubleValue(0.0); - - case ClassConstants.INTERNAL_TYPE_CLASS_START: - case ClassConstants.INTERNAL_TYPE_ARRAY: - return VALUE_FACTORY.createReferenceValueNull(); - - default: - throw new IllegalArgumentException("Invalid type ["+type+"]"); - } - } - - public static void setFieldOptimizationInfo(Clazz clazz, Field field) { field.setVisitorInfo(new FieldOptimizationInfo(clazz, field)); |