summaryrefslogtreecommitdiff
path: root/src/proguard/optimize/MemberDescriptorSpecializer.java
blob: 465609269814f732a253bcb6a3819335051d14a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * ProGuard -- shrinking, optimization, obfuscation, and preverification
 *             of Java bytecode.
 *
 * 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
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package proguard.optimize;

import proguard.classfile.*;
import proguard.classfile.editor.ClassReferenceFixer;
import proguard.classfile.util.*;
import proguard.classfile.visitor.MemberVisitor;
import proguard.evaluation.value.Value;
import proguard.optimize.evaluation.StoringInvocationUnit;

/**
 * This MemberVisitor specializes parameters in the descriptors of the
 * methods that it visits.
 *
 * @see StoringInvocationUnit
 * @see ClassReferenceFixer
 * @author Eric Lafortune
 */
public class MemberDescriptorSpecializer
extends      SimplifiedVisitor
implements   MemberVisitor
{
    private static final boolean DEBUG = false;


    private final MemberVisitor extraParameterMemberVisitor;


    /**
     * Creates a new MethodDescriptorShrinker.
     */
    public MemberDescriptorSpecializer()
    {
        this(null);
    }


    /**
     * Creates a new MethodDescriptorShrinker with an extra visitor.
     * @param extraParameterMemberVisitor an optional extra visitor for all
     *                                    class members whose parameters have
     *                                    been specialized.
     */
    public MemberDescriptorSpecializer(MemberVisitor extraParameterMemberVisitor)
    {
        this.extraParameterMemberVisitor = extraParameterMemberVisitor;
    }


    // Implementations for MemberVisitor.

    public void visitProgramField(ProgramClass programClass, ProgramField programField)
    {
        Value parameterValue = StoringInvocationUnit.getFieldValue(programField);
        if (parameterValue.computationalType() == Value.TYPE_REFERENCE)
        {
            Clazz referencedClass = parameterValue.referenceValue().getReferencedClass();
            if (programField.referencedClass != referencedClass)
            {
                if (DEBUG)
                {
                    System.out.println("MemberDescriptorSpecializer: "+programClass.getName()+"."+programField.getName(programClass)+" "+programField.getDescriptor(programClass));
                    System.out.println("  "+programField.referencedClass.getName()+" -> "+referencedClass.getName());
                }

                programField.referencedClass = referencedClass;

                // Visit the field, if required.
                if (extraParameterMemberVisitor != null)
                {
                    extraParameterMemberVisitor.visitProgramField(programClass, programField);
                }
            }
        }
    }


    public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
    {
        // All parameters of non-static methods are shifted by one in the local
        // variable frame.
        int firstParameterIndex =
            (programMethod.getAccessFlags() & ClassConstants.ACC_STATIC) != 0 ?
                0 : 1;

        int parameterCount =
            ClassUtil.internalMethodParameterCount(programMethod.getDescriptor(programClass));

        int classIndex = 0;

        // Go over the parameters.
        for (int parameterIndex = firstParameterIndex; parameterIndex < parameterCount; parameterIndex++)
        {
            Value parameterValue = StoringInvocationUnit.getMethodParameterValue(programMethod, parameterIndex);
             if (parameterValue.computationalType() == Value.TYPE_REFERENCE)
             {
                 Clazz referencedClass = parameterValue.referenceValue().getReferencedClass();
                 if (programMethod.referencedClasses[classIndex] != referencedClass)
                 {
                     if (DEBUG)
                     {
                         System.out.println("MemberDescriptorSpecializer: "+programClass.getName()+"."+programMethod.getName(programClass)+programMethod.getDescriptor(programClass));
                         System.out.println("  "+programMethod.referencedClasses[classIndex].getName()+" -> "+referencedClass.getName());
                     }

                     programMethod.referencedClasses[classIndex] = referencedClass;

                     // Visit the method, if required.
                     if (extraParameterMemberVisitor != null)
                     {
                         extraParameterMemberVisitor.visitProgramMethod(programClass, programMethod);
                     }
                 }

                 classIndex++;
             }
        }
    }
}