KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > smartValueObject > tools > SmartTask


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 JavaDoc;
10 import java.io.FileOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.util.Enumeration JavaDoc;
13 import java.util.Vector JavaDoc;
14
15 /**
16  * Ant task to do bytecode modification on compile time. Relies on
17  * a concrete implementation of <tt>Instrumentor</tt>.
18  * <p><blockquote><pre>
19  * &lt;smartify instrumentor="org.bsf.smartValueObject.tools.JavaAssistInstrumentor"&gt;
20  * &lt;fileset dir="${build.dir}/test"&gt;
21  * &lt;include name="*VO.class"/&gt;
22  * &lt;/fileset&gt;
23  * &lt;/smartify&gt;
24  * </pre></blockquote>
25  */

26 public class SmartTask extends org.apache.tools.ant.taskdefs.MatchingTask {
27     private Vector JavaDoc filesets = new Vector JavaDoc();
28     /** The classname of the instrumentor to use. */
29     private String JavaDoc instrumentor =
30             "org.bsf.smartValueObject.tools.JavaAssistInstrumentor";
31
32     public void addFileset(FileSet f) {
33         filesets.addElement(f);
34     }
35
36     /**
37      * Name of a class implementing
38      * <tt>org.bsf.smartValueObject.tools.Instrumentor</tt>.
39      * @param s implementation to use.
40      * @see org.bsf.smartValueObject.tools.Instrumentor
41      */

42     public void setInstrumentor(String JavaDoc s) {
43         this.instrumentor = s;
44     }
45
46     public String JavaDoc 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 JavaDoc e = filesets.elements();
56         while (e.hasMoreElements()) {
57             FileSet fs = (FileSet) e.nextElement();
58             DirectoryScanner ds = fs.getDirectoryScanner(getProject());
59             String JavaDoc[] files = ds.getIncludedFiles();
60
61             smartify(ds.getBasedir().getAbsolutePath(), files);
62         }
63     }
64
65     /**
66      * Smartify all files. Gets called by <code>execute()</code>.
67      *
68      * @param basedir relative directory
69      * @param files list of files as specified by the fileset-tag.
70      */

71     private void smartify(String JavaDoc basedir, String JavaDoc[] files) {
72         Instrumentor instrumentor = getInstrumentorInstance();
73         log("SmartTask: using " + getInstrumentor() + " to instrument classes");
74         for (int i = 0; i < files.length; i++) {
75             String JavaDoc 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 JavaDoc fos = new FileOutputStream JavaDoc(
90                         new File JavaDoc(basedir + File.separator + file));
91                 fos.write(bytecode);
92                 fos.close();
93             } catch (IOException JavaDoc e) {
94                 throw new BuildException(e.getLocalizedMessage(), e);
95             } catch (InstrumentorException e) {
96                 throw new BuildException(e.getLocalizedMessage(), e);
97             }
98         }
99     }
100
101     /**
102      * Gets an concrete instrumentor instance.
103      */

104     private Instrumentor getInstrumentorInstance() {
105         Instrumentor instrumentor;
106         try {
107             instrumentor = (Instrumentor)
108                     Class.forName(getInstrumentor()).newInstance();
109         } catch (Exception JavaDoc e) {
110             throw new BuildException("Error while instantiating " +
111                     "instrumentor " + getInstrumentor(), e);
112         }
113         return instrumentor;
114     }
115 }
116
Popular Tags