1 26 27 package net.sourceforge.groboutils.codecoverage.v2; 28 29 import java.io.File ; 30 import java.io.IOException ; 31 32 import junit.framework.Assert; 33 import net.sourceforge.groboutils.autodoc.v1.AutoDoc; 34 import net.sourceforge.groboutils.codecoverage.v2.compiler.CompilerCreatorUtil; 35 import net.sourceforge.groboutils.codecoverage.v2.compiler.ModifiedClass; 36 import net.sourceforge.groboutils.codecoverage.v2.compiler.ModifiedMethod; 37 import net.sourceforge.groboutils.codecoverage.v2.compiler.ParseCoverageLogger; 38 import net.sourceforge.groboutils.codecoverage.v2.datastore.AnalysisModuleSet; 39 import net.sourceforge.groboutils.codecoverage.v2.datastore.ClassRecord; 40 import net.sourceforge.groboutils.codecoverage.v2.datastore.DatastoreCreatorUtil; 41 import net.sourceforge.groboutils.codecoverage.v2.datastore.DirMetaDataReader; 42 import net.sourceforge.groboutils.codecoverage.v2.datastore.DirMetaDataWriter; 43 import net.sourceforge.groboutils.codecoverage.v2.datastore.IClassMetaDataWriter; 44 import net.sourceforge.groboutils.codecoverage.v2.datastore.IMetaDataReader; 45 import net.sourceforge.groboutils.codecoverage.v2.datastore.IMetaDataWriter; 46 import net.sourceforge.groboutils.codecoverage.v2.logger.DirectoryChannelLogReader; 47 import net.sourceforge.groboutils.codecoverage.v2.logger.DirectoryChannelLogger; 48 import net.sourceforge.groboutils.codecoverage.v2.module.BranchCountMeasure; 49 import net.sourceforge.groboutils.codecoverage.v2.module.BytecodeCountMeasure; 50 import net.sourceforge.groboutils.codecoverage.v2.module.DefaultAnalysisMetaData; 51 import net.sourceforge.groboutils.codecoverage.v2.module.LineCountMeasure; 52 import net.sourceforge.groboutils.codecoverage.v2.report.AnalysisModuleData; 53 54 import org.apache.bcel.classfile.JavaClass; 55 import org.apache.bcel.classfile.Method; 56 import org.apache.bcel.generic.MethodGen; 57 58 59 66 public class CCCreatorUtil 67 { 68 private static final Class THIS_CLASS = CCCreatorUtil.class; 69 private static final AutoDoc DOC = new AutoDoc( THIS_CLASS ); 70 71 72 73 public static AnalysisModuleSet createAnalysisModuleSet( int count ) 74 { 75 return createAnalysisModuleSet( createAnalysisModules( count ) ); 76 } 77 78 79 public static AnalysisModuleSet createAnalysisModuleSet( 80 IAnalysisModule[] amL ) 81 { 82 Assert.assertNotNull( amL ); 83 AnalysisModuleSet ams = new AnalysisModuleSet( amL ); 84 return ams; 85 } 86 87 88 public static IAnalysisModule[] createAnalysisModules( int count ) 89 { 90 IAnalysisModule amL[] = new IAnalysisModule[ count ]; 91 for (int i = 0; i < count; ++i) 92 { 93 amL[i] = createIAnalysisModule( 0 ); 94 }; 95 return amL; 96 } 97 98 99 private static int moduleIndex = 0; 100 public static IAnalysisModule createIAnalysisModule( int type ) 101 { 102 IAnalysisModule am; 103 switch (type) 104 { 105 case 0: 106 am = createIAnalysisModule( "n"+(moduleIndex++), 107 "u", "text/plain" ); 108 break; 109 case 1: 110 am = createIAnalysisModule( "a", "b", "text/html" ); 111 break; 112 case 2: 113 am = new LineCountMeasure(); 114 break; 115 case 3: 116 am = new BytecodeCountMeasure(); 117 break; 118 case 4: 119 am = new BranchCountMeasure(); 120 break; 121 default: 122 am = null; 123 } 124 return am; 125 } 126 127 128 public static IAnalysisModule createIAnalysisModule( String n, String u, 129 String m ) 130 { 131 IAnalysisModule am = DatastoreCreatorUtil.createAnalysisModule( 132 n, u, m ); 133 return am; 134 } 135 136 137 public static ClassRecord createClassRecord( Class c, 138 ModifiedMethod mm, AnalysisModuleSet ams ) 139 { 140 Assert.assertNotNull( 141 "Null method.", 142 mm ); 143 Assert.assertNotNull( "Null class.", c ); 144 ClassRecord cr = new ClassRecord( c.getName(), 145 0, "Source.java", new String [] { mm.getMethodName() }, ams ); 146 return cr; 147 } 148 149 150 public static ModifiedClass createModifiedClass( Class c ) 151 throws IOException 152 { 153 Assert.assertNotNull( c ); 154 String filename = BytecodeLoaderUtil.getClassFilename( c ); 155 ModifiedClass mc = new ModifiedClass( filename, 156 BytecodeLoaderUtil.loadBytecode( filename ) ); 157 return mc; 158 } 159 160 161 public static ModifiedClass createModifiedClass( ParseCoverageLogger pcl, 162 Class c ) 163 throws IOException 164 { 165 Assert.assertNotNull( c ); 166 String filename = BytecodeLoaderUtil.getClassFilename( c ); 167 ModifiedClass mc = createModifiedClass( pcl, filename, 168 BytecodeLoaderUtil.loadBytecode( filename ) ); 169 return mc; 170 } 171 172 173 public static ModifiedClass createModifiedClass( ParseCoverageLogger pcl, 174 String classFilename, byte[] bytecode ) 175 throws IOException 176 { 177 Assert.assertNotNull( bytecode ); 178 ModifiedClass mc = new ModifiedClass( pcl, classFilename, bytecode ); 179 return mc; 180 } 181 182 183 public static ModifiedMethod getMainModifiedMethod( ModifiedClass mc ) 184 { 185 ModifiedMethod mm[] = mc.getMethods(); 186 for (int i = 0; i < mm.length; ++i) 187 { 188 if ("main([Ljava/lang/String;)V".equals( mm[i].getMethodName() )) 189 { 190 return mm[i]; 191 } 192 } 193 Assert.fail( "Class "+mc.getClassName()+ 194 " does not have a main method." ); 195 throw new IllegalStateException ("Unreachable statement."); 197 } 198 199 200 public static ModifiedMethod createModifiedMethod( JavaClass jc, 201 int methodIndex, Method m, MethodGen mg ) 202 { 203 return CompilerCreatorUtil.createModifiedMethod( jc, methodIndex, 204 m, mg ); 205 } 206 207 208 public static ModifiedMethod createModifiedMethod( Class c, 209 int methodIndex ) 210 throws IOException 211 { 212 JavaClass jc = BCELCreatorUtil.createJavaClass( c ); 213 Method m = BCELCreatorUtil.getMethod( jc, methodIndex ); 214 MethodGen mg = BCELCreatorUtil.createMethodGen( jc, methodIndex ); 215 ModifiedMethod mm = createModifiedMethod( jc, methodIndex, m, mg ); 216 return mm; 217 } 218 219 220 public static IMethodCode createIMethodCode( Class c, int methodIndex, 221 AnalysisModuleSet ams, int measureIndex ) 222 throws IOException 223 { 224 ModifiedMethod mm = createModifiedMethod( c, methodIndex ); 225 return createIMethodCode( c, mm, ams, measureIndex ); 226 } 227 228 229 public static IMethodCode createIMethodCode( Class c, ModifiedMethod mm, 230 AnalysisModuleSet ams, int measureIndex ) 231 { 232 IMethodCode mc = CompilerCreatorUtil.createIMethodCode( 233 (short)measureIndex, mm, createClassRecord( c, mm, ams ) ); 234 return mc; 235 } 236 237 238 239 public static IAnalysisMetaData createIAnalysisMetaData( String c, 240 String nc, byte w ) 241 { 242 DefaultAnalysisMetaData damd = new DefaultAnalysisMetaData( c, nc, w ); 243 return damd; 244 } 245 246 247 public static AnalysisModuleData createAnalysisModuleData( 248 IAnalysisModule module, IMetaDataReader mdr, 249 IChannelLogReader clr ) 250 throws IOException 251 { 252 AnalysisModuleData amd = new AnalysisModuleData( module, mdr, clr ); 253 return amd; 254 } 255 256 257 public synchronized static File createNewDirectory() 258 { 259 File f = null; 260 while (f == null || f.exists()) 261 { 262 f = new File ( ".", Long.toString( System.currentTimeMillis() ) ); 263 } 264 f.mkdirs(); 265 Assert.assertTrue( "Did not generate a directory.", 266 f.isDirectory() ); 267 return f; 268 } 269 270 271 public static class SimpleClassLogData { 272 public String classSig; 273 public int methods[]; 274 public int marks[]; 275 276 public SimpleClassLogData( String cs, int me[], int ma[] ) 277 { 278 this.classSig = cs; 279 this.methods = me; 280 this.marks = ma; 281 validate(); 282 } 283 284 285 public void validate() 286 { 287 Assert.assertNotNull( this.classSig ); 288 Assert.assertNotNull( this.methods ); 289 Assert.assertNotNull( this.marks ); 290 Assert.assertEquals( this.methods.length, this.marks.length ); 291 } 292 } 293 294 295 public static void populateLogger( IChannelLogger cl, 296 SimpleClassLogData inputData[] ) 297 { 298 Assert.assertNotNull( cl ); 299 Assert.assertNotNull( inputData ); 300 for (int i = 0; i < inputData.length; ++i) 301 { 302 inputData[i].validate(); 303 DOC.getLog().info( "Logging class "+inputData[i].classSig+"." ); 304 for (int j = 0; j < inputData[i].methods.length; ++j) 305 { 306 DOC.getLog().debug( "-- mark "+inputData[i].methods[j]+"-"+ 307 inputData[i].marks[j]+"." ); 308 cl.cover( inputData[i].classSig, 309 (short)inputData[i].methods[j], 310 (short)inputData[i].marks[j] ); 311 } 312 } 313 } 314 315 316 321 public static DirectoryChannelLogReader createDirectoryChannelLogReader( 322 File basedir, SimpleClassLogData inputData[], int channel ) 323 { 324 short sc = (short)channel; 325 File channelDir = new File ( basedir, Short.toString( sc ) ); 326 DirectoryChannelLogger dcl = new DirectoryChannelLogger( channelDir ); 327 DOC.getLog().info( "Putting log data into dir '"+channelDir+"'." ); 328 populateLogger( dcl, inputData ); 329 DirectoryChannelLogReader dclr = new DirectoryChannelLogReader( 330 basedir, sc ); 331 return dclr; 332 } 333 334 335 public static void populateMetaData( IMetaDataWriter mdw, Class set[], 336 IAnalysisModule modules[] ) 337 throws IOException 338 { 339 Assert.assertNotNull( mdw ); 340 Assert.assertNotNull( set ); 341 Assert.assertNotNull( modules ); 342 AnalysisModuleSet ams = createAnalysisModuleSet( modules ); 343 for (int i = 0; i < modules.length; ++i) 344 { 345 IClassMetaDataWriter cmdw = mdw.getClassWriter( modules[i] ); 346 try 347 { 348 for (int j = 0; j < set.length; ++j) 349 { 350 ModifiedClass mc = createModifiedClass( set[j] ); 351 cmdw.writeClassRecord( mc.createClassRecord( ams ) ); 352 } 353 } 354 finally 355 { 356 cmdw.close(); 357 } 358 } 359 } 360 361 362 public static DirMetaDataReader createDirMetaDataReader( File basedir, 363 Class populateSet[], IAnalysisModule modules[] ) 364 throws IOException 365 { 366 IMetaDataWriter mdw = new DirMetaDataWriter( basedir ); 367 try 368 { 369 populateMetaData( mdw, populateSet, modules ); 370 } 371 finally 372 { 373 mdw.close(); 374 } 375 376 DirMetaDataReader dmdr = new DirMetaDataReader( basedir ); 377 return dmdr; 378 } 379 } 380 381 | Popular Tags |