Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 392   Methods: 14
NCLOC: 158   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ConstantPool.java 20% 28.4% 42.9% 26.9%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000-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.taskdefs.optional.depend.constantpool;
 55   
 
 56   
 import java.io.DataInputStream;
 57   
 import java.io.IOException;
 58   
 import java.util.Enumeration;
 59   
 import java.util.Hashtable;
 60   
 import java.util.Vector;
 61   
 
 62   
 /**
 63   
  * The constant pool of a Java class. The constant pool is a collection of
 64   
  * constants used in a Java class file. It stores strings, constant values,
 65   
  * class names, method names, field names etc.
 66   
  *
 67   
  * @author Conor MacNeill
 68   
  * @see <a href="http://java.sun.com/docs/books/vmspec/">The Java Virtual
 69   
  *      Machine Specification</a>
 70   
  */
 71   
 public class ConstantPool {
 72   
 
 73   
     /** The entries in the constant pool. */
 74   
     private Vector entries;
 75   
 
 76   
     /**
 77   
      * A Hashtable of UTF8 entries - used to get constant pool indexes of
 78   
      * the UTF8 values quickly
 79   
      */
 80   
     private Hashtable utf8Indexes;
 81   
 
 82   
     /** Initialise the constant pool. */
 83  17414
     public ConstantPool() {
 84  17414
         entries = new Vector();
 85   
 
 86   
         // The zero index is never present in the constant pool itself so
 87   
         // we add a null entry for it
 88  17414
         entries.addElement(null);
 89   
 
 90  17414
         utf8Indexes = new Hashtable();
 91   
     }
 92   
 
 93   
     /**
 94   
      * Read the constant pool from a class input stream.
 95   
      *
 96   
      * @param classStream the DataInputStream of a class file.
 97   
      * @exception IOException if there is a problem reading the constant pool
 98   
      *      from the stream
 99   
      */
 100  17414
     public void read(DataInputStream classStream) throws IOException {
 101  17414
         int numEntries = classStream.readUnsignedShort();
 102   
 
 103  17414
         for (int i = 1; i < numEntries;) {
 104  1936022
             ConstantPoolEntry nextEntry
 105   
                  = ConstantPoolEntry.readEntry(classStream);
 106   
 
 107  1936022
             i += nextEntry.getNumEntries();
 108   
 
 109  1936022
             addEntry(nextEntry);
 110   
         }
 111   
     }
 112   
 
 113   
     /**
 114   
      * Get the size of the constant pool.
 115   
      *
 116   
      * @return the size of the constant pool
 117   
      */
 118  476
     public int size() {
 119  476
         return entries.size();
 120   
     }
 121   
 
 122   
     /**
 123   
      * Add an entry to the constant pool.
 124   
      *
 125   
      * @param entry the new entry to be added to the constant pool.
 126   
      * @return the index into the constant pool at which the entry is
 127   
      *      stored.
 128   
      */
 129  1936022
     public int addEntry(ConstantPoolEntry entry) {
 130  1936022
         int index = entries.size();
 131   
 
 132  1936022
         entries.addElement(entry);
 133   
 
 134  1936022
         int numSlots = entry.getNumEntries();
 135   
 
 136   
         // add null entries for any additional slots required.
 137  1936022
         for (int j = 0; j < numSlots - 1; ++j) {
 138  2654
             entries.addElement(null);
 139   
         }
 140   
 
 141  1936022
         if (entry instanceof Utf8CPInfo) {
 142  978500
             Utf8CPInfo utf8Info = (Utf8CPInfo) entry;
 143   
 
 144  978500
             utf8Indexes.put(utf8Info.getValue(), new Integer(index));
 145   
         }
 146   
 
 147  1936022
         return index;
 148   
     }
 149   
 
 150   
     /**
 151   
      * Resolve the entries in the constant pool. Resolution of the constant
 152   
      * pool involves transforming indexes to other constant pool entries
 153   
      * into the actual data for that entry.
 154   
      */
 155  17414
     public void resolve() {
 156  17414
         for (Enumeration i = entries.elements(); i.hasMoreElements();) {
 157  1956090
             ConstantPoolEntry poolInfo = (ConstantPoolEntry) i.nextElement();
 158   
 
 159  1956090
             if (poolInfo != null && !poolInfo.isResolved()) {
 160  1583137
                 poolInfo.resolve(this);
 161   
             }
 162   
         }
 163   
     }
 164   
 
 165   
 
 166   
     /**
 167   
      * Get an constant pool entry at a particular index.
 168   
      *
 169   
      * @param index the index into the constant pool.
 170   
      * @return the constant pool entry at that index.
 171   
      */
 172  2754049
     public ConstantPoolEntry getEntry(int index) {
 173  2754049
         return (ConstantPoolEntry) entries.elementAt(index);
 174   
     }
 175   
 
 176   
     /**
 177   
      * Get the index of a given UTF8 constant pool entry.
 178   
      *
 179   
      * @param value the string value of the UTF8 entry.
 180   
      * @return the index at which the given string occurs in the constant
 181   
      *      pool or -1 if the value does not occur.
 182   
      */
 183  0
     public int getUTF8Entry(String value) {
 184  0
         int index = -1;
 185  0
         Integer indexInteger = (Integer) utf8Indexes.get(value);
 186   
 
 187  0
         if (indexInteger != null) {
 188  0
             index = indexInteger.intValue();
 189   
         }
 190   
 
 191  0
         return index;
 192   
     }
 193   
 
 194   
     /**
 195   
      * Get the index of a given CONSTANT_CLASS entry in the constant pool.
 196   
      *
 197   
      * @param className the name of the class for which the class entry
 198   
      *      index is required.
 199   
      * @return the index at which the given class entry occurs in the
 200   
      *      constant pool or -1 if the value does not occur.
 201   
      */
 202  0
     public int getClassEntry(String className) {
 203  0
         int index = -1;
 204   
 
 205  0
         for (int i = 0; i < entries.size() && index == -1; ++i) {
 206  0
             Object element = entries.elementAt(i);
 207   
 
 208  0
             if (element instanceof ClassCPInfo) {
 209  0
                 ClassCPInfo classinfo = (ClassCPInfo) element;
 210   
 
 211  0
                 if (classinfo.getClassName().equals(className)) {
 212  0
                     index = i;
 213   
                 }
 214   
             }
 215   
         }
 216   
 
 217  0
         return index;
 218   
     }
 219   
 
 220   
     /**
 221   
      * Get the index of a given constant value entry in the constant pool.
 222   
      *
 223   
      * @param constantValue the constant value for which the index is
 224   
      *      required.
 225   
      * @return the index at which the given value entry occurs in the
 226   
      *      constant pool or -1 if the value does not occur.
 227   
      */
 228  0
     public int getConstantEntry(Object constantValue) {
 229  0
         int index = -1;
 230   
 
 231  0
         for (int i = 0; i < entries.size() && index == -1; ++i) {
 232  0
             Object element = entries.elementAt(i);
 233   
 
 234  0
             if (element instanceof ConstantCPInfo) {
 235  0
                 ConstantCPInfo constantEntry = (ConstantCPInfo) element;
 236   
 
 237  0
                 if (constantEntry.getValue().equals(constantValue)) {
 238  0
                     index = i;
 239   
                 }
 240   
             }
 241   
         }
 242   
 
 243  0
         return index;
 244   
     }
 245   
 
 246   
     /**
 247   
      * Get the index of a given CONSTANT_METHODREF entry in the constant
 248   
      * pool.
 249   
      *
 250   
      * @param methodClassName the name of the class which contains the
 251   
      *      method being referenced.
 252   
      * @param methodName the name of the method being referenced.
 253   
      * @param methodType the type descriptor of the metho dbeing referenced.
 254   
      * @return the index at which the given method ref entry occurs in the
 255   
      *      constant pool or -1 if the value does not occur.
 256   
      */
 257  0
     public int getMethodRefEntry(String methodClassName, String methodName,
 258   
                                  String methodType) {
 259  0
         int index = -1;
 260   
 
 261  0
         for (int i = 0; i < entries.size() && index == -1; ++i) {
 262  0
             Object element = entries.elementAt(i);
 263   
 
 264  0
             if (element instanceof MethodRefCPInfo) {
 265  0
                 MethodRefCPInfo methodRefEntry = (MethodRefCPInfo) element;
 266   
 
 267  0
                 if (methodRefEntry.getMethodClassName().equals(methodClassName)
 268   
                      && methodRefEntry.getMethodName().equals(methodName)
 269   
                      && methodRefEntry.getMethodType().equals(methodType)) {
 270  0
                     index = i;
 271   
                 }
 272   
             }
 273   
         }
 274   
 
 275  0
         return index;
 276   
     }
 277   
 
 278   
     /**
 279   
      * Get the index of a given CONSTANT_INTERFACEMETHODREF entry in the
 280   
      * constant pool.
 281   
      *
 282   
      * @param interfaceMethodClassName the name of the interface which
 283   
      *      contains the method being referenced.
 284   
      * @param interfaceMethodName the name of the method being referenced.
 285   
      * @param interfaceMethodType the type descriptor of the metho dbeing
 286   
      *      referenced.
 287   
      * @return the index at which the given method ref entry occurs in the
 288   
      *      constant pool or -1 if the value does not occur.
 289   
      */
 290  0
     public int getInterfaceMethodRefEntry(String interfaceMethodClassName,
 291   
                                           String interfaceMethodName,
 292   
                                           String interfaceMethodType) {
 293  0
         int index = -1;
 294   
 
 295  0
         for (int i = 0; i < entries.size() && index == -1; ++i) {
 296  0
             Object element = entries.elementAt(i);
 297   
 
 298  0
             if (element instanceof InterfaceMethodRefCPInfo) {
 299  0
                 InterfaceMethodRefCPInfo interfaceMethodRefEntry
 300   
                      = (InterfaceMethodRefCPInfo) element;
 301   
 
 302  0
                 if (interfaceMethodRefEntry.getInterfaceMethodClassName().equals(interfaceMethodClassName)
 303   
                      && interfaceMethodRefEntry.getInterfaceMethodName().equals(interfaceMethodName)
 304   
                      && interfaceMethodRefEntry.getInterfaceMethodType().equals(interfaceMethodType)) {
 305  0
                     index = i;
 306   
                 }
 307   
             }
 308   
         }
 309   
 
 310  0
         return index;
 311   
     }
 312   
 
 313   
     /**
 314   
      * Get the index of a given CONSTANT_FIELDREF entry in the constant
 315   
      * pool.
 316   
      *
 317   
      * @param fieldClassName the name of the class which contains the field
 318   
      *      being referenced.
 319   
      * @param fieldName the name of the field being referenced.
 320   
      * @param fieldType the type descriptor of the field being referenced.
 321   
      * @return the index at which the given field ref entry occurs in the
 322   
      *      constant pool or -1 if the value does not occur.
 323   
      */
 324  0
     public int getFieldRefEntry(String fieldClassName, String fieldName,
 325   
                                 String fieldType) {
 326  0
         int index = -1;
 327   
 
 328  0
         for (int i = 0; i < entries.size() && index == -1; ++i) {
 329  0
             Object element = entries.elementAt(i);
 330   
 
 331  0
             if (element instanceof FieldRefCPInfo) {
 332  0
                 FieldRefCPInfo fieldRefEntry = (FieldRefCPInfo) element;
 333   
 
 334  0
                 if (fieldRefEntry.getFieldClassName().equals(fieldClassName)
 335   
                      && fieldRefEntry.getFieldName().equals(fieldName)
 336   
                      && fieldRefEntry.getFieldType().equals(fieldType)) {
 337  0
                     index = i;
 338   
                 }
 339   
             }
 340   
         }
 341   
 
 342  0
         return index;
 343   
     }
 344   
 
 345   
     /**
 346   
      * Get the index of a given CONSTANT_NAMEANDTYPE entry in the constant
 347   
      * pool.
 348   
      *
 349   
      * @param name the name
 350   
      * @param type the type
 351   
      * @return the index at which the given NameAndType entry occurs in the
 352   
      *      constant pool or -1 if the value does not occur.
 353   
      */
 354  0
     public int getNameAndTypeEntry(String name, String type) {
 355  0
         int index = -1;
 356   
 
 357  0
         for (int i = 0; i < entries.size() && index == -1; ++i) {
 358  0
             Object element = entries.elementAt(i);
 359   
 
 360  0
             if (element instanceof NameAndTypeCPInfo) {
 361  0
                 NameAndTypeCPInfo nameAndTypeEntry 
 362   
                     = (NameAndTypeCPInfo) element;
 363   
 
 364  0
                 if (nameAndTypeEntry.getName().equals(name)
 365   
                      && nameAndTypeEntry.getType().equals(type)) {
 366  0
                     index = i;
 367   
                 }
 368   
             }
 369   
         }
 370   
 
 371  0
         return index;
 372   
     }
 373   
 
 374   
     /**
 375   
      * Dump the constant pool to a string.
 376   
      *
 377   
      * @return the constant pool entries as strings
 378   
      */
 379  0
     public String toString() {
 380  0
         StringBuffer sb = new StringBuffer("\n");
 381  0
         int size = entries.size();
 382   
 
 383  0
         for (int i = 0; i < size; ++i) {
 384  0
             sb.append("[" + i + "] = " + getEntry(i) + "\n");
 385   
         }
 386   
 
 387  0
         return sb.toString();
 388   
     }
 389   
 
 390   
 }
 391   
 
 392