KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > io > DataEntryRenamer


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.io;
22
23 import proguard.classfile.*;
24
25 import java.io.*;
26
27 /**
28  * This DataEntryReader delegates to another DataEntryReader, renaming the
29  * data entries based on the renamed classes in the given ClassPool.
30  *
31  * @author Eric Lafortune
32  */

33 public class DataEntryRenamer implements DataEntryReader
34 {
35     private ClassPool classPool;
36     private DataEntryReader dataEntryReader;
37
38
39     public DataEntryRenamer(ClassPool classPool,
40                             DataEntryReader dataEntryReader)
41     {
42         this.classPool = classPool;
43         this.dataEntryReader = dataEntryReader;
44     }
45
46
47     // Implementations for DataEntryReader.
48

49     public void read(DataEntry dataEntry) throws IOException
50     {
51         String JavaDoc dataEntryName = dataEntry.getName();
52
53         int suffixIndex = dataEntryName.lastIndexOf('.');
54
55         String JavaDoc className = suffixIndex > 0 ?
56             dataEntryName.substring(0, suffixIndex) :
57             dataEntryName;
58
59         // Find the class corrsponding to the data entry.
60
Clazz clazz = classPool.getClass(className);
61         if (clazz != null)
62         {
63             // Rename the data entry if necessary.
64
String JavaDoc newClassName = clazz.getName();
65             if (!className.equals(newClassName))
66             {
67                 String JavaDoc newDataEntryName = suffixIndex > 0 ?
68                     newClassName + dataEntryName.substring(suffixIndex) :
69                     newClassName;
70
71                 dataEntry = new RenamedDataEntry(dataEntry, newDataEntryName);
72             }
73         }
74
75         // Delegate to the actual data entry reader.
76
dataEntryReader.read(dataEntry);
77     }
78 }
79
Popular Tags