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.util.*; 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 (ClassUtil.isInitializer(name)) 68 { 69 return; 70 } 71 72 // Get the member's new name. 73 String newName = MemberObfuscator.newMemberName(member); 74 75 // Remember it, if it has already been set. 76 if (newName != null) 77 { 78 // Get the member's descriptor. 79 String descriptor = member.getDescriptor(clazz); 80 81 // Check whether we're allowed to do aggressive overloading 82 if (!allowAggressiveOverloading) 83 { 84 // Trim the return argument from the descriptor if not. 85 // Works for fields and methods alike. 86 descriptor = descriptor.substring(0, descriptor.indexOf(')')+1); 87 } 88 89 // Put the [descriptor - new name] in the map, 90 // creating a new [new name - old name] map if necessary. 91 Map nameMap = MemberObfuscator.retrieveNameMap(descriptorMap, descriptor); 92 93 // Isn't there another original name for this new name, or should 94 // this original name get priority? 95 String otherName = (String)nameMap.get(newName); 96 if (otherName == null || 97 MemberObfuscator.hasFixedNewMemberName(member) || 98 name.compareTo(otherName) < 0) 99 { 100 // Remember not to use the new name again in this name space. 101 nameMap.put(newName, name); 102 } 103 } 104 } 105} 106