001: /*
002: * ProGuard -- shrinking, optimization, obfuscation, and preverification
003: * of Java bytecode.
004: *
005: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the Free
009: * Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful, but WITHOUT
013: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
015: * more details.
016: *
017: * You should have received a copy of the GNU General Public License along
018: * with this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: package proguard.obfuscate;
022:
023: import proguard.classfile.*;
024: import proguard.classfile.util.*;
025: import proguard.classfile.visitor.*;
026:
027: import java.io.PrintStream;
028:
029: /**
030: * This ClassVisitor prints out the renamed classes and class members with
031: * their old names and new names.
032: *
033: * @see ClassRenamer
034: *
035: * @author Eric Lafortune
036: */
037: public class MappingPrinter extends SimplifiedVisitor implements
038: ClassVisitor, MemberVisitor {
039: private final PrintStream ps;
040:
041: /**
042: * Creates a new MappingPrinter that prints to <code>System.out</code>.
043: */
044: public MappingPrinter() {
045: this (System.out);
046: }
047:
048: /**
049: * Creates a new MappingPrinter that prints to the given stream.
050: * @param printStream the stream to which to print
051: */
052: public MappingPrinter(PrintStream printStream) {
053: this .ps = printStream;
054: }
055:
056: // Implementations for ClassVisitor.
057:
058: public void visitProgramClass(ProgramClass programClass) {
059: String name = programClass.getName();
060: String newName = ClassObfuscator.newClassName(programClass);
061:
062: ps.println(ClassUtil.externalClassName(name) + " -> "
063: + ClassUtil.externalClassName(newName) + ":");
064:
065: // Print out the class members.
066: programClass.fieldsAccept(this );
067: programClass.methodsAccept(this );
068: }
069:
070: public void visitLibraryClass(LibraryClass libraryClass) {
071: }
072:
073: // Implementations for MemberVisitor.
074:
075: public void visitProgramField(ProgramClass programClass,
076: ProgramField programField) {
077: String newName = MemberObfuscator.newMemberName(programField);
078: if (newName != null) {
079: ps.println(" "
080: +
081: //lineNumberRange(programClass, programField) +
082: ClassUtil.externalFullFieldDescription(0,
083: programField.getName(programClass),
084: programField.getDescriptor(programClass))
085: + " -> " + newName);
086: }
087: }
088:
089: public void visitProgramMethod(ProgramClass programClass,
090: ProgramMethod programMethod) {
091: // Special cases: <clinit> and <init> are always kept unchanged.
092: // We can ignore them here.
093: String name = programMethod.getName(programClass);
094: if (name.equals(ClassConstants.INTERNAL_METHOD_NAME_CLINIT)
095: || name
096: .equals(ClassConstants.INTERNAL_METHOD_NAME_INIT)) {
097: return;
098: }
099:
100: String newName = MemberObfuscator.newMemberName(programMethod);
101: if (newName != null) {
102: ps.println(" "
103: + lineNumberRange(programClass, programMethod)
104: + ClassUtil.externalFullMethodDescription(
105: programClass.getName(), 0, programMethod
106: .getName(programClass),
107: programMethod.getDescriptor(programClass))
108: + " -> " + newName);
109: }
110: }
111:
112: // Small utility methods.
113:
114: /**
115: * Returns the line number range of the given class member, followed by a
116: * colon, or just an empty String if no range is available.
117: */
118: private static String lineNumberRange(ProgramClass programClass,
119: ProgramMember programMember) {
120: String range = programMember.getLineNumberRange(programClass);
121: return range != null ? (range + ":") : "";
122: }
123: }
|