Home | History | Annotate | Download | only in io
      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.io;
     22 
     23 import proguard.classfile.*;
     24 import proguard.classfile.io.ProgramClassWriter;
     25 
     26 import java.io.*;
     27 
     28 
     29 /**
     30  * This DataEntryReader reads class entries and writes their corresponding
     31  * versions from the ClassPool to a given DataEntryWriter.
     32  *
     33  * @author Eric Lafortune
     34  */
     35 public class ClassRewriter implements DataEntryReader
     36 {
     37     private final ClassPool       classPool;
     38     private final DataEntryWriter dataEntryWriter;
     39 
     40 
     41     public ClassRewriter(ClassPool       classPool,
     42                          DataEntryWriter dataEntryWriter)
     43     {
     44         this.classPool       = classPool;
     45         this.dataEntryWriter = dataEntryWriter;
     46     }
     47 
     48 
     49     // Implementations for DataEntryReader.
     50 
     51     public void read(DataEntry dataEntry) throws IOException
     52     {
     53         String inputName = dataEntry.getName();
     54         String className = inputName.substring(0, inputName.length() - ClassConstants.CLASS_FILE_EXTENSION.length());
     55 
     56         // Find the modified class corrsponding to the input entry.
     57         ProgramClass programClass = (ProgramClass)classPool.getClass(className);
     58         if (programClass != null)
     59         {
     60             // Rename the data entry if necessary.
     61             String newClassName = programClass.getName();
     62             if (!className.equals(newClassName))
     63             {
     64                 dataEntry = new RenamedDataEntry(dataEntry, newClassName + ClassConstants.CLASS_FILE_EXTENSION);
     65             }
     66 
     67             // Get the output entry corresponding to this input entry.
     68             OutputStream outputStream = dataEntryWriter.getOutputStream(dataEntry);
     69             if (outputStream != null)
     70             {
     71                 // Write the class to the output entry.
     72                 DataOutputStream classOutputStream = new DataOutputStream(outputStream);
     73 
     74                 new ProgramClassWriter(classOutputStream).visitProgramClass(programClass);
     75 
     76                 classOutputStream.flush();
     77             }
     78         }
     79     }
     80 }
     81