1 30 package org.objectweb.asm.optimizer; 31 32 import java.io.File ; 33 import java.io.FileInputStream ; 34 import java.io.FileOutputStream ; 35 import java.io.IOException ; 36 import java.io.OutputStream ; 37 import java.util.Comparator ; 38 import java.util.Iterator ; 39 import java.util.Set ; 40 import java.util.TreeSet ; 41 42 import org.objectweb.asm.ClassReader; 43 import org.objectweb.asm.ClassWriter; 44 45 50 public class Shrinker { 51 52 public static void main(final String [] args) throws IOException { 53 NameMapping mapping = new NameMapping(args[0]); 54 File f = new File (args[1]); 55 File d = new File (args[2]); 56 optimize(f, d, mapping); 57 } 58 59 static void optimize(final File f, final File d, final NameMapping mapping) 60 throws IOException 61 { 62 if (f.isDirectory()) { 63 File [] 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 (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 constants = new TreeSet (new ConstantComparator()); 76 constants.addAll(cp.values()); 77 78 cr = new ClassReader(cw.toByteArray()); 79 cw = new ClassWriter(false); 80 Iterator 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 n = mapping.map(co.getClassName()); 88 File g = new File (d, n + ".class"); 89 if (!g.exists() || g.lastModified() < f.lastModified()) { 90 g.getParentFile().mkdirs(); 91 OutputStream os = new FileOutputStream (g); 92 os.write(cw.toByteArray()); 93 os.close(); 94 } 95 } 96 } 97 98 static class ConstantComparator implements Comparator { 99 100 public int compare(final Object o1, final Object 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 (c1.intVal).compareTo(new Integer (c2.intVal)); 108 case 'J': 109 return new Long (c1.longVal).compareTo(new Long (c2.longVal)); 110 case 'F': 111 return new Float (c1.floatVal).compareTo(new Float (c2.floatVal)); 112 case 'D': 113 return new Double (c1.doubleVal).compareTo(new Double (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 |