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 String sCls = cls.getBytes(this.javaClass.getConstantPool()); 20 if (sCls.indexOf("/") != -1) { 21 sCls = sCls.replace('/', '.'); 22 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 String utfString = utf.toString().substring(utf.toString().indexOf('"') + 1,utf.toString().lastIndexOf('"')); 35 if (isValidJavaClass(utfString)) { 37 if (!this.strings.contains(utfString)) { 38 String [] classes = this.separateClasses(utfString); 39 for (int i = 0; i < classes.length; i++) { 40 if (classes[i] != null) { 41 String cls = classes[i].replace('/', '.'); 42 cls = this.stripClassName(cls); 43 cls = this.cleanClass(cls); 44 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 this.strings.add(str.getBytes(this.javaClass.getConstantPool()).toString()); 58 } 59 60 private boolean isValidJavaClass(String 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 [] separateClasses(String utfString) { 76 StringTokenizer tokenizer = new StringTokenizer(utfString, ";"); 77 String classes[] = new String [tokenizer.countTokens()]; 78 int i = 0; 79 while (tokenizer.hasMoreTokens()) { 80 String cls = tokenizer.nextToken(); 81 if (cls.indexOf('/') != -1) { 82 classes[i] = cls; 83 i++; 84 } 85 } 86 return classes; 87 } 88 89 private String cleanClass(String cls) { 90 91 int index = cls.indexOf('L'); 92 String newCls = cls; 93 if (index != -1) { 94 newCls = cls.substring(index + 1); 95 } 96 return newCls; 98 } 100 101 private String stripClassName(String 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 pkg = (String ) i.next(); 113 if (ignorePackages.isEmpty() == false) { 114 Iterator filter = ignorePackages.iterator(); 115 boolean filterPackage = false; 116 while (filter.hasNext()) { 117 String packageFilter = (String ) 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 |