~ubuntu-branches/ubuntu/maverick/proguard/maverick

« back to all changes in this revision

Viewing changes to src/proguard/obfuscate/MemberInfoNameCollector.java

  • Committer: Bazaar Package Importer
  • Author(s): Sam Clegg
  • Date: 2005-11-13 09:42:59 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051113094259-432zf4yyw4890mmn
Tags: 3.4-1
* New upstream release (Closes: #338355)
* debian/control: bump standards version
* debian/copyright: update FSF address
* increase java stack size for proguard and proguardgui

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: MemberInfoNameCollector.java,v 1.3 2005/06/11 13:21:35 eric Exp $
 
2
 *
 
3
 * ProGuard -- shrinking, optimization, and obfuscation of Java class files.
 
4
 *
 
5
 * Copyright (c) 2002-2005 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
 */
 
21
package proguard.obfuscate;
 
22
 
 
23
import proguard.classfile.*;
 
24
import proguard.classfile.visitor.*;
 
25
 
 
26
import java.io.IOException;
 
27
import java.util.*;
 
28
 
 
29
 
 
30
/**
 
31
 * This MemberInfoVisitor collects all new (obfuscation) names of the members
 
32
 * that it visits.
 
33
 *
 
34
 * @see MemberInfoLinker
 
35
 * @see MemberInfoObfuscator
 
36
 *
 
37
 * @author Eric Lafortune
 
38
 */
 
39
public class MemberInfoNameCollector implements MemberInfoVisitor
 
40
{
 
41
    private boolean allowAggressiveOverloading;
 
42
    private Map     descriptorMap;
 
43
 
 
44
 
 
45
    /**
 
46
     * Creates a new MemberInfoNameCollector.
 
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 MemberInfoNameCollector(boolean allowAggressiveOverloading,
 
53
                                   Map     descriptorMap)
 
54
    {
 
55
        this.allowAggressiveOverloading = allowAggressiveOverloading;
 
56
        this.descriptorMap              = descriptorMap;
 
57
    }
 
58
 
 
59
 
 
60
    // Implementations for MemberInfoVisitor.
 
61
 
 
62
    public void visitProgramFieldInfo(ProgramClassFile programClassFile, ProgramFieldInfo programFieldInfo)
 
63
    {
 
64
        collectName(programClassFile, programFieldInfo);
 
65
    }
 
66
 
 
67
 
 
68
    public void visitProgramMethodInfo(ProgramClassFile programClassFile, ProgramMethodInfo programMethodInfo)
 
69
    {
 
70
        collectName(programClassFile, programMethodInfo);
 
71
    }
 
72
 
 
73
 
 
74
    public void visitLibraryFieldInfo(LibraryClassFile libraryClassFile, LibraryFieldInfo libraryFieldInfo)
 
75
    {
 
76
        collectName(libraryClassFile, libraryFieldInfo);
 
77
    }
 
78
 
 
79
 
 
80
    public void visitLibraryMethodInfo(LibraryClassFile libraryClassFile, LibraryMethodInfo libraryMethodInfo)
 
81
    {
 
82
        collectName(libraryClassFile, libraryMethodInfo);
 
83
    }
 
84
 
 
85
 
 
86
    /**
 
87
     * Inserts the new name of the given class member into the map.
 
88
     * @param classFile  the class file of the given member.
 
89
     * @param memberInfo the class member to be linked.
 
90
     */
 
91
    private void collectName(ClassFile classFile, MemberInfo memberInfo)
 
92
    {
 
93
        // Special cases: <clinit> and <init> are always kept unchanged.
 
94
        // We can ignore them here.
 
95
        String name = memberInfo.getName(classFile);
 
96
        if (name.equals(ClassConstants.INTERNAL_METHOD_NAME_CLINIT) ||
 
97
            name.equals(ClassConstants.INTERNAL_METHOD_NAME_INIT))
 
98
        {
 
99
            return;
 
100
        }
 
101
 
 
102
        // Get the member's new name.
 
103
        String newName = MemberInfoObfuscator.newMemberName(memberInfo);
 
104
 
 
105
        // Remember it, if it has already been set.
 
106
        if (newName != null)
 
107
        {
 
108
            // Get the member's descriptor.
 
109
            String descriptor = memberInfo.getDescriptor(classFile);
 
110
 
 
111
            // Check whether we're allowed to do aggressive overloading
 
112
            if (!allowAggressiveOverloading)
 
113
            {
 
114
                // Trim the return argument from the descriptor if not.
 
115
                // Works for fields and methods alike.
 
116
                descriptor = descriptor.substring(0, descriptor.indexOf(')')+1);
 
117
            }
 
118
 
 
119
            // Put the [descriptor - new name] in the map,
 
120
            // creating a new [new name - old name] map if necessary.
 
121
            Map newNameMap = MemberInfoObfuscator.retrieveNameMap(descriptorMap, descriptor);
 
122
 
 
123
            // Is the other original name different from this original name?
 
124
            if (newNameMap.get(newName) == null ||
 
125
                MemberInfoObfuscator.hasFixedNewMemberName(memberInfo))
 
126
            {
 
127
                // Remember not to use the new name again in this name space.
 
128
                newNameMap.put(newName, name);
 
129
            }
 
130
        }
 
131
    }
 
132
}