1
/* $Id: MemberInfoNameCollector.java,v 1.3 2005/06/11 13:21:35 eric Exp $
3
* ProGuard -- shrinking, optimization, and obfuscation of Java class files.
5
* Copyright (c) 2002-2005 Eric Lafortune (eric@graphics.cornell.edu)
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)
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
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
21
package proguard.obfuscate;
23
import proguard.classfile.*;
24
import proguard.classfile.visitor.*;
26
import java.io.IOException;
31
* This MemberInfoVisitor collects all new (obfuscation) names of the members
34
* @see MemberInfoLinker
35
* @see MemberInfoObfuscator
37
* @author Eric Lafortune
39
public class MemberInfoNameCollector implements MemberInfoVisitor
41
private boolean allowAggressiveOverloading;
42
private Map descriptorMap;
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.
52
public MemberInfoNameCollector(boolean allowAggressiveOverloading,
55
this.allowAggressiveOverloading = allowAggressiveOverloading;
56
this.descriptorMap = descriptorMap;
60
// Implementations for MemberInfoVisitor.
62
public void visitProgramFieldInfo(ProgramClassFile programClassFile, ProgramFieldInfo programFieldInfo)
64
collectName(programClassFile, programFieldInfo);
68
public void visitProgramMethodInfo(ProgramClassFile programClassFile, ProgramMethodInfo programMethodInfo)
70
collectName(programClassFile, programMethodInfo);
74
public void visitLibraryFieldInfo(LibraryClassFile libraryClassFile, LibraryFieldInfo libraryFieldInfo)
76
collectName(libraryClassFile, libraryFieldInfo);
80
public void visitLibraryMethodInfo(LibraryClassFile libraryClassFile, LibraryMethodInfo libraryMethodInfo)
82
collectName(libraryClassFile, libraryMethodInfo);
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.
91
private void collectName(ClassFile classFile, MemberInfo memberInfo)
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))
102
// Get the member's new name.
103
String newName = MemberInfoObfuscator.newMemberName(memberInfo);
105
// Remember it, if it has already been set.
108
// Get the member's descriptor.
109
String descriptor = memberInfo.getDescriptor(classFile);
111
// Check whether we're allowed to do aggressive overloading
112
if (!allowAggressiveOverloading)
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);
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);
123
// Is the other original name different from this original name?
124
if (newNameMap.get(newName) == null ||
125
MemberInfoObfuscator.hasFixedNewMemberName(memberInfo))
127
// Remember not to use the new name again in this name space.
128
newNameMap.put(newName, name);