1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2013 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.io;
22
23import proguard.classfile.*;
24import proguard.classfile.io.*;
25import proguard.classfile.util.*;
26import proguard.classfile.visitor.ClassVisitor;
27
28import java.io.*;
29
30/**
31 * This DataEntryReader applies a given ClassVisitor to the class
32 * definitions that it reads.
33 * <p>
34 * Class files are read as ProgramClass objects or LibraryClass objects,
35 * depending on the <code>isLibrary</code> flag.
36 * <p>
37 * In case of libraries, only public classes are considered, if the
38 * <code>skipNonPublicLibraryClasses</code> flag is set.
39 *
40 * @author Eric Lafortune
41 */
42public class ClassReader implements DataEntryReader
43{
44    private final boolean        isLibrary;
45    private final boolean        skipNonPublicLibraryClasses;
46    private final boolean        skipNonPublicLibraryClassMembers;
47    private final WarningPrinter warningPrinter;
48    private final ClassVisitor   classVisitor;
49
50
51    /**
52     * Creates a new DataEntryClassFilter for reading the specified
53     * Clazz objects.
54     */
55    public ClassReader(boolean        isLibrary,
56                       boolean        skipNonPublicLibraryClasses,
57                       boolean        skipNonPublicLibraryClassMembers,
58                       WarningPrinter warningPrinter,
59                       ClassVisitor   classVisitor)
60    {
61        this.isLibrary                        = isLibrary;
62        this.skipNonPublicLibraryClasses      = skipNonPublicLibraryClasses;
63        this.skipNonPublicLibraryClassMembers = skipNonPublicLibraryClassMembers;
64        this.warningPrinter                   = warningPrinter;
65        this.classVisitor                     = classVisitor;
66    }
67
68
69    // Implementations for DataEntryReader.
70
71    public void read(DataEntry dataEntry) throws IOException
72    {
73        try
74        {
75            // Get the input stream.
76            InputStream inputStream = dataEntry.getInputStream();
77
78            // Wrap it into a data input stream.
79            DataInputStream dataInputStream = new DataInputStream(inputStream);
80
81            // Create a Clazz representation.
82            Clazz clazz;
83            if (isLibrary)
84            {
85                clazz = new LibraryClass();
86                clazz.accept(new LibraryClassReader(dataInputStream, skipNonPublicLibraryClasses, skipNonPublicLibraryClassMembers));
87            }
88            else
89            {
90                clazz = new ProgramClass();
91                clazz.accept(new ProgramClassReader(dataInputStream));
92            }
93
94            // Apply the visitor, if we have a real class.
95            String className = clazz.getName();
96            if (className != null)
97            {
98                if (!dataEntry.getName().replace(File.pathSeparatorChar, ClassConstants.INTERNAL_PACKAGE_SEPARATOR).equals(className+ClassConstants.CLASS_FILE_EXTENSION) &&
99                    warningPrinter != null)
100                {
101                    warningPrinter.print(className,
102                                         "Warning: class [" + dataEntry.getName() + "] unexpectedly contains class [" + ClassUtil.externalClassName(className) + "]");
103                }
104
105                clazz.accept(classVisitor);
106            }
107
108            dataEntry.closeInputStream();
109        }
110        catch (Exception ex)
111        {
112            throw (IOException)new IOException("Can't process class ["+dataEntry.getName()+"] ("+ex.getMessage()+")").initCause(ex);
113        }
114    }
115}
116