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;
22
23import proguard.classfile.ClassPool;
24import proguard.classfile.attribute.visitor.AllAttributeVisitor;
25import proguard.classfile.constant.visitor.AllConstantVisitor;
26import proguard.classfile.instruction.visitor.AllInstructionVisitor;
27import proguard.classfile.util.*;
28import proguard.classfile.visitor.*;
29import proguard.optimize.*;
30import proguard.util.*;
31
32import java.io.*;
33import java.util.*;
34
35/**
36 * This class prints out the seeds specified by keep options.
37 *
38 * @author Eric Lafortune
39 */
40public class SeedPrinter
41{
42    private final PrintStream ps;
43
44
45    /**
46     * Creates a new ConfigurationWriter for the given PrintStream.
47     */
48    public SeedPrinter(PrintStream ps) throws IOException
49    {
50        this.ps = ps;
51    }
52
53
54    /**
55     * Prints out the seeds for the classes in the given program class pool.
56     * @param configuration the configuration containing the keep options.
57     * @throws IOException if an IO error occurs while writing the configuration.
58     */
59    public void write(Configuration configuration,
60                      ClassPool     programClassPool,
61                      ClassPool     libraryClassPool) throws IOException
62    {
63        // Check if we have at least some keep commands.
64        if (configuration.keep == null)
65        {
66            throw new IOException("You have to specify '-keep' options for the shrinking step.");
67        }
68
69        // Clean up any old visitor info.
70        programClassPool.classesAccept(new ClassCleaner());
71        libraryClassPool.classesAccept(new ClassCleaner());
72
73        // Create a visitor for printing out the seeds. We're  printing out
74        // the program elements that are preserved against shrinking,
75        // optimization, or obfuscation.
76        KeepMarker keepMarker = new KeepMarker();
77        ClassPoolVisitor classPoolvisitor =
78            ClassSpecificationVisitorFactory.createClassPoolVisitor(configuration.keep,
79                                                                    keepMarker,
80                                                                    keepMarker,
81                                                                    true,
82                                                                    true,
83                                                                    true);
84        // Mark the seeds.
85        programClassPool.accept(classPoolvisitor);
86        libraryClassPool.accept(classPoolvisitor);
87
88        // Print out the seeds.
89        SimpleClassPrinter printer = new SimpleClassPrinter(false, ps);
90        programClassPool.classesAcceptAlphabetically(new MultiClassVisitor(
91            new ClassVisitor[]
92            {
93                new KeptClassFilter(printer),
94                new AllMemberVisitor(new KeptMemberFilter(printer))
95            }));
96    }
97}