1 21 package com.db4o.nativequery.main; 22 23 import java.io.*; 24 import java.net.*; 25 26 import EDU.purdue.cs.bloat.editor.*; 27 import EDU.purdue.cs.bloat.file.*; 28 29 import com.db4o.nativequery.bloat.*; 30 import com.db4o.nativequery.optimization.*; 31 import com.db4o.query.*; 32 33 public class Db4oFileEnhancer { 34 private NativeQueryEnhancer enhancer=new NativeQueryEnhancer(); 35 36 public void enhance(String sourceDir,String targetDir,String [] classPath,String packagePredicate) throws Exception { 37 File source = new File(sourceDir); 38 File target = new File(targetDir); 39 40 ClassFileLoader fileLoader=new ClassFileLoader(); 41 fileLoader.setClassPath(sourceDir); 42 URL[] urls=new URL[classPath.length+1]; 43 urls[0]=source.toURL(); 44 for (int pathIdx = 0; pathIdx < classPath.length; pathIdx++) { 45 fileLoader.appendClassPath(classPath[pathIdx]); 46 urls[pathIdx+1]=new File(classPath[pathIdx]).toURL(); 47 } 48 URLClassLoader classLoader=new URLClassLoader(urls,ClassLoader.getSystemClassLoader()); 49 fileLoader.setOutputDir(target); 50 if(!source.isDirectory()) { 51 throw new IOException("No directory: "+sourceDir); 52 } 53 enhance(source.getCanonicalPath(),source,target,classLoader,new BloatUtil(fileLoader),packagePredicate); 54 fileLoader.done(); 55 } 56 57 private void enhance(String prefix,File source,File target,ClassLoader classLoader,BloatUtil bloatUtil,String packagePredicate) throws Exception { 58 if(source.isDirectory()) { 59 File[] subFiles=source.listFiles(new FileFilter() { 60 public boolean accept(File file) { 61 return file.isDirectory()||file.getName().endsWith(".class"); 62 } 63 }); 64 target.mkdirs(); 65 for (int idx = 0; idx < subFiles.length; idx++) { 66 enhance(prefix,subFiles[idx],new File(target,subFiles[idx].getName()),classLoader,bloatUtil,packagePredicate); 67 } 68 } 69 else { 70 String className = source.getCanonicalPath().substring(prefix.length()+1); 71 className = className.substring(0, className.length()-".class".length()); 72 className=className.replace(File.separatorChar,'.'); 73 if(!className.startsWith(packagePredicate)) { 74 copyFile(source,target); 75 return; 76 } 77 try { 78 Class clazz = classLoader.loadClass(className); 79 Class filterClass = classLoader.loadClass(Predicate.class.getName()); 80 if(filterClass.isAssignableFrom(clazz)&&filterClass!=clazz) { 81 System.err.println("Processing "+className); 82 ClassEditor classEditor=bloatUtil.classEditor(className); 83 enhancer.enhance(bloatUtil,classEditor,Predicate.PREDICATEMETHOD_NAME,classLoader); 84 } 85 else { 86 copyFile(source,target); 87 } 88 } catch (Exception e) { 89 e.printStackTrace(); 90 } catch (NoClassDefFoundError e) { 91 System.err.println("Omitting "+className+": Referenced class "+e.getMessage()+" not found."); 92 copyFile(source,target); 93 } 94 } 95 } 96 97 private void copyFile(File source, File target) throws IOException { 98 BufferedInputStream in=new BufferedInputStream(new FileInputStream(source)); 99 BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(target)); 100 try { 101 byte[] buffer=new byte[4096]; 102 int bytesRead=0; 103 while((bytesRead=in.read(buffer))>=0) { 104 out.write(buffer,0,bytesRead); 105 } 106 out.flush(); 107 } finally { 108 try { 109 in.close(); 110 } catch (IOException e) { 111 } 112 try { 113 out.close(); 114 } catch (IOException e) { 115 } 116 } 117 } 118 119 public static void main(String [] args) throws Exception { 120 new Db4oFileEnhancer().enhance(args[0],args[1],new String []{},""); 121 } 122 } 123 | Popular Tags |