001: package com.kirkk.analyzer.framework.bcel;
002:
003: import org.apache.bcel.classfile.*;
004: import java.util.*;
005:
006: public class PackageVisitor extends EmptyVisitor {
007:
008: private JavaClass javaClass;
009: private ArrayList imports;
010: private ArrayList strings;
011:
012: public PackageVisitor(JavaClass javaClass) {
013: this .javaClass = javaClass;
014: //System.out.println("PackageVisitor constructor: " + javaClass.getClassName());
015: this .imports = new ArrayList();
016: this .strings = new ArrayList();
017: }
018:
019: public void visitConstantClass(ConstantClass cls) {
020: //System.out.println("CONSTANTCLASS: " + cls.getBytes(this.pool));
021: String sCls = cls.getBytes(this .javaClass.getConstantPool());
022: if (sCls.indexOf("/") != -1) {
023: //sCls = sCls.substring(0, sCls.lastIndexOf("."));
024: sCls = this .stripClassName(sCls);
025: sCls = sCls.replace('/', '.');
026: sCls = this .cleanClass(sCls);
027: if (!this .imports.contains(sCls)
028: && (!this .javaClass.getPackageName().equals(sCls))) {
029: this .imports.add(sCls);
030: }
031: }
032: }
033:
034: public void visitConstantUtf8(ConstantUtf8 utf) {
035: //System.out.println("UTF8: " + utf.getBytes());
036: //System.out.println("UTF8 Strng: " + utf.toString());
037: String utfString = utf.toString().substring(
038: utf.toString().indexOf('"') + 1,
039: utf.toString().lastIndexOf('"'));
040: //System.out.println("FIXED: " + utfString);
041: if (isValidJavaClass(utfString)) {
042: if (!this .strings.contains(utfString)) {
043: String[] classes = this .separateClasses(utfString);
044: for (int i = 0; i < classes.length; i++) {
045: if (classes[i] != null) {
046: String cls = classes[i];
047: String packageName = this .stripClassName(cls);
048: packageName = packageName.replace('/', '.');
049: String cleanedPackage = this
050: .cleanClass(packageName);
051: //System.out.println("UTF " + cls);
052: if (!this .imports.contains(cleanedPackage)
053: && (!this .javaClass.getPackageName()
054: .equals(cleanedPackage))) {
055: this .imports.add(cleanedPackage);
056: }
057: }
058: }
059: }
060: }
061:
062: }
063:
064: public void visitConstantString(ConstantString str) {
065: //System.out.println("CONSTANTSTRING: " + str.getBytes(this.pool));
066: this .strings.add(str.getBytes(this .javaClass.getConstantPool())
067: .toString());
068: }
069:
070: private boolean isValidJavaClass(String cls) {
071: if (cls.indexOf("/") == -1) {
072: return false;
073: }
074: if ((!cls.startsWith("(")) && (!cls.startsWith("L"))) {
075: return false;
076: }
077:
078: if ((!cls.endsWith("V")) && (!cls.endsWith(";"))
079: && (!cls.endsWith(")"))) {
080: return false;
081: }
082:
083: return true;
084: }
085:
086: private String[] separateClasses(String utfString) {
087: StringTokenizer tokenizer = new StringTokenizer(utfString, ";");
088: String classes[] = new String[tokenizer.countTokens()];
089: int i = 0;
090: while (tokenizer.hasMoreTokens()) {
091: String cls = tokenizer.nextToken();
092: if (cls.indexOf('/') != -1) {
093: classes[i] = cls;
094: i++;
095: }
096: }
097: return classes;
098: }
099:
100: private String cleanClass(String cls) {
101: int index = cls.lastIndexOf('L');
102: //int index = cls.indexOf('L');
103: String newCls = cls;
104: if (index != -1) {
105: newCls = cls.substring(index + 1);
106: }
107: return newCls;
108: }
109:
110: private String stripClassName(String cls) {
111: //String strippedName = cls.substring(0, cls.lastIndexOf("."));
112: String strippedName = cls.substring(0, cls.lastIndexOf("/"));
113: return strippedName;
114: }
115:
116: public List getAllImports() {
117: return this .imports;
118: }
119:
120: public List getImports(List ignorePackages) {
121: Iterator i = this .imports.iterator();
122: ArrayList imports = new ArrayList();
123: while (i.hasNext()) {
124: String pkg = (String) i.next();
125: //System.out.println("IMPORT: " + pkg);
126: if (ignorePackages.isEmpty() == false) {
127: Iterator filter = ignorePackages.iterator();
128: boolean filterPackage = false;
129: while (filter.hasNext()) {
130: String packageFilter = (String) filter.next();
131: if (pkg.startsWith(packageFilter)) {
132: filterPackage = true;
133: }
134: }
135: if (!filterPackage) {
136: imports.add(pkg);
137: }
138: } else {
139: imports.add(pkg);
140: }
141: }
142: return imports;
143:
144: }
145: }
|