1 26 27 package net.sourceforge.groboutils.codecoverage.v2.ant; 28 29 import java.util.Hashtable ; 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 53 public class AnalysisModuleType extends DataType 54 { 55 private static final Hashtable NAMES_TO_CLASSES = new Hashtable (); 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 [] types = { "line", "linecount", "bytecode", 75 "branch", "call", "callpair", "call-pair", "function", 76 "method" }; 77 public String [] getValues() 78 { 79 return this.types; 80 } 81 } 82 83 private String moduleName; 84 private String className; 85 private Path classpath; 86 private String resource; 87 private String loaderId = null; 88 private String classpathId = null; 89 90 91 public void setName( String 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 c ) 104 { 105 this.className = c; 106 } 107 108 109 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 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 142 public void setClasspathRef( Reference r ) 143 { 144 this.classpathId = r.getRefId(); 145 createClasspath().setRefid(r); 146 } 147 148 149 160 public void setLoaderRef( Reference r ) 161 { 162 loaderId = r.getRefId(); 163 } 164 165 166 167 168 169 170 173 public IAnalysisModule getAnalysisModule() 174 throws BuildException 175 { 176 if (isReference()) 177 { 178 return getRef().getAnalysisModule(); 179 } 180 181 182 Class c = null; 183 if (this.moduleName != null) 184 { 185 c = (Class )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 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 232 private Class createClass( String classname ) 233 throws BuildException 234 { 235 if (loaderId != null) 237 { 238 Object 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 e) 249 { 250 throw new BuildException( "Error loading class "+ 251 classname, e ); 252 } 253 } 254 } 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 275 276 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 e) 291 { 292 throw new BuildException( "Error loading class "+classname, e ); 293 } 294 } 295 } 296 297 | Popular Tags |