1 18 package org.apache.tools.ant.util.depend.bcel; 19 20 import java.util.Enumeration ; 21 import java.util.Hashtable ; 22 import java.util.StringTokenizer ; 23 import org.apache.bcel.classfile.ConstantClass; 24 import org.apache.bcel.classfile.ConstantPool; 25 import org.apache.bcel.classfile.EmptyVisitor; 26 import org.apache.bcel.classfile.Field; 27 import org.apache.bcel.classfile.JavaClass; 28 import org.apache.bcel.classfile.Method; 29 import org.apache.bcel.classfile.ConstantNameAndType; 30 31 35 public class DependencyVisitor extends EmptyVisitor { 36 37 private Hashtable dependencies = new Hashtable (); 38 42 private ConstantPool constantPool; 43 44 50 public Enumeration getDependencies() { 51 return dependencies.keys(); 52 } 53 54 55 public void clearDependencies() { 56 dependencies.clear(); 57 } 58 59 64 public void visitConstantPool(ConstantPool constantPool) { 65 this.constantPool = constantPool; 66 } 67 68 73 public void visitConstantClass(ConstantClass constantClass) { 74 String classname 75 = constantClass.getConstantValue(constantPool).toString(); 76 addSlashClass(classname); 77 } 78 79 86 public void visitConstantNameAndType(ConstantNameAndType obj) { 87 String name = obj.getName(constantPool); 88 if (obj.getSignature(constantPool).equals("Ljava/lang/Class;") 89 && name.startsWith("class$")) { 90 String classname = name.substring(6).replace('$', '.'); 91 int index = classname.lastIndexOf("."); 93 if (index > 0) { 94 char start; 95 int index2 = classname.lastIndexOf(".", index - 1); 97 if (index2 != -1) { 98 start = classname.charAt(index2 + 1); 100 } else { 101 start = classname.charAt(0); 103 } 104 if ((start > 0x40) && (start < 0x5B)) { 106 classname = classname.substring(0, index) + "$" 109 + classname.substring(index + 1); 110 addClass(classname); 111 } else { 112 addClass(classname); 114 } 115 } else { 116 addClass(classname); 118 } 119 } 120 } 121 122 127 public void visitField(Field field) { 128 addClasses(field.getSignature()); 129 } 130 131 136 public void visitJavaClass(JavaClass javaClass) { 137 addClass(javaClass.getClassName()); 138 } 139 140 145 public void visitMethod(Method method) { 146 String signature = method.getSignature(); 147 int pos = signature.indexOf(")"); 148 addClasses(signature.substring(1, pos)); 149 addClasses(signature.substring(pos + 1)); 150 } 151 152 157 void addClass(String classname) { 158 dependencies.put(classname, classname); 159 } 160 161 167 private void addClasses(String string) { 168 StringTokenizer tokens = new StringTokenizer (string, ";"); 169 while (tokens.hasMoreTokens()) { 170 String descriptor = tokens.nextToken(); 171 int pos = descriptor.indexOf('L'); 172 if (pos != -1) { 173 addSlashClass(descriptor.substring(pos + 1)); 174 } 175 } 176 } 177 178 184 private void addSlashClass(String classname) { 185 addClass(classname.replace('/', '.')); 186 } 187 } 188 189 | Popular Tags |