Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 204   Methods: 11
NCLOC: 71   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DependencyVisitor.java 87.5% 88.9% 90.9% 89.1%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 package org.apache.tools.ant.util.depend.bcel;
 55   
 
 56   
 import java.util.Enumeration;
 57   
 import java.util.Hashtable;
 58   
 import java.util.StringTokenizer;
 59   
 import org.apache.bcel.classfile.ConstantClass;
 60   
 import org.apache.bcel.classfile.ConstantPool;
 61   
 import org.apache.bcel.classfile.EmptyVisitor;
 62   
 import org.apache.bcel.classfile.Field;
 63   
 import org.apache.bcel.classfile.JavaClass;
 64   
 import org.apache.bcel.classfile.Method;
 65   
 import org.apache.bcel.classfile.ConstantNameAndType;
 66   
 
 67   
 /**
 68   
  * A BCEL visitor implementation to collect class dependency information
 69   
  *
 70   
  * @author Conor MacNeill
 71   
  * @author <a href="mailto:hengels@innovidata.com">Holger Engels</a>
 72   
  */
 73   
 public class DependencyVisitor extends EmptyVisitor {
 74   
     /** The collectd dependencies */
 75   
     private Hashtable dependencies = new Hashtable();
 76   
     /**
 77   
      * The current class's constant pool - used to determine class names
 78   
      * from class references.
 79   
      */
 80   
     private ConstantPool constantPool;
 81   
 
 82   
     /**
 83   
      * Get the dependencies collected by this visitor
 84   
      *
 85   
      * @return a Enumeration of classnames, being the classes upon which the
 86   
      *      visited classes depend.
 87   
      */
 88  32
     public Enumeration getDependencies() {
 89  32
         return dependencies.keys();
 90   
     }
 91   
 
 92   
     /** Clear the curretn set of collected dependencies. */
 93  0
     public void clearDependencies() {
 94  0
         dependencies.clear();
 95   
     }
 96   
 
 97   
     /**
 98   
      * Visit the constant pool of a class
 99   
      *
 100   
      * @param constantPool the constant pool of the class being visited.
 101   
      */
 102  34
     public void visitConstantPool(ConstantPool constantPool) {
 103  34
         this.constantPool = constantPool;
 104   
     }
 105   
 
 106   
     /**
 107   
      * Visit a class reference
 108   
      *
 109   
      * @param constantClass the constantClass entry for the class reference
 110   
      */
 111  84
     public void visitConstantClass(ConstantClass constantClass) {
 112  84
         String classname
 113   
              = constantClass.getConstantValue(constantPool).toString();
 114  84
         addSlashClass(classname);
 115   
     }
 116   
 
 117   
     /**
 118   
      * Visit a name and type ref
 119   
      *
 120   
      * Look for class references in this
 121   
      */
 122  54
     public void visitConstantNameAndType(ConstantNameAndType obj) {
 123  54
         String name = obj.getName(constantPool);
 124  54
         if (obj.getSignature(constantPool).equals("Ljava/lang/Class;")
 125   
                 && name.startsWith("class$")) {
 126  2
             String classname = name.substring(6).replace('$', '.');
 127  2
             addClass(classname);
 128  2
             int index = classname.lastIndexOf(".");
 129  2
             if (index != -1) {
 130  0
                 classname = classname.substring(0, index) + "$" +
 131   
                             classname.substring(index + 1);
 132  0
                 addClass(classname);
 133   
             }
 134   
         }
 135   
     }
 136   
 
 137   
     /**
 138   
      * Visit a field of the class.
 139   
      *
 140   
      * @param field the field being visited
 141   
      */
 142  8
     public void visitField(Field field) {
 143  8
         addClasses(field.getSignature());
 144   
     }
 145   
 
 146   
     /**
 147   
      * Visit a Java class
 148   
      *
 149   
      * @param javaClass the class being visited.
 150   
      */
 151  34
     public void visitJavaClass(JavaClass javaClass) {
 152  34
         addClass(javaClass.getClassName());
 153   
     }
 154   
 
 155   
     /**
 156   
      * Visit a method of the current class
 157   
      *
 158   
      * @param method the method being visited.
 159   
      */
 160  36
     public void visitMethod(Method method) {
 161  36
         String signature = method.getSignature();
 162  36
         int pos = signature.indexOf(")");
 163  36
         addClasses(signature.substring(1, pos));
 164  36
         addClasses(signature.substring(pos + 1));
 165   
     }
 166   
 
 167   
     /**
 168   
      * Add a classname to the list of dependency classes
 169   
      *
 170   
      * @param classname the class to be added to the list of dependencies.
 171   
      */
 172  132
     void addClass(String classname) {
 173  132
         dependencies.put(classname, classname);
 174   
     }
 175   
 
 176   
     /**
 177   
      * Add all the classes from a descriptor string.
 178   
      *
 179   
      * @param string the descriptor string, being descriptors separated by
 180   
      *      ';' characters.
 181   
      */
 182  80
     private void addClasses(String string) {
 183  80
         StringTokenizer tokens = new StringTokenizer(string, ";");
 184  80
         while (tokens.hasMoreTokens()) {
 185  46
             String descriptor = tokens.nextToken();
 186  46
             int pos = descriptor.indexOf('L');
 187  46
             if (pos != -1) {
 188  12
                 addSlashClass(descriptor.substring(pos + 1));
 189   
             }
 190   
         }
 191   
     }
 192   
 
 193   
     /**
 194   
      * Adds a class name in slash format
 195   
      * (for example org/apache/tools/ant/Main).
 196   
      *
 197   
      * @param classname the class name in slash format
 198   
      */
 199  96
     private void addSlashClass(String classname) {
 200  96
         addClass(classname.replace('/', '.'));
 201   
     }
 202   
 }
 203   
 
 204