1 package org.bsf.smartValueObject.tools; 2 3 import org.apache.tools.ant.BuildException; 4 import org.apache.tools.ant.DirectoryScanner; 5 import org.apache.tools.ant.Project; 6 import org.apache.tools.ant.types.FileSet; 7 import org.bsf.smartValueObject.tools.Instrumentor; 8 9 import java.io.File ; 10 import java.io.FileOutputStream ; 11 import java.io.IOException ; 12 import java.util.Enumeration ; 13 import java.util.Vector ; 14 15 26 public class SmartTask extends org.apache.tools.ant.taskdefs.MatchingTask { 27 private Vector filesets = new Vector (); 28 29 private String instrumentor = 30 "org.bsf.smartValueObject.tools.JavaAssistInstrumentor"; 31 32 public void addFileset(FileSet f) { 33 filesets.addElement(f); 34 } 35 36 42 public void setInstrumentor(String s) { 43 this.instrumentor = s; 44 } 45 46 public String getInstrumentor() { 47 return this.instrumentor; 48 } 49 50 public void execute() throws BuildException { 51 if (filesets.size() == 0) { 52 throw new BuildException("Need a fileset!"); 53 } 54 55 Enumeration e = filesets.elements(); 56 while (e.hasMoreElements()) { 57 FileSet fs = (FileSet) e.nextElement(); 58 DirectoryScanner ds = fs.getDirectoryScanner(getProject()); 59 String [] files = ds.getIncludedFiles(); 60 61 smartify(ds.getBasedir().getAbsolutePath(), files); 62 } 63 } 64 65 71 private void smartify(String basedir, String [] files) { 72 Instrumentor instrumentor = getInstrumentorInstance(); 73 log("SmartTask: using " + getInstrumentor() + " to instrument classes"); 74 for (int i = 0; i < files.length; i++) { 75 String file = files[i]; 76 77 log("SmartTask: smartify " + file, Project.MSG_INFO); 78 79 try { 80 instrumentor.modifyClass(basedir, file); 81 } catch (InstrumentorException e) { 82 throw new BuildException(e.getLocalizedMessage(), e); 83 } 84 85 try { 86 byte[] bytecode = instrumentor.getBytecode(); 87 if (bytecode.length == 0) 88 continue; 89 FileOutputStream fos = new FileOutputStream ( 90 new File (basedir + File.separator + file)); 91 fos.write(bytecode); 92 fos.close(); 93 } catch (IOException e) { 94 throw new BuildException(e.getLocalizedMessage(), e); 95 } catch (InstrumentorException e) { 96 throw new BuildException(e.getLocalizedMessage(), e); 97 } 98 } 99 } 100 101 104 private Instrumentor getInstrumentorInstance() { 105 Instrumentor instrumentor; 106 try { 107 instrumentor = (Instrumentor) 108 Class.forName(getInstrumentor()).newInstance(); 109 } catch (Exception e) { 110 throw new BuildException("Error while instantiating " + 111 "instrumentor " + getInstrumentor(), e); 112 } 113 return instrumentor; 114 } 115 } 116 | Popular Tags |