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