KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > kirkk > analyzer > framework > bcel > PackageVisitor


1 package com.kirkk.analyzer.framework.bcel;
2
3 import org.apache.bcel.classfile.*;
4 import java.util.*;
5
6 public class PackageVisitor extends EmptyVisitor {
7
8     private JavaClass javaClass;
9     private ArrayList imports;
10     private ArrayList strings;
11
12     public PackageVisitor(JavaClass javaClass) {
13         this.javaClass = javaClass;
14         this.imports = new ArrayList();
15         this.strings = new ArrayList();
16     }
17     public void visitConstantClass(ConstantClass cls) {
18         //System.out.println("CONSTANTCLASS: " + cls.getBytes(this.pool));
19
String JavaDoc sCls = cls.getBytes(this.javaClass.getConstantPool());
20         if (sCls.indexOf("/") != -1) {
21             sCls = sCls.replace('/', '.');
22             //this.imports.add("CLASS " + sCls);
23
sCls = this.stripClassName(sCls);
24             sCls = this.cleanClass(sCls);
25             if (!this.imports.contains(sCls) && (!this.javaClass.getPackageName().equals(sCls))) {
26                 this.imports.add(sCls);
27             }
28         }
29     }
30
31     public void visitConstantUtf8(ConstantUtf8 utf) {
32         //System.out.println("UTF8: " + utf.getBytes());
33
//System.out.println("UTF8 Strng: " + utf.toString());
34
String JavaDoc utfString = utf.toString().substring(utf.toString().indexOf('"') + 1,utf.toString().lastIndexOf('"'));
35         //System.out.println("FIXED: " + utfString);
36
if (isValidJavaClass(utfString)) {
37             if (!this.strings.contains(utfString)) {
38                 String JavaDoc[] classes = this.separateClasses(utfString);
39                 for (int i = 0; i < classes.length; i++) {
40                     if (classes[i] != null) {
41                         String JavaDoc cls = classes[i].replace('/', '.');
42                         cls = this.stripClassName(cls);
43                         cls = this.cleanClass(cls);
44                         //this.imports.add("UTF " + cls);
45
if (!this.imports.contains(cls) && (!this.javaClass.getPackageName().equals(cls))) {
46                             this.imports.add(cls);
47                         }
48                     }
49                 }
50             }
51         }
52
53     }
54
55     public void visitConstantString(ConstantString str) {
56         //System.out.println("CONSTANTSTRING: " + str.getBytes(this.pool));
57
this.strings.add(str.getBytes(this.javaClass.getConstantPool()).toString());
58     }
59
60     private boolean isValidJavaClass(String JavaDoc cls) {
61         if (cls.indexOf("/") == -1) {
62             return false;
63         }
64         if ( (!cls.startsWith("(")) && (!cls.startsWith("L")) ) {
65             return false;
66         }
67
68         if ( (!cls.endsWith("V")) && (!cls.endsWith(";")) && (!cls.endsWith(")")) ) {
69             return false;
70         }
71
72         return true;
73     }
74
75     private String JavaDoc[] separateClasses(String JavaDoc utfString) {
76         StringTokenizer tokenizer = new StringTokenizer(utfString, ";");
77         String JavaDoc classes[] = new String JavaDoc[tokenizer.countTokens()];
78         int i = 0;
79         while (tokenizer.hasMoreTokens()) {
80             String JavaDoc cls = tokenizer.nextToken();
81             if (cls.indexOf('/') != -1) {
82                 classes[i] = cls;
83                 i++;
84             }
85         }
86         return classes;
87     }
88
89     private String JavaDoc cleanClass(String JavaDoc cls) {
90
91         int index = cls.indexOf('L');
92         String JavaDoc newCls = cls;
93         if (index != -1) {
94             newCls = cls.substring(index + 1);
95         }
96         //System.out.println("CLASS: " + newCls);
97
return newCls;
98         //return cls;
99
}
100
101     private String JavaDoc stripClassName(String JavaDoc cls) {
102         return cls.substring(0, cls.lastIndexOf('.'));
103     }
104     public List getAllImports() {
105         return this.imports;
106     }
107
108     public List getImports(List ignorePackages) {
109         Iterator i = this.imports.iterator();
110         ArrayList imports = new ArrayList();
111         while (i.hasNext()) {
112             String JavaDoc pkg = (String JavaDoc) i.next();
113             if (ignorePackages.isEmpty() == false) {
114                 Iterator filter = ignorePackages.iterator();
115                 boolean filterPackage = false;
116                 while (filter.hasNext()) {
117                     String JavaDoc packageFilter = (String JavaDoc) filter.next();
118                     if (pkg.startsWith(packageFilter)) {
119                         filterPackage = true;
120                     }
121                 }
122                 if (!filterPackage) {
123                     imports.add(pkg);
124                 }
125             } else {
126                 imports.add(pkg);
127             }
128         }
129         return imports;
130
131     }
132 }
Popular Tags