1 32 33 package com.jeantessier.dependencyfinder.ant; 34 35 import java.io.*; 36 import java.util.*; 37 38 import org.apache.tools.ant.*; 39 import org.apache.tools.ant.types.*; 40 41 import com.jeantessier.classreader.*; 42 import com.jeantessier.dependencyfinder.*; 43 44 public class ClassMetrics extends Task { 45 private boolean list = false; 46 private boolean instructionCounts = false; 47 private File destfile; 48 private Path path; 49 50 public boolean getList() { 51 return list; 52 } 53 54 public void setList(boolean list) { 55 this.list = list; 56 } 57 58 public boolean getInstructioncounts() { 59 return instructionCounts; 60 } 61 62 public void setInstructioncounts(boolean instructionCounts) { 63 this.instructionCounts = instructionCounts; 64 } 65 66 public File getDestfile() { 67 return destfile; 68 } 69 70 public void setDestfile(File destfile) { 71 this.destfile = destfile; 72 } 73 74 public Path createPath() { 75 if (path == null) { 76 path = new Path(getProject()); 77 } 78 79 return path; 80 } 81 82 public Path getPath() { 83 return path; 84 } 85 86 public void execute() throws BuildException { 87 89 if (getPath() == null) { 90 throw new BuildException("path must be set!"); 91 } 92 93 if (getDestfile() == null) { 94 throw new BuildException("destfile must be set!"); 95 } 96 97 log("Reading classes from path " + getPath()); 98 99 VerboseListener verboseListener = new VerboseListener(this); 100 101 ClassfileLoader loader = new AggregatingClassfileLoader(); 102 loader.addLoadListener(verboseListener); 103 loader.load(Arrays.asList(getPath().list())); 104 105 MetricsGatherer metrics = new MetricsGatherer(); 106 metrics.visitClassfiles(loader.getAllClassfiles()); 107 108 log("Saving class metrics to " + getDestfile().getAbsolutePath()); 109 110 try { 111 PrintWriter out = new PrintWriter(new FileWriter(getDestfile())); 112 113 out.println(metrics.getClasses().size() + " class(es)"); 114 if (getList()) { 115 Iterator j = metrics.getClasses().iterator(); 116 while (j.hasNext()) { 117 out.println(" " + j.next()); 118 } 119 } 120 121 out.println(metrics.getInterfaces().size() + " interface(s)"); 122 if (getList()) { 123 Iterator j = metrics.getInterfaces().iterator(); 124 while (j.hasNext()) { 125 out.println(" " + j.next()); 126 } 127 } 128 129 out.println(); 130 out.println(metrics.getMethods().size() + " method(s) (average " + (metrics.getMethods().size() / (metrics.getClasses().size() + (double) metrics.getInterfaces().size())) + " per class/interface)"); 131 out.println(metrics.getFields().size() + " field(s) (average " + (metrics.getFields().size() / (metrics.getClasses().size() + (double) metrics.getInterfaces().size())) + " per class/interface)"); 132 out.println(); 133 134 printCFM(out, " synthetic element(s)", metrics.getSyntheticClasses(), metrics.getSyntheticFields(), metrics.getSyntheticMethods()); 135 printCFM(out, " deprecated element(s)", metrics.getDeprecatedClasses(), metrics.getDeprecatedFields(), metrics.getDeprecatedMethods()); 136 printCFMIC(out, " public element(s)", metrics.getPublicClasses(), metrics.getPublicFields(), metrics.getPublicMethods(), metrics.getPublicInnerClasses()); 137 printFMIC(out, " protected element(s)", metrics.getProtectedFields(), metrics.getProtectedMethods(), metrics.getProtectedInnerClasses()); 138 printFMIC(out, " private element(s)", metrics.getPrivateFields(), metrics.getPrivateMethods(), metrics.getPrivateInnerClasses()); 139 printCFMIC(out, " package element(s)", metrics.getPackageClasses(), metrics.getPackageFields(), metrics.getPackageMethods(), metrics.getPackageInnerClasses()); 140 printCMIC(out, " abstract element(s)", metrics.getAbstractClasses(), metrics.getAbstractMethods(), metrics.getAbstractInnerClasses()); 141 142 printFMIC(out, " static element(s)", metrics.getStaticFields(), metrics.getStaticMethods(), metrics.getStaticInnerClasses()); 143 printCFMIC(out, " final element(s)", metrics.getFinalClasses(), metrics.getFinalFields(), metrics.getFinalMethods(), metrics.getFinalInnerClasses()); 144 145 out.println(metrics.getSynchronizedMethods().size() + " synchronized method(s)"); 146 if (getList()) { 147 Iterator j = metrics.getSynchronizedMethods().iterator(); 148 while (j.hasNext()) { 149 out.println(" " + j.next()); 150 } 151 } 152 153 out.println(metrics.getNativeMethods().size() + " native method(s)"); 154 if (getList()) { 155 Iterator j = metrics.getNativeMethods().iterator(); 156 while (j.hasNext()) { 157 out.println(" " + j.next()); 158 } 159 } 160 161 out.println(metrics.getVolatileFields().size() + " volatile field(s)"); 162 if (getList()) { 163 Iterator j = metrics.getVolatileFields().iterator(); 164 while (j.hasNext()) { 165 out.println(" " + j.next()); 166 } 167 } 168 169 out.println(metrics.getTransientFields().size() + " transient field(s)"); 170 if (getList()) { 171 Iterator j = metrics.getTransientFields().iterator(); 172 while (j.hasNext()) { 173 out.println(" " + j.next()); 174 } 175 } 176 177 out.println(metrics.getCustomAttributes().size() + " custom attribute(s)"); 178 if (getList()) { 179 Iterator j = metrics.getCustomAttributes().iterator(); 180 while (j.hasNext()) { 181 out.println(" " + j.next()); 182 } 183 } 184 185 if (getInstructioncounts()) { 186 out.println(); 187 out.println("Instruction counts:"); 188 for (int opcode=0; opcode<256; opcode++) { 189 out.print(" 0x"); 190 Hex.print(out, (byte) opcode); 191 out.println(" " + Instruction.getMnemonic(opcode) + ": " + metrics.getInstructionCounts()[opcode]); 192 } 193 } 194 195 out.close(); 196 } catch (IOException ex) { 197 throw new BuildException(ex); 198 } 199 } 200 201 private void printCMIC(PrintWriter out, String label, Collection classes, Collection methods, Collection innerClasses) { 202 out.println((classes.size() + 203 methods.size() + 204 innerClasses.size()) + label); 205 if (getList()) { 206 Iterator j; 207 208 out.println(" " + classes.size() + " class(es)"); 209 j = classes.iterator(); 210 while (j.hasNext()) { 211 out.println(" " + j.next()); 212 } 213 214 out.println(" " + methods.size() + " method(s)"); 215 j = methods.iterator(); 216 while (j.hasNext()) { 217 out.println(" " + j.next()); 218 } 219 220 out.println(" " + innerClasses.size() + " inner class(es)"); 221 j = innerClasses.iterator(); 222 while (j.hasNext()) { 223 out.println(" " + j.next()); 224 } 225 } else { 226 out.println(" " + classes.size() + " class(es)"); 227 out.println(" " + methods.size() + " method(s)"); 228 out.println(" " + innerClasses.size() + " inner class(es)"); 229 } 230 } 231 232 private void printCFMIC(PrintWriter out, String label, Collection classes, Collection fields, Collection methods, Collection innerClasses) { 233 out.println((classes.size() + 234 fields.size() + 235 methods.size() + 236 innerClasses.size()) + label); 237 if (getList()) { 238 Iterator j; 239 240 out.println(" " + classes.size() + " class(es)"); 241 j = classes.iterator(); 242 while (j.hasNext()) { 243 out.println(" " + j.next()); 244 } 245 246 out.println(" " + fields.size() + " field(s)"); 247 j = fields.iterator(); 248 while (j.hasNext()) { 249 out.println(" " + j.next()); 250 } 251 252 out.println(" " + methods.size() + " method(s)"); 253 j = methods.iterator(); 254 while (j.hasNext()) { 255 out.println(" " + j.next()); 256 } 257 258 out.println(" " + innerClasses.size() + " inner class(es)"); 259 j = innerClasses.iterator(); 260 while (j.hasNext()) { 261 out.println(" " + j.next()); 262 } 263 } else { 264 out.println(" " + classes.size() + " class(es)"); 265 out.println(" " + fields.size() + " fields(s)"); 266 out.println(" " + methods.size() + " method(s)"); 267 out.println(" " + innerClasses.size() + " inner class(es)"); 268 } 269 } 270 271 private void printCFM(PrintWriter out, String label, Collection classes, Collection fields, Collection methods) { 272 out.println((classes.size() + 273 fields.size() + 274 methods.size()) + label); 275 if (getList()) { 276 Iterator j; 277 278 out.println(" " + classes.size() + " class(es)"); 279 j = classes.iterator(); 280 while (j.hasNext()) { 281 out.println(" " + j.next()); 282 } 283 284 out.println(" " + fields.size() + " field(s)"); 285 j = fields.iterator(); 286 while (j.hasNext()) { 287 out.println(" " + j.next()); 288 } 289 290 out.println(" " + methods.size() + " method(s)"); 291 j = methods.iterator(); 292 while (j.hasNext()) { 293 out.println(" " + j.next()); 294 } 295 } else { 296 out.println(" " + classes.size() + " class(es)"); 297 out.println(" " + fields.size() + " fields(s)"); 298 out.println(" " + methods.size() + " method(s)"); 299 } 300 } 301 302 private void printFMIC(PrintWriter out, String label, Collection fields, Collection methods, Collection innerClasses) { 303 out.println((fields.size() + 304 methods.size() + 305 innerClasses.size()) + label); 306 if (getList()) { 307 Iterator j; 308 309 out.println(" " + fields.size() + " field(s)"); 310 j = fields.iterator(); 311 while (j.hasNext()) { 312 out.println(" " + j.next()); 313 } 314 315 out.println(" " + methods.size() + " method(s)"); 316 j = methods.iterator(); 317 while (j.hasNext()) { 318 out.println(" " + j.next()); 319 } 320 321 out.println(" " + innerClasses.size() + " inner class(es)"); 322 j = innerClasses.iterator(); 323 while (j.hasNext()) { 324 out.println(" " + j.next()); 325 } 326 } else { 327 out.println(" " + fields.size() + " fields(s)"); 328 out.println(" " + methods.size() + " method(s)"); 329 out.println(" " + innerClasses.size() + " inner class(es)"); 330 } 331 } 332 } 333 | Popular Tags |