KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > shrink > UsagePrinter


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 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 ClassVisitor prints out the classes and class members that have been
32  * marked as being used (or not used).
33  *
34  * @see UsageMarker
35  *
36  * @author Eric Lafortune
37  */

38 public class UsagePrinter
39 extends SimplifiedVisitor
40 implements ClassVisitor,
41              MemberVisitor
42 {
43     private UsageMarker usageMarker;
44     private boolean printUnusedItems;
45     private PrintStream ps;
46
47     // A field to remember the class name, if a header is needed for class members.
48
private String JavaDoc className;
49
50
51     /**
52      * Creates a new UsagePrinter that prints to <code>System.out</code>.
53      * @param usageMarker the usage marker that was used to mark the classes
54      * and class members.
55      * @param printUsedItems a flag that indicates whether only unused items
56      * should be printed, or alternatively, only used items.
57      */

58     public UsagePrinter(UsageMarker usageMarker,
59                         boolean printUnusedItems)
60     {
61         this(usageMarker, printUnusedItems, System.out);
62     }
63
64
65     /**
66      * Creates a new UsagePrinter that prints to the given stream.
67      * @param usageMarker the usage marker that was used to mark the classes
68      * and class members.
69      * @param printUsedItems a flag that indicates whether only unused items
70      * should be printed, or alternatively, only used items.
71      * @param printStream the stream to which to print
72      */

73     public UsagePrinter(UsageMarker usageMarker,
74                         boolean printUnusedItems,
75                         PrintStream printStream)
76     {
77         this.usageMarker = usageMarker;
78         this.printUnusedItems = printUnusedItems;
79         this.ps = printStream;
80     }
81
82
83     // Implementations for ClassVisitor.
84

85     public void visitProgramClass(ProgramClass programClass)
86     {
87         if (usageMarker.isUsed(programClass))
88         {
89             if (printUnusedItems)
90             {
91                 className = programClass.getName();
92
93                 programClass.fieldsAccept(this);
94                 programClass.methodsAccept(this);
95
96                 className = null;
97             }
98             else
99             {
100                 ps.println(ClassUtil.externalClassName(programClass.getName()));
101             }
102         }
103         else
104         {
105             if (printUnusedItems)
106             {
107                 ps.println(ClassUtil.externalClassName(programClass.getName()));
108             }
109         }
110     }
111
112
113     // Implementations for MemberVisitor.
114

115     public void visitProgramField(ProgramClass programClass, ProgramField programField)
116     {
117         if (usageMarker.isUsed(programField) ^ printUnusedItems)
118         {
119             printClassNameHeader();
120
121             ps.println(" " +
122                        lineNumberRange(programClass, programField) +
123                        ClassUtil.externalFullFieldDescription(
124                            programField.getAccessFlags(),
125                            programField.getName(programClass),
126                            programField.getDescriptor(programClass)));
127         }
128     }
129
130
131     public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
132     {
133         if (usageMarker.isUsed(programMethod) ^ printUnusedItems)
134         {
135             printClassNameHeader();
136
137             ps.println(" " +
138                        lineNumberRange(programClass, programMethod) +
139                        ClassUtil.externalFullMethodDescription(
140                            programClass.getName(),
141                            programMethod.getAccessFlags(),
142                            programMethod.getName(programClass),
143                            programMethod.getDescriptor(programClass)));
144         }
145     }
146
147
148     // Small utility methods.
149

150     /**
151      * Prints the class name field. The field is then cleared, so it is not
152      * printed again.
153      */

154     private void printClassNameHeader()
155     {
156         if (className != null)
157         {
158             ps.println(ClassUtil.externalClassName(className) + ":");
159             className = null;
160         }
161     }
162
163
164     /**
165      * Returns the line number range of the given class member, followed by a
166      * colon, or just an empty String if no range is available.
167      */

168     private static String JavaDoc lineNumberRange(ProgramClass programClass, ProgramMember programMember)
169     {
170         String JavaDoc range = programMember.getLineNumberRange(programClass);
171         return range != null ?
172             (range + ":") :
173             "";
174     }
175 }
176
Popular Tags