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.obfuscate;
22
23import proguard.classfile.*;
24import proguard.classfile.attribute.*;
25import proguard.classfile.attribute.visitor.AttributeVisitor;
26import proguard.classfile.util.SimplifiedVisitor;
27
28/**
29 * This AttributeVisitor trims and marks all local variable (type) table
30 * attributes that it visits. It keeps parameter names and types and removes
31 * the ordinary local variable names and types.
32 *
33 * @author Eric Lafortune
34 */
35public class ParameterNameMarker
36extends      SimplifiedVisitor
37implements   AttributeVisitor
38{
39    private final AttributeVisitor attributeUsageMarker;
40
41
42    /**
43     * Constructs a new ParameterNameMarker.
44     * @param attributeUsageMarker the marker that will be used to mark
45     *                             attributes containing local variable info.
46     */
47    public ParameterNameMarker(AttributeVisitor attributeUsageMarker)
48    {
49        this.attributeUsageMarker = attributeUsageMarker;
50    }
51
52
53    // Implementations for AttributeVisitor.
54
55    public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
56
57
58    public void visitLocalVariableTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTableAttribute localVariableTableAttribute)
59    {
60        if (!AttributeUsageMarker.isUsed(localVariableTableAttribute) &&
61            hasParameters(clazz, method))
62        {
63            // Shift the entries that start at offset 0 to the front.
64            int newIndex = 0;
65
66            for (int index = 0; index < localVariableTableAttribute.u2localVariableTableLength; index++)
67            {
68                LocalVariableInfo localVariableInfo =
69                    localVariableTableAttribute.localVariableTable[index];
70
71                if (localVariableInfo.u2startPC == 0)
72                {
73                    localVariableTableAttribute.localVariableTable[newIndex++] =
74                        localVariableInfo;
75                }
76            }
77
78            // Trim the table.
79            localVariableTableAttribute.u2localVariableTableLength = newIndex;
80
81            // Mark the table if there are any entries.
82            if (newIndex > 0)
83            {
84                attributeUsageMarker.visitLocalVariableTableAttribute(clazz, method, codeAttribute, localVariableTableAttribute);
85            }
86        }
87    }
88
89
90    public void visitLocalVariableTypeTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeTableAttribute localVariableTypeTableAttribute)
91    {
92        if (!AttributeUsageMarker.isUsed(localVariableTypeTableAttribute) &&
93            hasParameters(clazz, method))
94        {
95            // Shift the entries that start at offset 0 to the front.
96            int newIndex = 0;
97
98            for (int index = 0; index < localVariableTypeTableAttribute.u2localVariableTypeTableLength; index++)
99            {
100                LocalVariableTypeInfo localVariableTypeInfo =
101                    localVariableTypeTableAttribute.localVariableTypeTable[index];
102
103                if (localVariableTypeInfo.u2startPC == 0)
104                {
105                    localVariableTypeTableAttribute.localVariableTypeTable[newIndex++] =
106                        localVariableTypeInfo;
107                }
108            }
109
110            // Trim the table.
111            localVariableTypeTableAttribute.u2localVariableTypeTableLength = newIndex;
112
113            // Mark the table if there are any entries.
114            if (newIndex > 0)
115            {
116                attributeUsageMarker.visitLocalVariableTypeTableAttribute(clazz, method, codeAttribute, localVariableTypeTableAttribute);
117            }
118        }
119    }
120
121
122    // Small utility methods.
123
124    private boolean hasParameters(Clazz clazz, Method method)
125    {
126        return method.getDescriptor(clazz).charAt(1) != ClassConstants.METHOD_ARGUMENTS_CLOSE;
127    }
128}