001: package com.kirkk.analyzer.framework.bcelbundle;
002:
003: import com.kirkk.analyzer.framework.*;
004: import java.util.*;
005: import java.math.BigDecimal;
006:
007: public class JarImpl implements Jar {
008:
009: public List packages = new ArrayList();
010: private List dependentJars = new ArrayList();
011: private List incomingDependencies = new ArrayList();
012: private List cyclicJars = new ArrayList();
013: private boolean containsCycles = false;
014: private String jarFileName;
015:
016: public JarImpl(String jarFileName) {
017: this .jarFileName = jarFileName;
018: }
019:
020: public List getAllContainedPackages() {
021: return packages;
022: }
023:
024: public void addOutgoingDependency(Jar jar) {
025: if (!this .dependentJars.contains(jar)) {
026: this .dependentJars.add(jar);
027: }
028:
029: //Test for cyclic dependencies.
030: if (jar.getOutgoingDependencies().contains(this )) {
031: //May need to put a conditional here to make sure we only add a cyclic jar if it hasn't already been added.
032: if (!this .cyclicJars.contains(jar)) {
033: jar.addCyclicJar(this );
034: this .addCyclicJar(jar);
035: }
036: }
037:
038: //When adding a dependent jar, setup the relationship so a jar knows of it's incoming dependencies too.
039: if (!jar.getIncomingDependencies().contains(this )) {
040: jar.addIncomingDependency(this );
041: }
042: }
043:
044: public void addIncomingDependency(Jar jar) {
045: this .incomingDependencies.add(jar);
046: }
047:
048: public void addCyclicJar(Jar jar) {
049: if (this .cyclicJars == null) {
050: this .cyclicJars = new ArrayList();
051: }
052: if (!this .cyclicJars.contains(jar)) {
053: this .cyclicJars.add(jar);
054: }
055: this .containsCycles = true;
056: }
057:
058: public boolean hasCycles() {
059: return this .containsCycles;
060: }
061:
062: public List getCyclicJars() {
063: return this .cyclicJars;
064: }
065:
066: public List getOutgoingDependencies() {
067: return this .dependentJars;
068: }
069:
070: public List getIncomingDependencies() {
071: return this .incomingDependencies;
072: }
073:
074: public List getAllExternallyReferencedPackages() {
075: Iterator allPackages = this .getAllContainedPackages()
076: .iterator();
077: ArrayList externalImports = new ArrayList();
078: while (allPackages.hasNext()) {
079: JarPackage javaPackage = (JarPackage) allPackages.next();
080: Iterator dependencies = javaPackage
081: .getImportedPackageNames().iterator();
082: while (dependencies.hasNext()) {
083: String dependentPackage = (String) dependencies.next();
084: if ((this .containsPackage(dependentPackage) == false)
085: && (externalImports.contains(dependentPackage) == false)) {
086: externalImports.add(dependentPackage);
087: }
088:
089: }
090: }
091: return externalImports;
092: }
093:
094: public List getAllUnidentifiableExternallyReferencedPackages() {
095: Iterator externalPackages = this
096: .getAllExternallyReferencedPackages().iterator();
097: ArrayList unresolvablePackages = new ArrayList();
098: while (externalPackages.hasNext()) {
099: String packageName = (String) externalPackages.next();
100: Iterator dependencies = this .dependentJars.iterator();
101: boolean packageFound = false;
102: while (dependencies.hasNext()) {
103: Jar jar = (Jar) dependencies.next();
104: if (jar.containsPackage(packageName)) {
105: packageFound = true;
106: }
107: }
108: if ((!packageFound)
109: && (!unresolvablePackages.contains(packageName))) {
110: unresolvablePackages.add(packageName);
111: }
112: }
113: return unresolvablePackages;
114: }
115:
116: public int getPackageCount() {
117: return this .packages.size();
118: }
119:
120: public String getJarFileName() {
121: //return this.jarFileName.substring(this.jarFileName.lastIndexOf("\\") + 1,
122: // this.jarFileName.length());
123: //return this.jarFileName.substring(this.jarFileName.lastIndexOf("/") + 1,
124: // this.jarFileName.length());
125: return this .jarFileName.substring(this .jarFileName
126: .lastIndexOf(System.getProperty("file.separator")) + 1,
127: this .jarFileName.length());
128: }
129:
130: public String getFullyQualifiedJarFileName() {
131: return this .jarFileName;
132: }
133:
134: public int getClassCount() {
135: Iterator p = this .packages.iterator();
136: int classCount = 0;
137: while (p.hasNext()) {
138: JarPackage pkg = (JarPackage) p.next();
139: classCount += pkg.getClassCount();
140: }
141:
142: return classCount;
143: }
144:
145: public int getAbstractClassCount() {
146: Iterator p = this .packages.iterator();
147: int classCount = 0;
148: while (p.hasNext()) {
149: JarPackage pkg = (JarPackage) p.next();
150: Iterator classes = pkg.getClasses().iterator();
151: while (classes.hasNext()) {
152: JarClass jarClass = (JarClass) classes.next();
153: if (jarClass.isAbstract() || jarClass.isInterface()) {
154: classCount++;
155: }
156: }
157: }
158: return classCount;
159: }
160:
161: public void addPackage(JarPackage jarPackage) {
162: packages.add(jarPackage);
163: }
164:
165: public JarPackage getPackage(String s) {
166: JarPackage jarPackage = new JarPackageImpl(s);
167: int i = packages.indexOf(jarPackage);
168: return (JarPackage) packages.get(i);
169: }
170:
171: public boolean containsPackage(String s) {
172: if (packages == null) {
173: return false;
174: }
175: JarPackage jarPackage = new JarPackageImpl(s);
176: return packages.contains(jarPackage);
177: }
178:
179: public JarMetrics calculateMetrics() {
180: JarMetrics metrics = new JarMetricsImpl(this);
181: return metrics;
182: }
183:
184: }
|