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.attribute.*;
25import proguard.classfile.attribute.visitor.AttributeVisitor;
26import proguard.classfile.util.*;
27import proguard.classfile.visitor.*;
28
29import java.io.PrintStream;
30
31
32/**
33 * This ClassVisitor prints out the renamed classes and class members with
34 * their old names and new names.
35 *
36 * @see ClassRenamer
37 *
38 * @author Eric Lafortune
39 */
40public class MappingPrinter
41extends      SimplifiedVisitor
42implements   ClassVisitor,
43             MemberVisitor,
44             AttributeVisitor
45{
46    private final PrintStream ps;
47
48
49    /**
50     * Creates a new MappingPrinter that prints to <code>System.out</code>.
51     */
52    public MappingPrinter()
53    {
54        this(System.out);
55    }
56
57
58    /**
59     * Creates a new MappingPrinter that prints to the given stream.
60     * @param printStream the stream to which to print
61     */
62    public MappingPrinter(PrintStream printStream)
63    {
64        this.ps = printStream;
65    }
66
67
68    // Implementations for ClassVisitor.
69
70    public void visitProgramClass(ProgramClass programClass)
71    {
72        String name    = programClass.getName();
73        String newName = ClassObfuscator.newClassName(programClass);
74
75        ps.println(ClassUtil.externalClassName(name) +
76                   " -> " +
77                   ClassUtil.externalClassName(newName) +
78                   ":");
79
80        // Print out the class members.
81        programClass.fieldsAccept(this);
82        programClass.methodsAccept(this);
83    }
84
85
86    // Implementations for MemberVisitor.
87
88    public void visitProgramField(ProgramClass programClass, ProgramField programField)
89    {
90        String newName = MemberObfuscator.newMemberName(programField);
91        if (newName != null)
92        {
93            ps.println("    " +
94                       ClassUtil.externalFullFieldDescription(
95                           0,
96                           programField.getName(programClass),
97                           programField.getDescriptor(programClass)) +
98                       " -> " +
99                       newName);
100        }
101    }
102
103
104    public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
105    {
106        // Special cases: <clinit> and <init> are always kept unchanged.
107        // We can ignore them here.
108        String name = programMethod.getName(programClass);
109        if (ClassUtil.isInitializer(name))
110        {
111            return;
112        }
113
114        String newName = MemberObfuscator.newMemberName(programMethod);
115        if (newName != null)
116        {
117            ps.print("    ");
118            programMethod.attributesAccept(programClass, this);
119            ps.println(ClassUtil.externalFullMethodDescription(
120                           programClass.getName(),
121                           0,
122                           programMethod.getName(programClass),
123                           programMethod.getDescriptor(programClass)) +
124                       " -> " +
125                       newName);
126        }
127    }
128
129
130    // Implementations for AttributeVisitor.
131
132    public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
133
134
135    public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
136    {
137        codeAttribute.attributesAccept(clazz, method, this);
138    }
139
140
141    public void visitLineNumberTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LineNumberTableAttribute lineNumberTableAttribute)
142    {
143        ps.print(lineNumberTableAttribute.getLowestLineNumber() + ":" +
144                 lineNumberTableAttribute.getHighestLineNumber() + ":");
145    }
146}
147