| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.phoenix.report; |
| 34 | | | |
| 35 | | | import java.io.File; |
| 36 | | | import java.io.FileNotFoundException; |
| 37 | | | import java.io.FileOutputStream; |
| 38 | | | import java.io.PrintWriter; |
| 39 | | | import java.util.ArrayList; |
| 40 | | | import java.util.Iterator; |
| 41 | | | import java.util.List; |
| 42 | | | |
| 43 | | | import org.apache.bcel.classfile.Constant; |
| 44 | | | import org.apache.bcel.classfile.ConstantClass; |
| 45 | | | import org.apache.bcel.classfile.ConstantPool; |
| 46 | | | import org.apache.bcel.classfile.JavaClass; |
| 47 | | | import org.apache.bcel.util.ClassLoader; |
| 48 | | | import org.apache.bcel.util.ClassLoaderRepository; |
| 49 | | | |
| 50 | | | |
| 51 | | | @author |
| 52 | | | |
| 53 | | | public final class JDepend |
| 54 | | | { |
| 55 | 0 | (1) | private static final ClassLoaderRepository REPOSITORY |
| 56 | | | = new ClassLoaderRepository(new ClassLoader()); |
| 57 | | | |
| 58 | 0 | | private static final char[] PROPELLER_CHARS |
| 59 | | | = new char[]{'\\', '|', '/', '-'}; |
| 60 | | | |
| 61 | 0 | | private final List mClassDependencies = new ArrayList(); |
| 62 | 0 | | private final List mAllClasses = new ArrayList(); |
| 63 | 0 | | private final List mPackageDependency = new ArrayList(); |
| 64 | | | |
| 65 | 0 | | private String mOutputFilename = "out.dot"; |
| 66 | 0 | | private File mClassDir = new File("build/classes"); |
| 67 | 0 | | private String mIncludePattern = ".*"; |
| 68 | 0 | | private String mExcludePattern = ""; |
| 69 | | | |
| 70 | 0 | | private int mPropellerIndex = 0; |
| 71 | | | |
| 72 | | | private JDepend () |
| 73 | 0 | | { |
| 74 | 0 | | } |
| 75 | | | |
| 76 | | (2) | public static void main (String[] args) |
| 77 | | | throws ClassNotFoundException, FileNotFoundException |
| 78 | | | { |
| 79 | 0 | | final JDepend main = new JDepend(); |
| 80 | 0 | | main.parseArgs(args); |
| 81 | 0 | | main.findClasses(); |
| 82 | 0 | | main.writeDotFile(); |
| 83 | 0 | | } |
| 84 | | | |
| 85 | | | private void parseArgs (String[] args) |
| 86 | | | { |
| 87 | 0 | | for (int i = 0; i < args.length; i++) |
| 88 | | | { |
| 89 | 0 | (3) | if (args[i].equals("-out")) |
| 90 | | | { |
| 91 | 0 | | mOutputFilename = args[i + 1]; |
| 92 | | | } |
| 93 | 0 | (4) | else if (args[i].equals("-classes")) |
| 94 | | | { |
| 95 | 0 | | mClassDir = new File(args[i + 1]); |
| 96 | 0 | | if (! mClassDir.isDirectory()) |
| 97 | | | { |
| 98 | 0 | | throw new IllegalArgumentException("The argument '" + mClassDir |
| 99 | | | + "' for parameter classdir is not a valid directory"); |
| 100 | | | } |
| 101 | | | } |
| 102 | 0 | (5) | else if (args[i].equals("-include")) |
| 103 | | | { |
| 104 | 0 | | mIncludePattern = args[i + 1]; |
| 105 | | | } |
| 106 | 0 | (6) | else if (args[i].equals("-exclude")) |
| 107 | | | { |
| 108 | 0 | | mExcludePattern = args[i + 1]; |
| 109 | | | } |
| 110 | 0 | | else if (args[i].startsWith("-h") |
| 111 | | | || args[i].startsWith("--h") |
| 112 | | | || args[i].indexOf('?') != -1) |
| 113 | | | { |
| 114 | 0 | | help(); |
| 115 | | | } |
| 116 | | | else |
| 117 | | | { |
| 118 | 0 | | throw new IllegalArgumentException( |
| 119 | | | "Unknown parameter '" + args[i] + "'"); |
| 120 | | | } |
| 121 | 0 | | ++i; |
| 122 | | | } |
| 123 | 0 | | } |
| 124 | | | |
| 125 | | | private static void help () |
| 126 | | | { |
| 127 | 0 | | System.err.println(" _ ____ _"); |
| 128 | 0 | | System.err.println(" | | _ \\ ___ _ __ ___ _ __ __| |"); |
| 129 | 0 | | System.err.println(" _ | | | | |/ _ \\ \'_ \\ / _ \\ \'_ \\ / _` |"); |
| 130 | 0 | | System.err.println("| |_| | |_| | __/ |_) | __/ | | | (_| |"); |
| 131 | 0 | | System.err.println(" \\___/|____/ \\___| .__/ \\___|_| |_|\\__,_|"); |
| 132 | 0 | | System.err.println(" |_|"); |
| 133 | 0 | | System.err.println(" )=- The Java Dependency Checker -=("); |
| 134 | 0 | | System.err.println(); |
| 135 | 0 | | System.err.println("Usage:"); |
| 136 | 0 | | System.err.println( |
| 137 | | | " -classes DIR ... path to class directory [build/classes]"); |
| 138 | 0 | | System.err.println( |
| 139 | | | " -out FILE ... path to the output file [out.dot]"); |
| 140 | 0 | | System.err.println( |
| 141 | | | " -include PATTERN ... regex for include packages [.*]"); |
| 142 | 0 | | System.err.println( |
| 143 | | | " -exclude PATTERN ... regex for exclude packages []"); |
| 144 | 0 | | System.err.println(""); |
| 145 | 0 | (7) | System.exit(-1); |
| 146 | 0 | | } |
| 147 | | | |
| 148 | | (8) | public void writeDotFile () |
| 149 | | | throws FileNotFoundException |
| 150 | | | { |
| 151 | 0 | | final PrintWriter out |
| 152 | | | = new PrintWriter(new FileOutputStream(mOutputFilename)); |
| 153 | 0 | | out.println("digraph G {\n" |
| 154 | | | + " center=\"\"\n" |
| 155 | | | + " node[width=.25,hight=.375,fontsize=10,shape=box]"); |
| 156 | 0 | | for (final Iterator iterator = mPackageDependency.iterator(); |
| 157 | 0 | | iterator.hasNext();) |
| 158 | | | { |
| 159 | 0 | | final PackageDependency d = (PackageDependency) iterator.next(); |
| 160 | 0 | | out.println(" " + d.toDotString() |
| 161 | | | + " [fontname=\"verdana\", fontcolor=\"black\", fontsize=10.0];"); |
| 162 | 0 | | } |
| 163 | 0 | | out.println("}"); |
| 164 | 0 | | out.close(); |
| 165 | 0 | | } |
| 166 | | | |
| 167 | | | private void visit (String clazz) |
| 168 | | | throws ClassNotFoundException |
| 169 | | | { |
| 170 | 0 | | final JavaClass javaClass = REPOSITORY.loadClass(clazz); |
| 171 | 0 | | final ConstantPool constantPool = javaClass.getConstantPool(); |
| 172 | 0 | (9) | mAllClasses.add(clazz); |
| 173 | 0 | | propeller(); |
| 174 | 0 | | final Constant[] constant = constantPool.getConstantPool(); |
| 175 | 0 | | for (int i = 0; i < constant.length; i++) |
| 176 | | | { |
| 177 | 0 | | final Constant c = constant[i]; |
| 178 | 0 | | if (c instanceof ConstantClass) |
| 179 | | | { |
| 180 | 0 | | final ConstantClass constantClass = (ConstantClass) c; |
| 181 | | | |
| 182 | 0 | | String newClazz |
| 183 | | | = (String) constantClass.getConstantValue(constantPool); |
| 184 | 0 | | newClazz = newClazz.replace('/', '.'); |
| 185 | | | |
| 186 | 0 | (10) | if (mAllClasses.contains(newClazz)) |
| 187 | | | { |
| 188 | 0 | | continue; |
| 189 | | | } |
| 190 | | | |
| 191 | | | |
| 192 | 0 | | if (!newClazz.matches(mIncludePattern) |
| 193 | | (11) | || newClazz.startsWith("[") |
| 194 | | | || newClazz.matches(mExcludePattern)) |
| 195 | | | { |
| 196 | | | |
| 197 | 0 | | continue; |
| 198 | | | } |
| 199 | | | |
| 200 | 0 | | final JDepend.ClassDependency x |
| 201 | | | = new JDepend.ClassDependency(clazz, newClazz); |
| 202 | 0 | | final JDepend.PackageDependency p |
| 203 | | | = new JDepend.PackageDependency(clazz, newClazz); |
| 204 | 0 | (12) | if (! mPackageDependency.contains(p)) |
| 205 | | | { |
| 206 | 0 | (13) | mPackageDependency.add(p); |
| 207 | | | } |
| 208 | | | |
| 209 | 0 | (14) | if (! mClassDependencies.contains(x)) |
| 210 | | | { |
| 211 | 0 | (15) | mClassDependencies.add(x); |
| 212 | | | |
| 213 | 0 | | visit(newClazz); |
| 214 | | | } |
| 215 | | | } |
| 216 | | | } |
| 217 | | | |
| 218 | 0 | | } |
| 219 | | | |
| 220 | | | |
| 221 | | | |
| 222 | | | |
| 223 | | | private void propeller () |
| 224 | | | { |
| 225 | 0 | (16) | System.out.print("\b"); |
| 226 | 0 | | System.out.print( |
| 227 | | | PROPELLER_CHARS[mPropellerIndex++ % PROPELLER_CHARS.length]); |
| 228 | 0 | | } |
| 229 | | | |
| 230 | | (17) | public void findClasses () |
| 231 | | | { |
| 232 | 0 | | System.out.print("Scanning classpath ... "); |
| 233 | 0 | | findClasses(mClassDir, null); |
| 234 | 0 | | System.out.println(); |
| 235 | 0 | | System.out.println("Scanned " + mAllClasses.size() + " classes."); |
| 236 | 0 | | } |
| 237 | | | |
| 238 | | | private void findClasses (File dir, String pkg) |
| 239 | | | { |
| 240 | 0 | | final File[] files = dir.listFiles(); |
| 241 | 0 | | for (int i = 0; i < files.length; i++) |
| 242 | | | { |
| 243 | 0 | | if (files[i].isDirectory()) |
| 244 | | | { |
| 245 | | | final String newpkg; |
| 246 | 0 | | if (pkg == null) |
| 247 | | | { |
| 248 | 0 | | newpkg = files[i].getName(); |
| 249 | | | } |
| 250 | | | else |
| 251 | | | { |
| 252 | 0 | | newpkg = pkg + "." + files[i].getName(); |
| 253 | | | } |
| 254 | 0 | | findClasses(files[i], newpkg); |
| 255 | 0 | | } |
| 256 | 0 | | else if (files[i].getName().endsWith(".class") |
| 257 | | | && files[i].getName().indexOf('$') == -1) |
| 258 | | | { |
| 259 | 0 | | final String filename = files[i].getName(); |
| 260 | 0 | | final String clazz = pkg + "." |
| 261 | | | + filename.substring(0, |
| 262 | | | filename.length() - ".class".length()); |
| 263 | | | try |
| 264 | | | { |
| 265 | 0 | | visit(clazz); |
| 266 | | | } |
| 267 | 0 | | catch (ClassNotFoundException ignore) |
| 268 | | | { |
| 269 | | | |
| 270 | 0 | | } |
| 271 | | | } |
| 272 | | | } |
| 273 | 0 | | } |
| 274 | | | |
| 275 | | | |
| 276 | | (18) | public static final class ClassDependency |
| 277 | | | { |
| 278 | | | private final String mClazz; |
| 279 | | | private final String mDependsClazz; |
| 280 | | | |
| 281 | | | ClassDependency (String c, String dc) |
| 282 | 0 | | { |
| 283 | 0 | | mClazz = c; |
| 284 | 0 | | mDependsClazz = dc; |
| 285 | 0 | | } |
| 286 | | | |
| 287 | | (19) | public String toString () |
| 288 | | | { |
| 289 | 0 | (20) | return mClazz + " -> " + mDependsClazz; |
| 290 | | | } |
| 291 | | | |
| 292 | | (21) | public String toDotString () |
| 293 | | | { |
| 294 | 0 | | return "\"" + mClazz + "\"" + " -> " + "\"" + mDependsClazz + "\""; |
| 295 | | | } |
| 296 | | | |
| 297 | | | {@inheritDoc} |
| 298 | | | public boolean equals (Object obj) |
| 299 | | | { |
| 300 | 0 | | if (! (obj instanceof JDepend.ClassDependency)) |
| 301 | | | { |
| 302 | 0 | | return false; |
| 303 | | | } |
| 304 | 0 | | final JDepend.ClassDependency o = (JDepend.ClassDependency) obj; |
| 305 | | | |
| 306 | 0 | | return o.mClazz.equals(mClazz) |
| 307 | | | && o.mDependsClazz.equals(mDependsClazz); |
| 308 | | | } |
| 309 | | | |
| 310 | | | {@inheritDoc} |
| 311 | | | public int hashCode () |
| 312 | | | { |
| 313 | 0 | | return mClazz.hashCode() + mDependsClazz.hashCode(); |
| 314 | | | } |
| 315 | | | } |
| 316 | | | |
| 317 | | (22) | public static final class PackageDependency |
| 318 | | | { |
| 319 | | | private final String mPackage; |
| 320 | | | private final String mDependsPackage; |
| 321 | | | |
| 322 | | | PackageDependency (String c, String dc) |
| 323 | 0 | | { |
| 324 | 0 | | mPackage = c.substring(0, c.lastIndexOf('.')); |
| 325 | 0 | | mDependsPackage = dc.substring(0, dc.lastIndexOf('.')); |
| 326 | 0 | | } |
| 327 | | | |
| 328 | | (23) | public String toString () |
| 329 | | | { |
| 330 | 0 | | return mPackage + " -> " + mDependsPackage; |
| 331 | | | } |
| 332 | | | |
| 333 | | (24) | public String toDotString () |
| 334 | | | { |
| 335 | 0 | | return "\"" + mPackage + "\"" + " -> " + "\"" |
| 336 | | | + mDependsPackage + "\""; |
| 337 | | | } |
| 338 | | | |
| 339 | | | {@inheritDoc} |
| 340 | | | public boolean equals (Object obj) |
| 341 | | | { |
| 342 | 0 | | if (! (obj instanceof JDepend.PackageDependency)) |
| 343 | | | { |
| 344 | 0 | | return false; |
| 345 | | | } |
| 346 | 0 | | final JDepend.PackageDependency o = (JDepend.PackageDependency) obj; |
| 347 | | | |
| 348 | 0 | | return o.mPackage.equals(mPackage) |
| 349 | | | && o.mDependsPackage.equals(mDependsPackage); |
| 350 | | | } |
| 351 | | | |
| 352 | | | {@inheritDoc} |
| 353 | | | public int hashCode () |
| 354 | | | { |
| 355 | 0 | | return mPackage.hashCode() + mDependsPackage.hashCode(); |
| 356 | | | } |
| 357 | | | } |
| 358 | | | } |