1 package jdepend.framework; 2 3 import java.io.IOException ; 4 import java.util.*; 5 6 102 103 public class JDepend { 104 105 private HashMap packages; 106 private FileManager fileManager; 107 private PackageFilter filter; 108 private ClassFileParser parser; 109 private JavaClassBuilder builder; 110 private Collection components; 111 112 public JDepend() { 113 this(new PackageFilter()); 114 } 115 116 public JDepend(PackageFilter filter) { 117 118 setFilter(filter); 119 120 this.packages = new HashMap(); 121 this.fileManager = new FileManager(); 122 123 this.parser = new ClassFileParser(filter); 124 this.builder = new JavaClassBuilder(parser, fileManager); 125 126 PropertyConfigurator config = new PropertyConfigurator(); 127 addPackages(config.getConfiguredPackages()); 128 analyzeInnerClasses(config.getAnalyzeInnerClasses()); 129 } 130 131 137 public Collection analyze() { 138 139 Collection classes = builder.build(); 140 141 for (Iterator i = classes.iterator(); i.hasNext();) { 142 analyzeClass((JavaClass)i.next()); 143 } 144 145 return getPackages(); 146 } 147 148 155 public void addDirectory(String name) throws IOException { 156 fileManager.addDirectory(name); 157 } 158 159 164 public void setComponents(String components) { 165 this.components = new ArrayList(); 166 StringTokenizer st = new StringTokenizer(components, ","); 167 while (st.hasMoreTokens()) { 168 String component = st.nextToken(); 169 this.components.add(component); 170 } 171 } 172 173 179 public void analyzeInnerClasses(boolean b) { 180 fileManager.acceptInnerClasses(b); 181 } 182 183 188 public Collection getPackages() { 189 return packages.values(); 190 } 191 192 198 public JavaPackage getPackage(String name) { 199 return (JavaPackage)packages.get(name); 200 } 201 202 207 public int countPackages() { 208 return getPackages().size(); 209 } 210 211 216 public int countClasses() { 217 return builder.countClasses(); 218 } 219 220 225 public boolean containsCycles() { 226 for (Iterator i = getPackages().iterator(); i.hasNext();) { 227 JavaPackage jPackage = (JavaPackage)i.next(); 228 if (jPackage.containsCycle()) { 229 return true; 230 } 231 } 232 233 return false; 234 } 235 236 243 public boolean dependencyMatch(DependencyConstraint constraint) { 244 return constraint.match(getPackages()); 245 } 246 247 252 public void addParseListener(ParserListener listener) { 253 parser.addParseListener(listener); 254 } 255 256 263 public JavaPackage addPackage(String name) { 264 name = toComponent(name); 265 JavaPackage pkg = (JavaPackage)packages.get(name); 266 if (pkg == null) { 267 pkg = new JavaPackage(name); 268 addPackage(pkg); 269 } 270 271 return pkg; 272 } 273 274 private String toComponent(String packageName) { 275 if (components != null) { 276 for (Iterator i = components.iterator(); i.hasNext();) { 277 String component = (String )i.next(); 278 if (packageName.startsWith(component + ".")) { 279 return component; 280 } 281 } 282 } 283 return packageName; 284 } 285 286 292 public void addPackages(Collection packages) { 293 for (Iterator i = packages.iterator(); i.hasNext();) { 294 JavaPackage pkg = (JavaPackage)i.next(); 295 addPackage(pkg); 296 } 297 } 298 299 305 public void addPackage(JavaPackage pkg) { 306 if (!packages.containsValue(pkg)) { 307 packages.put(pkg.getName(), pkg); 308 } 309 } 310 311 public PackageFilter getFilter() { 312 if (filter == null) { 313 filter = new PackageFilter(); 314 } 315 316 return filter; 317 } 318 319 public void setFilter(PackageFilter filter) { 320 if (parser != null) { 321 parser.setFilter(filter); 322 } 323 this.filter = filter; 324 } 325 326 private void analyzeClass(JavaClass clazz) { 327 328 String packageName = clazz.getPackageName(); 329 330 if (!getFilter().accept(packageName)) { 331 return; 332 } 333 334 JavaPackage clazzPackage = addPackage(packageName); 335 clazzPackage.addClass(clazz); 336 337 Collection imports = clazz.getImportedPackages(); 338 for (Iterator i = imports.iterator(); i.hasNext();) { 339 JavaPackage importedPackage = (JavaPackage)i.next(); 340 importedPackage = addPackage(importedPackage.getName()); 341 clazzPackage.dependsUpon(importedPackage); 342 } 343 } 344 } 345 | Popular Tags |