FileDocCategorySizeDatePackage
DependencyVisitor.javaAPI DocApache Ant 1.706394Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.util.depend.bcel

DependencyVisitor

public class DependencyVisitor extends org.apache.bcel.classfile.EmptyVisitor
A BCEL visitor implementation to collect class dependency information

Fields Summary
private Hashtable
dependencies
The collectd dependencies
private org.apache.bcel.classfile.ConstantPool
constantPool
The current class's constant pool - used to determine class names from class references.
Constructors Summary
Methods Summary
voidaddClass(java.lang.String classname)
Add a classname to the list of dependency classes

param
classname the class to be added to the list of dependencies.

        dependencies.put(classname, classname);
    
private voidaddClasses(java.lang.String string)
Add all the classes from a descriptor string.

param
string the descriptor string, being descriptors separated by ';' characters.

        StringTokenizer tokens = new StringTokenizer(string, ";");
        while (tokens.hasMoreTokens()) {
            String descriptor = tokens.nextToken();
            int pos = descriptor.indexOf('L");
            if (pos != -1) {
                addSlashClass(descriptor.substring(pos + 1));
            }
        }
    
private voidaddSlashClass(java.lang.String classname)
Adds a class name in slash format (for example org/apache/tools/ant/Main).

param
classname the class name in slash format

        addClass(classname.replace('/", '."));
    
public voidclearDependencies()
Clear the curretn set of collected dependencies.

        dependencies.clear();
    
public java.util.EnumerationgetDependencies()
Get the dependencies collected by this visitor

return
a Enumeration of classnames, being the classes upon which the visited classes depend.


                                   
       
        return dependencies.keys();
    
public voidvisitConstantClass(org.apache.bcel.classfile.ConstantClass constantClass)
Visit a class reference

param
constantClass the constantClass entry for the class reference

        String classname
             = constantClass.getConstantValue(constantPool).toString();
        addSlashClass(classname);
    
public voidvisitConstantNameAndType(org.apache.bcel.classfile.ConstantNameAndType obj)
Visit a name and type ref Look for class references in this

param
obj the name and type reference being visited.

        String name = obj.getName(constantPool);
        if (obj.getSignature(constantPool).equals("Ljava/lang/Class;")
                && name.startsWith("class$")) {
            String classname = name.substring(6).replace('$", '.");
            // does the class have a package structure
            int index = classname.lastIndexOf(".");
            if (index > 0) {
                char start;
                // check if the package structure is more than 1 level deep
                int index2 = classname.lastIndexOf(".", index - 1);
                if (index2 != -1) {
                    // class name has more than 1 package level 'com.company.Class'
                    start = classname.charAt(index2 + 1);
                } else {
                    // class name has only 1 package level 'package.Class'
                    start = classname.charAt(0);
                }
                // Check to see if it's an inner class 'com.company.Class$Inner'
                if ((start > 0x40) && (start < 0x5B)) {
                    // first letter of the previous segment of the class name 'Class'
                    // is upper case ascii. so according to the spec it's an inner class
                    classname = classname.substring(0, index) + "$"
                        + classname.substring(index + 1);
                    addClass(classname);
                } else {
                    // Add the class in dotted notation 'com.company.Class'
                    addClass(classname);
                }
            } else {
                // Add a class with no package 'Class'
                addClass(classname);
            }
        }
    
public voidvisitConstantPool(org.apache.bcel.classfile.ConstantPool constantPool)
Visit the constant pool of a class

param
constantPool the constant pool of the class being visited.

        this.constantPool = constantPool;
    
public voidvisitField(org.apache.bcel.classfile.Field field)
Visit a field of the class.

param
field the field being visited

        addClasses(field.getSignature());
    
public voidvisitJavaClass(org.apache.bcel.classfile.JavaClass javaClass)
Visit a Java class

param
javaClass the class being visited.

        addClass(javaClass.getClassName());
    
public voidvisitMethod(org.apache.bcel.classfile.Method method)
Visit a method of the current class

param
method the method being visited.

        String signature = method.getSignature();
        int pos = signature.indexOf(")");
        addClasses(signature.substring(1, pos));
        addClasses(signature.substring(pos + 1));