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.evaluation;
22
23import proguard.classfile.*;
24import proguard.classfile.util.SimplifiedVisitor;
25import proguard.classfile.visitor.*;
26import proguard.evaluation.value.*;
27import proguard.optimize.info.*;
28
29/**
30 * This ClassVisitor propagates the value of the $VALUES field to the values()
31 * method in the simple enum classes that it visits.
32 *
33 * @see SimpleEnumMarker
34 * @author Eric Lafortune
35 */
36public class SimpleEnumArrayPropagator
37extends      SimplifiedVisitor
38implements   ClassVisitor,
39             MemberVisitor
40{
41    private final ValueFactory valueFactory = new ParticularValueFactory();
42
43    private Value array;
44
45
46    // Implementations for ClassVisitor.
47
48    public void visitProgramClass(ProgramClass programClass)
49    {
50        // Update the return value of the "int[] values()" method.
51        programClass.methodsAccept(new MemberDescriptorFilter("()[I", this));
52    }
53
54
55    // Implementations for MemberVisitor.
56
57    public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
58    {
59        // Find the array length of the "int[] $VALUES" field.
60        programClass.fieldsAccept(new MemberDescriptorFilter("[I", this));
61
62        if (array != null)
63        {
64            // Set the array value with the found array length. We can't use
65            // the original array, because its elements might get overwritten.
66            Value propagatedArray =
67                valueFactory.createArrayReferenceValue("I",
68                                                       null,
69                                                       array.referenceValue().arrayLength(
70                                                           valueFactory));
71
72            setMethodReturnValue(programMethod, propagatedArray);
73
74            array = null;
75        }
76    }
77
78    public void visitProgramField(ProgramClass programClass, ProgramField programField)
79    {
80        array = StoringInvocationUnit.getFieldValue(programField);
81    }
82
83
84    // Small utility methods.
85
86    private static void setMethodReturnValue(Method method, Value value)
87    {
88        MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
89        if (info != null)
90        {
91            info.setReturnValue(value);
92        }
93    }
94}
95