KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > InputReader


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;
22
23 import proguard.classfile.ClassPool;
24 import proguard.classfile.util.WarningPrinter;
25 import proguard.classfile.visitor.*;
26 import proguard.io.*;
27
28 import java.io.IOException JavaDoc;
29
30 /**
31  * This class reads the input class files.
32  *
33  * @author Eric Lafortune
34  */

35 public class InputReader
36 {
37     private Configuration configuration;
38
39
40     /**
41      * Creates a new InputReader to read input class files as specified by the
42      * given configuration.
43      */

44     public InputReader(Configuration configuration)
45     {
46         this.configuration = configuration;
47     }
48
49
50     /**
51      * Fills the given program class pool and library class pool by reading
52      * class files, based on the current configuration.
53      */

54     public void execute(ClassPool programClassPool,
55                         ClassPool libraryClassPool) throws IOException JavaDoc
56     {
57         WarningPrinter warningPrinter = configuration.warn ?
58             new WarningPrinter(System.err) :
59             null;
60
61         WarningPrinter notePrinter = configuration.note ?
62             new WarningPrinter(System.out) :
63             null;
64
65         DuplicateClassPrinter duplicateClassPrinter = configuration.note ?
66             new DuplicateClassPrinter(notePrinter) :
67             null;
68
69         // Check if we have at least some input classes.
70
if (configuration.programJars == null)
71         {
72             throw new IOException JavaDoc("The input is empty. You have to specify one or more '-injars' options");
73         }
74
75         // Read the program class files.
76
// Prepare a data entry reader to filter all classes,
77
// which are then decoded to classes by a class reader,
78
// which are then put in the class pool by a class pool filler.
79
readInput("Reading program ",
80                   configuration.programJars,
81                   new ClassFilter(
82                   new ClassReader(false,
83                                   configuration.skipNonPublicLibraryClasses,
84                                   configuration.skipNonPublicLibraryClassMembers,
85                                   warningPrinter,
86                   new ClassPresenceFilter(programClassPool, duplicateClassPrinter,
87                   new ClassPoolFiller(programClassPool)))));
88
89         // Check if we have at least some input classes.
90
if (programClassPool.size() == 0)
91         {
92             throw new IOException JavaDoc("The input doesn't contain any classes. Did you specify the proper '-injars' options?");
93         }
94
95         // Read the library class files, if any.
96
if (configuration.libraryJars != null)
97         {
98             // Prepare a data entry reader to filter all classes,
99
// which are then decoded to classes by a class reader,
100
// which are then put in the class pool by a class pool filler.
101
readInput("Reading library ",
102                       configuration.libraryJars,
103                       new ClassFilter(
104                       new ClassReader(true,
105                                       configuration.skipNonPublicLibraryClasses,
106                                       configuration.skipNonPublicLibraryClassMembers,
107                                       warningPrinter,
108                       new ClassPresenceFilter(programClassPool, duplicateClassPrinter,
109                       new ClassPresenceFilter(libraryClassPool, duplicateClassPrinter,
110                       new ClassPoolFiller(libraryClassPool))))));
111         }
112
113         // Print out a summary of the notes, if necessary.
114
if (configuration.note)
115         {
116             int noteCount = notePrinter.getWarningCount();
117             if (noteCount > 0)
118             {
119                 System.err.println("Note: there were " + noteCount +
120                                    " duplicate class definitions.");
121             }
122         }
123
124         // Print out a summary of the warnings, if necessary.
125
if (configuration.warn)
126         {
127             int warningCount = warningPrinter.getWarningCount();
128             if (warningCount > 0)
129             {
130                 System.err.println("Warning: there were " + warningCount +
131                                    " classes in incorrectly named files.");
132                 System.err.println(" You should make sure all file names correspond to their class names.");
133                 System.err.println(" The directory hierarchies must correspond to the package hierarchies.");
134
135                 if (!configuration.ignoreWarnings)
136                 {
137                     System.err.println(" If you don't mind the mentioned classes not being written out,");
138                     System.err.println(" you could try your luck using the '-ignorewarnings' option.");
139                     throw new IOException JavaDoc("Please correct the above warnings first.");
140                 }
141             }
142         }
143     }
144
145
146     /**
147      * Reads all input entries from the given class path.
148      */

149     private void readInput(String JavaDoc messagePrefix,
150                            ClassPath classPath,
151                            DataEntryReader reader) throws IOException JavaDoc
152     {
153         readInput(messagePrefix,
154                   classPath,
155                   0,
156                   classPath.size(),
157                   reader);
158     }
159
160
161     /**
162      * Reads all input entries from the given section of the given class path.
163      */

164     public void readInput(String JavaDoc messagePrefix,
165                           ClassPath classPath,
166                           int fromIndex,
167                           int toIndex,
168                           DataEntryReader reader) throws IOException JavaDoc
169     {
170         for (int index = fromIndex; index < toIndex; index++)
171         {
172             ClassPathEntry entry = classPath.get(index);
173             if (!entry.isOutput())
174             {
175                 readInput(messagePrefix, entry, reader);
176             }
177         }
178     }
179
180
181     /**
182      * Reads the given input class path entry.
183      */

184     private void readInput(String JavaDoc messagePrefix,
185                            ClassPathEntry classPathEntry,
186                            DataEntryReader dataEntryReader) throws IOException JavaDoc
187     {
188         try
189         {
190             // Create a reader that can unwrap jars, wars, ears, and zips.
191
DataEntryReader reader =
192                 DataEntryReaderFactory.createDataEntryReader(messagePrefix,
193                                                              classPathEntry,
194                                                              dataEntryReader);
195
196             // Create the data entry pump.
197
DirectoryPump directoryPump =
198                 new DirectoryPump(classPathEntry.getFile());
199
200             // Pump the data entries into the reader.
201
directoryPump.pumpDataEntries(reader);
202         }
203         catch (IOException JavaDoc ex)
204         {
205             throw new IOException JavaDoc("Can't read [" + classPathEntry + "] (" + ex.getMessage() + ")");
206         }
207     }
208 }
209
Popular Tags