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

« back to all changes in this revision

Viewing changes to src/proguard/shrink/ShortestUsagePrinter.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: ShortestUsagePrinter.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.shrink;
 
22
 
 
23
import proguard.classfile.*;
 
24
import proguard.classfile.util.*;
 
25
import proguard.classfile.visitor.*;
 
26
 
 
27
import java.io.*;
 
28
 
 
29
 
 
30
/**
 
31
 * This ClassFileVisitor and MemberInfoVisitor prints out the reasons why
 
32
 * class files and class members have been marked as being used.
 
33
 *
 
34
 * @see UsageMarker
 
35
 *
 
36
 * @author Eric Lafortune
 
37
 */
 
38
public class ShortestUsagePrinter
 
39
  implements ClassFileVisitor,
 
40
             MemberInfoVisitor
 
41
{
 
42
    private ShortestUsageMarker shortestUsageMarker;
 
43
    private boolean             verbose;
 
44
    private PrintStream         ps;
 
45
 
 
46
 
 
47
    /**
 
48
     * Creates a new UsagePrinter that prints verbosely to <code>System.out</code>.
 
49
     * @param shortestUsageMarker the usage marker that was used to mark the
 
50
     *                            classes and class members.
 
51
     */
 
52
    public ShortestUsagePrinter(ShortestUsageMarker shortestUsageMarker)
 
53
    {
 
54
        this(shortestUsageMarker, true);
 
55
    }
 
56
 
 
57
 
 
58
    /**
 
59
     * Creates a new UsagePrinter that prints to the given stream.
 
60
     * @param shortestUsageMarker the usage marker that was used to mark the
 
61
     *                            classes and class members.
 
62
     * @param verbose             specifies whether the output should be verbose.
 
63
     */
 
64
    public ShortestUsagePrinter(ShortestUsageMarker shortestUsageMarker,
 
65
                                boolean             verbose)
 
66
    {
 
67
        this(shortestUsageMarker, verbose, System.out);
 
68
    }
 
69
 
 
70
    /**
 
71
     * Creates a new UsagePrinter that prints to the given stream.
 
72
     * @param shortestUsageMarker the usage marker that was used to mark the
 
73
     *                            classes and class members.
 
74
     * @param verbose             specifies whether the output should be verbose.
 
75
     * @param printStream         the stream to which to print.
 
76
     */
 
77
    public ShortestUsagePrinter(ShortestUsageMarker shortestUsageMarker,
 
78
                                boolean             verbose,
 
79
                                PrintStream         printStream)
 
80
    {
 
81
        this.shortestUsageMarker = shortestUsageMarker;
 
82
        this.verbose             = verbose;
 
83
        this.ps                  = printStream;
 
84
    }
 
85
 
 
86
 
 
87
    // Implementations for ClassFileVisitor.
 
88
 
 
89
    public void visitProgramClassFile(ProgramClassFile programClassFile)
 
90
    {
 
91
        // Print the name of this class file.
 
92
        ps.println(ClassUtil.externalClassName(programClassFile.getName()));
 
93
 
 
94
        // Print the reason for keeping this class file.
 
95
        printReason(programClassFile);
 
96
    }
 
97
 
 
98
 
 
99
    public void visitLibraryClassFile(LibraryClassFile libraryClassFile)
 
100
    {
 
101
        // Print the name of this class file.
 
102
        ps.println(ClassUtil.externalClassName(libraryClassFile.getName()));
 
103
 
 
104
        // Print the reason for keeping this class file.
 
105
        ps.println("  is a library class.\n");
 
106
    }
 
107
 
 
108
 
 
109
    // Implementations for MemberInfoVisitor.
 
110
 
 
111
    public void visitProgramFieldInfo(ProgramClassFile programClassFile, ProgramFieldInfo programFieldInfo)
 
112
    {
 
113
        // Print the name of this field.
 
114
        String name = programFieldInfo.getName(programClassFile);
 
115
        String type = programFieldInfo.getDescriptor(programClassFile);
 
116
 
 
117
        ps.println(ClassUtil.externalClassName(programClassFile.getName()) +
 
118
                   (verbose ?
 
119
                        ": " + ClassUtil.externalFullFieldDescription(0, name, type):
 
120
                        "."  + name) +
 
121
                   lineNumberRange(programClassFile, programFieldInfo));
 
122
 
 
123
        // Print the reason for keeping this method.
 
124
        printReason(programFieldInfo);
 
125
    }
 
126
 
 
127
 
 
128
    public void visitProgramMethodInfo(ProgramClassFile programClassFile, ProgramMethodInfo programMethodInfo)
 
129
    {
 
130
        // Print the name of this method.
 
131
        String name = programMethodInfo.getName(programClassFile);
 
132
        String type = programMethodInfo.getDescriptor(programClassFile);
 
133
 
 
134
        ps.println(ClassUtil.externalClassName(programClassFile.getName()) +
 
135
                   (verbose ?
 
136
                        ": " + ClassUtil.externalFullMethodDescription(programClassFile.getName(), 0, name, type):
 
137
                        "."  + name) +
 
138
                   lineNumberRange(programClassFile, programMethodInfo));
 
139
 
 
140
        // Print the reason for keeping this method.
 
141
        printReason(programMethodInfo);
 
142
    }
 
143
 
 
144
 
 
145
    public void visitLibraryFieldInfo(LibraryClassFile libraryClassFile, LibraryFieldInfo libraryFieldInfo)
 
146
    {
 
147
        // Print the name of this field.
 
148
        String name = libraryFieldInfo.getName(libraryClassFile);
 
149
        String type = libraryFieldInfo.getDescriptor(libraryClassFile);
 
150
 
 
151
        ps.println(ClassUtil.externalClassName(libraryClassFile.getName()) +
 
152
                   (verbose ?
 
153
                        ": " + ClassUtil.externalFullFieldDescription(0, name, type):
 
154
                        "."  + name));
 
155
 
 
156
        // Print the reason for keeping this field.
 
157
        ps.println("  is a library field.\n");
 
158
    }
 
159
 
 
160
 
 
161
    public void visitLibraryMethodInfo(LibraryClassFile libraryClassFile, LibraryMethodInfo libraryMethodInfo)
 
162
    {
 
163
        // Print the name of this method.
 
164
        String name = libraryMethodInfo.getName(libraryClassFile);
 
165
        String type = libraryMethodInfo.getDescriptor(libraryClassFile);
 
166
 
 
167
        ps.println(ClassUtil.externalClassName(libraryClassFile.getName()) +
 
168
                   (verbose ?
 
169
                        ": " + ClassUtil.externalFullMethodDescription(libraryClassFile.getName(), 0, name, type):
 
170
                        "."  + name));
 
171
 
 
172
        // Print the reason for keeping this method.
 
173
        ps.println("  is a library method.\n");
 
174
    }
 
175
 
 
176
 
 
177
    // Small utility methods.
 
178
 
 
179
    private void printReason(VisitorAccepter visitorAccepter)
 
180
    {
 
181
        if (shortestUsageMarker.isUsed(visitorAccepter))
 
182
        {
 
183
            ShortestUsageMark shortestUsageMark = shortestUsageMarker.getShortestUsageMark(visitorAccepter);
 
184
 
 
185
            // Print the reason for keeping this class file.
 
186
            ps.print("  " + shortestUsageMark.getReason());
 
187
 
 
188
            // Print the class or method that is responsible, with its reasons.
 
189
            shortestUsageMark.acceptClassFileVisitor(this);
 
190
            shortestUsageMark.acceptMethodInfoVisitor(this);
 
191
        }
 
192
        else
 
193
        {
 
194
            ps.println("  is not being kept.\n");
 
195
        }
 
196
    }
 
197
 
 
198
 
 
199
    /**
 
200
     * Returns the line number range of the given class member, followed by a
 
201
     * colon, or just an empty String if no range is available.
 
202
     */
 
203
    private static String lineNumberRange(ProgramClassFile programClassFile, ProgramMemberInfo programMemberInfo)
 
204
    {
 
205
        String range = programMemberInfo.getLineNumberRange(programClassFile);
 
206
        return range != null ?
 
207
            (" (" + range + ")") :
 
208
            "";
 
209
    }
 
210
}