1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2009 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.util.SimplifiedVisitor;
25import proguard.classfile.visitor.MemberVisitor;
26
27import java.util.Map;
28
29/**
30 * This MemberVisitor collects all new (obfuscation) names of the members
31 * that it visits.
32 *
33 * @see MemberObfuscator
34 *
35 * @author Eric Lafortune
36 */
37public class MemberNameCollector
38extends      SimplifiedVisitor
39implements   MemberVisitor
40{
41    private final boolean allowAggressiveOverloading;
42    private final Map     descriptorMap;
43
44
45    /**
46     * Creates a new MemberNameCollector.
47     * @param allowAggressiveOverloading a flag that specifies whether class
48     *                                   members can be overloaded aggressively.
49     * @param descriptorMap              the map of descriptors to
50     *                                   [new name - old name] maps.
51     */
52    public MemberNameCollector(boolean allowAggressiveOverloading,
53                               Map     descriptorMap)
54    {
55        this.allowAggressiveOverloading = allowAggressiveOverloading;
56        this.descriptorMap              = descriptorMap;
57    }
58
59
60    // Implementations for MemberVisitor.
61
62    public void visitAnyMember(Clazz clazz, Member member)
63    {
64        // Special cases: <clinit> and <init> are always kept unchanged.
65        // We can ignore them here.
66        String name = member.getName(clazz);
67        if (name.equals(ClassConstants.INTERNAL_METHOD_NAME_CLINIT) ||
68            name.equals(ClassConstants.INTERNAL_METHOD_NAME_INIT))
69        {
70            return;
71        }
72
73        // Get the member's new name.
74        String newName = MemberObfuscator.newMemberName(member);
75
76        // Remember it, if it has already been set.
77        if (newName != null)
78        {
79            // Get the member's descriptor.
80            String descriptor = member.getDescriptor(clazz);
81
82            // Check whether we're allowed to do aggressive overloading
83            if (!allowAggressiveOverloading)
84            {
85                // Trim the return argument from the descriptor if not.
86                // Works for fields and methods alike.
87                descriptor = descriptor.substring(0, descriptor.indexOf(')')+1);
88            }
89
90            // Put the [descriptor - new name] in the map,
91            // creating a new [new name - old name] map if necessary.
92            Map nameMap = MemberObfuscator.retrieveNameMap(descriptorMap, descriptor);
93
94            // Isn't there another original name for this new name, or should
95            // this original name get priority?
96            String otherName = (String)nameMap.get(newName);
97            if (otherName == null                              ||
98                MemberObfuscator.hasFixedNewMemberName(member) ||
99                name.compareTo(otherName) < 0)
100            {
101                // Remember not to use the new name again in this name space.
102                nameMap.put(newName, name);
103            }
104        }
105    }
106}
107