1 18 package org.objectweb.speedo.generation.enhancer; 19 20 import org.objectweb.speedo.generation.lib.AbstractGeneratorComponent; 21 import org.objectweb.asm.ClassReader; 22 import org.objectweb.asm.ClassWriter; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.InputStream ; 29 import java.util.Map ; 30 import java.util.HashMap ; 31 32 38 public abstract class EnhancerComponent extends AbstractGeneratorComponent { 39 40 44 protected boolean isSrcJar; 45 46 49 private Map cache; 50 51 54 private final static char separator = File.separatorChar; 55 56 59 public EnhancerComponent() { 60 cache = new HashMap (); 61 } 62 63 75 public ClassReader loadJavaClass(final boolean isSrcJar, 76 final String completeName, 77 final String srcFiles, 78 final boolean remove) 79 throws SpeedoEnhancerException { 80 ClassReader jclass = null; 81 jclass = (ClassReader) cache.get(completeName); 83 if (jclass != null) { 84 return jclass; 85 } 86 String file = completeName.replace('.', separator) + ".class"; 88 if (!isSrcJar) { 89 file = srcFiles + separator + file; 90 } 91 try { 92 File f = new File (file); 93 if (f.exists()) { 94 FileInputStream fis = new FileInputStream (f); 95 jclass = new ClassReader(fis); 96 fis.close(); 97 if (remove) { 98 f.delete(); 99 } 100 } else { 101 file = completeName.replace('.', '/') + ".class"; 102 InputStream is = getClass().getClassLoader() 103 .getResourceAsStream(file); 104 if (is == null) { 105 throw new SpeedoEnhancerException( 106 "Error during loading of the class: " + file); 107 } 108 jclass = new ClassReader(is); 109 } 110 } catch (IOException e) { 111 throw new SpeedoEnhancerException("Error during loading " + file, e); 112 } 113 cache.put(completeName, jclass); 114 return jclass; 115 } 116 117 131 public ClassReader loadJavaClass(final boolean isSrcJar, 132 final String completeName, 133 final String srcFiles, 134 final boolean remove, 135 final String storeDir) 136 throws SpeedoEnhancerException { 137 ClassReader jclass = loadJavaClass(isSrcJar, completeName, srcFiles, remove); 138 ClassWriter cw = new ClassWriter(false); 139 jclass.accept(cw, false); 140 writeJavaClass(completeName, cw, storeDir); 141 return jclass; 142 } 143 144 152 public void writeJavaClass(final String name, 153 final ClassWriter jclass, 154 final String srcFiles) 155 throws SpeedoEnhancerException { 156 cache.remove(name); try { 158 int p = name.lastIndexOf('.'); 159 String pkg; 160 String clas; 161 if (p == -1) { 162 pkg = ""; 163 clas = name; 164 } else { 165 pkg = name.substring(0, p).replace('.', separator); 166 clas = name.substring(p + 1); 167 } 168 File outputDir; 169 if (srcFiles == null) { 170 outputDir = new File (pkg); 171 } else { 172 outputDir = new File (srcFiles + separator + pkg); 173 } 174 outputDir.mkdirs(); 175 File outputFile = new File (outputDir, clas + ".class"); 176 outputFile.createNewFile(); 177 FileOutputStream fos = new FileOutputStream (outputFile); 178 fos.write(jclass.toByteArray()); 179 fos.close(); 180 } catch (IOException e) { 181 throw new SpeedoEnhancerException("Cannot write " + name, e); 182 } 183 } 184 } 185 | Popular Tags |