KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > optimizer > Shrinker


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000-2005 INRIA, France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package org.objectweb.asm.optimizer;
31
32 import java.io.File JavaDoc;
33 import java.io.FileInputStream JavaDoc;
34 import java.io.FileOutputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.OutputStream JavaDoc;
37 import java.util.Comparator JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.Set JavaDoc;
40 import java.util.TreeSet JavaDoc;
41
42 import org.objectweb.asm.ClassReader;
43 import org.objectweb.asm.ClassWriter;
44
45 /**
46  * A class file shrinker utility.
47  *
48  * @author Eric Bruneton
49  */

50 public class Shrinker {
51
52     public static void main(final String JavaDoc[] args) throws IOException JavaDoc {
53         NameMapping mapping = new NameMapping(args[0]);
54         File JavaDoc f = new File JavaDoc(args[1]);
55         File JavaDoc d = new File JavaDoc(args[2]);
56         optimize(f, d, mapping);
57     }
58
59     static void optimize(final File JavaDoc f, final File JavaDoc d, final NameMapping mapping)
60             throws IOException JavaDoc
61     {
62         if (f.isDirectory()) {
63             File JavaDoc[] files = f.listFiles();
64             for (int i = 0; i < files.length; ++i) {
65                 optimize(files[i], d, mapping);
66             }
67         } else if (f.getName().endsWith(".class")) {
68             ConstantPool cp = new ConstantPool();
69             ClassReader cr = new ClassReader(new FileInputStream JavaDoc(f));
70             ClassWriter cw = new ClassWriter(false);
71             ClassConstantsCollector ccc = new ClassConstantsCollector(cw, cp);
72             ClassOptimizer co = new ClassOptimizer(ccc, mapping);
73             cr.accept(co, true);
74
75             Set JavaDoc constants = new TreeSet JavaDoc(new ConstantComparator());
76             constants.addAll(cp.values());
77
78             cr = new ClassReader(cw.toByteArray());
79             cw = new ClassWriter(false);
80             Iterator JavaDoc i = constants.iterator();
81             while (i.hasNext()) {
82                 Constant c = (Constant) i.next();
83                 c.write(cw);
84             }
85             cr.accept(cw, true);
86
87             String JavaDoc n = mapping.map(co.getClassName());
88             File JavaDoc g = new File JavaDoc(d, n + ".class");
89             if (!g.exists() || g.lastModified() < f.lastModified()) {
90                 g.getParentFile().mkdirs();
91                 OutputStream JavaDoc os = new FileOutputStream JavaDoc(g);
92                 os.write(cw.toByteArray());
93                 os.close();
94             }
95         }
96     }
97
98     static class ConstantComparator implements Comparator JavaDoc {
99
100         public int compare(final Object JavaDoc o1, final Object JavaDoc o2) {
101             Constant c1 = (Constant) o1;
102             Constant c2 = (Constant) o2;
103             int d = getSort(c1) - getSort(c2);
104             if (d == 0) {
105                 switch (c1.type) {
106                     case 'I':
107                         return new Integer JavaDoc(c1.intVal).compareTo(new Integer JavaDoc(c2.intVal));
108                     case 'J':
109                         return new Long JavaDoc(c1.longVal).compareTo(new Long JavaDoc(c2.longVal));
110                     case 'F':
111                         return new Float JavaDoc(c1.floatVal).compareTo(new Float JavaDoc(c2.floatVal));
112                     case 'D':
113                         return new Double JavaDoc(c1.doubleVal).compareTo(new Double JavaDoc(c2.doubleVal));
114                     case 's':
115                     case 'S':
116                     case 'C':
117                         return c1.strVal1.compareTo(c2.strVal1);
118                     case 'T':
119                         d = c1.strVal1.compareTo(c2.strVal1);
120                         if (d == 0) {
121                             d = c1.strVal2.compareTo(c2.strVal2);
122                         }
123                         break;
124                     default:
125                         d = c1.strVal1.compareTo(c2.strVal1);
126                         if (d == 0) {
127                             d = c1.strVal2.compareTo(c2.strVal2);
128                             if (d == 0) {
129                                 d = c1.strVal3.compareTo(c2.strVal3);
130                             }
131                         }
132                 }
133             }
134             return d;
135         }
136
137         private int getSort(Constant c) {
138             switch (c.type) {
139                 case 'I':
140                     return 0;
141                 case 'J':
142                     return 1;
143                 case 'F':
144                     return 2;
145                 case 'D':
146                     return 3;
147                 case 's':
148                     return 4;
149                 case 'S':
150                     return 5;
151                 case 'C':
152                     return 6;
153                 case 'T':
154                     return 7;
155                 case 'G':
156                     return 8;
157                 case 'M':
158                     return 9;
159                 default:
160                     return 10;
161             }
162         }
163     }
164 }
165
Popular Tags