KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > kirkk > analyzer > framework > bcelbundle > JarImpl


1 package com.kirkk.analyzer.framework.bcelbundle;
2
3 import com.kirkk.analyzer.framework.*;
4 import java.util.*;
5 import java.math.BigDecimal JavaDoc;
6
7 public class JarImpl implements Jar {
8
9     public List packages = new ArrayList();
10     private List dependentJars = new ArrayList();
11     private List incomingDependencies = new ArrayList();
12     private List cyclicJars = new ArrayList();
13     private boolean containsCycles = false;
14     private String JavaDoc jarFileName;
15
16     public JarImpl(String JavaDoc jarFileName) {
17         this.jarFileName = jarFileName;
18     }
19
20     public List getAllContainedPackages() {
21         return packages;
22     }
23
24     public void addOutgoingDependency(Jar jar) {
25         if (!this.dependentJars.contains(jar)) {
26             this.dependentJars.add(jar);
27         }
28
29         //Test for cyclic dependencies.
30
if (jar.getOutgoingDependencies().contains(this)) {
31             //May need to put a conditional here to make sure we only add a cyclic jar if it hasn't already been added.
32
if (!this.cyclicJars.contains(jar)) {
33                 jar.addCyclicJar(this);
34                 this.addCyclicJar(jar);
35             }
36         }
37
38         //When adding a dependent jar, setup the relationship so a jar knows of it's incoming dependencies too.
39
if (!jar.getIncomingDependencies().contains(this)) {
40             jar.addIncomingDependency(this);
41         }
42     }
43
44     public void addIncomingDependency(Jar jar) {
45         this.incomingDependencies.add(jar);
46     }
47
48     public void addCyclicJar(Jar jar) {
49         if (this.cyclicJars == null) {
50             this.cyclicJars = new ArrayList();
51         }
52         if (!this.cyclicJars.contains(jar)) {
53             this.cyclicJars.add(jar);
54         }
55         this.containsCycles = true;
56     }
57
58     public boolean hasCycles() {
59         return this.containsCycles;
60     }
61
62     public List getCyclicJars() {
63         return this.cyclicJars;
64     }
65
66     public List getOutgoingDependencies() {
67         return this.dependentJars;
68     }
69
70     public List getIncomingDependencies() {
71         return this.incomingDependencies;
72     }
73
74     public List getAllExternallyReferencedPackages() {
75         Iterator allPackages = this.getAllContainedPackages().iterator();
76         ArrayList externalImports = new ArrayList();
77         while (allPackages.hasNext()) {
78             JarPackage javaPackage = (JarPackage) allPackages.next();
79             Iterator dependencies = javaPackage.getImportedPackageNames().iterator();
80             while (dependencies.hasNext()) {
81                 String JavaDoc dependentPackage = (String JavaDoc) dependencies.next();
82                 if ( (this.containsPackage(dependentPackage) == false) &&
83                      (externalImports.contains(dependentPackage) == false) ) {
84                     externalImports.add(dependentPackage);
85                 }
86
87             }
88         }
89         return externalImports;
90     }
91
92     public List getAllUnidentifiableExternallyReferencedPackages() {
93         Iterator externalPackages = this.getAllExternallyReferencedPackages().iterator();
94         ArrayList unresolvablePackages = new ArrayList();
95         while (externalPackages.hasNext()) {
96             String JavaDoc packageName = (String JavaDoc) externalPackages.next();
97             Iterator dependencies = this.dependentJars.iterator();
98             boolean packageFound = false;
99             while (dependencies.hasNext()) {
100                 Jar jar = (Jar) dependencies.next();
101                 if (jar.containsPackage(packageName)) {
102                     packageFound = true;
103                 }
104             }
105             if ( (!packageFound) && (!unresolvablePackages.contains(packageName)) ) {
106                 unresolvablePackages.add(packageName);
107             }
108         }
109         return unresolvablePackages;
110     }
111
112     public int getPackageCount() {
113         return this.packages.size();
114     }
115
116     public String JavaDoc getJarFileName() {
117         return this.jarFileName.substring(this.jarFileName.lastIndexOf("\\") + 1,
118                                                             this.jarFileName.length());
119     }
120
121     public String JavaDoc getFullyQualifiedJarFileName() {
122         return this.jarFileName;
123     }
124
125     public int getClassCount() {
126         Iterator p = this.packages.iterator();
127         int classCount = 0;
128         while (p.hasNext()) {
129             JarPackage pkg = (JarPackage) p.next();
130             classCount += pkg.getClassCount();
131         }
132
133         return classCount;
134     }
135
136     public int getAbstractClassCount() {
137         Iterator p = this.packages.iterator();
138         int classCount = 0;
139         while (p.hasNext()) {
140             JarPackage pkg = (JarPackage) p.next();
141             Iterator classes = pkg.getClasses().iterator();
142             while (classes.hasNext()) {
143                 JarClass jarClass = (JarClass) classes.next();
144                 if (jarClass.isAbstract() || jarClass.isInterface()) {
145                     classCount ++;
146                 }
147             }
148         }
149         return classCount;
150     }
151
152     public void addPackage(JarPackage jarPackage) {
153         packages.add(jarPackage);
154     }
155
156     public JarPackage getPackage(String JavaDoc s) {
157         JarPackage jarPackage = new JarPackageImpl(s);
158         int i = packages.indexOf(jarPackage);
159         return (JarPackage)packages.get(i);
160     }
161
162     public boolean containsPackage(String JavaDoc s) {
163         if(packages == null) {
164             return false;
165         }
166         JarPackage jarPackage = new JarPackageImpl(s);
167         return packages.contains(jarPackage);
168     }
169
170     public JarMetrics calculateMetrics() {
171         JarMetrics metrics = new JarMetricsImpl(this);
172         return metrics;
173     }
174
175 }
176
Popular Tags