KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > ant > AnalysisModuleType


1 /*
2  * @(#)AnalysisModuleType.java
3  *
4  * Copyright (C) 2002-2004 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.codecoverage.v2.ant;
28
29 import java.util.Hashtable JavaDoc;
30
31 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
32 import net.sourceforge.groboutils.codecoverage.v2.module.BranchCountMeasure;
33 import net.sourceforge.groboutils.codecoverage.v2.module.BytecodeCountMeasure;
34 import net.sourceforge.groboutils.codecoverage.v2.module.CallPairMeasure;
35 import net.sourceforge.groboutils.codecoverage.v2.module.FunctionMeasure;
36 import net.sourceforge.groboutils.codecoverage.v2.module.LineCountMeasure;
37
38 import org.apache.tools.ant.AntClassLoader;
39 import org.apache.tools.ant.BuildException;
40 import org.apache.tools.ant.types.DataType;
41 import org.apache.tools.ant.types.EnumeratedAttribute;
42 import org.apache.tools.ant.types.Path;
43 import org.apache.tools.ant.types.Reference;
44
45
46 /**
47  * Loads an analysis module.
48  *
49  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
50  * @version $Date: 2004/04/15 05:48:25 $
51  * @since December 18, 2002
52  */

53 public class AnalysisModuleType extends DataType
54 {
55     private static final Hashtable JavaDoc NAMES_TO_CLASSES = new Hashtable JavaDoc();
56     static {
57         NAMES_TO_CLASSES.put( "line", LineCountMeasure.class );
58         NAMES_TO_CLASSES.put( "linecount", LineCountMeasure.class );
59         
60         NAMES_TO_CLASSES.put( "bytecode", BytecodeCountMeasure.class );
61         
62         NAMES_TO_CLASSES.put( "branch", BranchCountMeasure.class );
63         
64         NAMES_TO_CLASSES.put( "call", CallPairMeasure.class );
65         NAMES_TO_CLASSES.put( "callpair", CallPairMeasure.class );
66         NAMES_TO_CLASSES.put( "call-pair", CallPairMeasure.class );
67         
68         NAMES_TO_CLASSES.put( "function", FunctionMeasure.class );
69         NAMES_TO_CLASSES.put( "method", FunctionMeasure.class );
70     }
71     
72     public static final class TypeAttribute extends EnumeratedAttribute
73     {
74         private String JavaDoc[] types = { "line", "linecount", "bytecode",
75             "branch", "call", "callpair", "call-pair", "function",
76             "method" };
77         public String JavaDoc[] getValues()
78         {
79             return this.types;
80         }
81     }
82     
83     private String JavaDoc moduleName;
84     private String JavaDoc className;
85     private Path classpath;
86     private String JavaDoc resource;
87     private String JavaDoc loaderId = null;
88     private String JavaDoc classpathId = null;
89     
90     
91     public void setName( String JavaDoc n )
92     {
93         this.moduleName = n;
94     }
95     
96     
97     public void setType( TypeAttribute n )
98     {
99         this.moduleName = n.getValue();
100     }
101     
102     
103     public void setClassName( String JavaDoc c )
104     {
105         this.className = c;
106     }
107     
108     
109     /**
110      * Set the classpath to be used when searching for component being defined
111      *
112      * @param classpath an Ant Path object containing the classpath.
113      */

114     public void setClasspath(Path classpath)
115     {
116         if (this.classpath == null)
117         {
118             this.classpath = classpath;
119         }
120         else
121         {
122             this.classpath.append( classpath );
123         }
124     }
125
126     /**
127      * Create the classpath to be used when searching for component being defined
128      */

129     public Path createClasspath()
130     {
131         if (this.classpath == null)
132         {
133             this.classpath = new Path( getProject() );
134         }
135         return this.classpath.createPath();
136     }
137
138     /**
139      * reference to a classpath to use when loading the files.
140      * To actually share the same loader, set loaderref as well
141      */

142     public void setClasspathRef( Reference r )
143     {
144         this.classpathId = r.getRefId();
145         createClasspath().setRefid(r);
146     }
147     
148     
149     /**
150      * Use the reference to locate the loader. If the loader is not
151      * found, taskdef will use the specified classpath and register it
152      * with the specified name.
153      *
154      * This allow multiple taskdef/typedef to use the same class loader,
155      * so they can be used together. It eliminate the need to
156      * put them in the CLASSPATH.
157      *
158      * @since Ant 1.5
159      */

160     public void setLoaderRef( Reference r )
161     {
162         loaderId = r.getRefId();
163     }
164     
165     
166     
167     
168     
169     
170     /**
171      * This method is guaranteed to never return <tt>null</tt>.
172      */

173     public IAnalysisModule getAnalysisModule()
174             throws BuildException
175     {
176         if (isReference())
177         {
178             return getRef().getAnalysisModule();
179         }
180         
181         
182         Class JavaDoc c = null;
183         if (this.moduleName != null)
184         {
185             c = (Class JavaDoc)NAMES_TO_CLASSES.get( this.moduleName.toLowerCase() );
186             if (c == null)
187             {
188                 throw new BuildException( "Unknown analysis module name '"+
189                     this.moduleName+"'." );
190             }
191         }
192         else
193         if (this.className != null)
194         {
195             c = createClass( this.className );
196             if (c == null)
197             {
198                 throw new BuildException( "Could not find class "+
199                     this.className+" from classpath." );
200             }
201         }
202         else
203         {
204             throw new BuildException( "Never set either the 'name' "+
205                 "or the 'classname' attribute." );
206         }
207         
208         try
209         {
210             return (IAnalysisModule)c.newInstance();
211         }
212         catch (Exception JavaDoc ex)
213         {
214             throw new BuildException(
215                 "Error creating analysis module of type "+
216                 c.getName()+".", ex );
217         }
218     }
219     
220     
221     
222     protected AnalysisModuleType getRef()
223     {
224         return (AnalysisModuleType)getCheckedRef(
225             AnalysisModuleType.class, "analysismodule" );
226     }
227     
228     
229     /**
230      * Stolen from o.a.t.a.taskdefs.Definer
231      */

232     private Class JavaDoc createClass( String JavaDoc classname )
233             throws BuildException
234     {
235         // If a loader has been set ( either by loaderRef or magic property )
236
if (loaderId != null)
237         {
238             Object JavaDoc reusedLoader = getProject().getReference( loaderId );
239             if (reusedLoader != null)
240             {
241                 if (reusedLoader instanceof AntClassLoader)
242                 {
243                     try
244                     {
245                         return ((AntClassLoader)reusedLoader).
246                             loadClass( classname );
247                     }
248                     catch (Exception JavaDoc e)
249                     {
250                         throw new BuildException( "Error loading class "+
251                             classname, e );
252                     }
253                 }
254                 // In future the reference object may be the <loader> type
255
// if( reusedLoader instanceof Loader ) {
256
// return ((Loader)reusedLoader).getLoader(project);
257
// }
258
}
259         }
260        
261         AntClassLoader al = null;
262         if (classpath != null)
263         {
264             al = new AntClassLoader( getProject(), classpath, true );
265         }
266         else
267         {
268             al = new AntClassLoader( getProject(), Path.systemClasspath, true );
269         }
270         // need to load Task via system classloader or the new
271
// task we want to define will never be a Task but always
272
// be wrapped into a TaskAdapter.
273
// al.addSystemPackageRoot("org.apache.tools.ant");
274

275
276         // If the loader is new, record it for future uses by other
277
// task/typedefs
278
if (loaderId != null)
279         {
280             if (getProject().getReference(loaderId) == null)
281             {
282                 getProject().addReference( loaderId, al );
283             }
284         }
285         
286         try
287         {
288             return al.loadClass( classname );
289         }
290         catch (Exception JavaDoc e)
291         {
292             throw new BuildException( "Error loading class "+classname, e );
293         }
294     }
295 }
296
297
Popular Tags