Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 418   Methods: 22
NCLOC: 211   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Utils.java 9.3% 33% 27.3% 25.7%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2003 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   
 
 55   
 package org.apache.tools.ant.taskdefs.optional.sitraka.bytecode;
 56   
 
 57   
 import java.util.Vector;
 58   
 import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ConstantPool;
 59   
 import org.apache.tools.ant.taskdefs.optional.depend.constantpool.Utf8CPInfo;
 60   
 
 61   
 /**
 62   
  * Utilities mostly to manipulate methods and access flags.
 63   
  *
 64   
  * @author <a href="sbailliez@imediation.com">Stephane Bailliez</a>
 65   
  */
 66   
 public class Utils {
 67   
     /** public access flag */
 68   
     public static final short ACC_PUBLIC = 1;
 69   
     /** private access flag */
 70   
     public static final short ACC_PRIVATE = 2;
 71   
     /** protected access flag */
 72   
     public static final short ACC_PROTECTED = 4;
 73   
     /** static access flag */
 74   
     public static final short ACC_STATIC = 8;
 75   
     /** final access flag */
 76   
     public static final short ACC_FINAL = 16;
 77   
     /** super access flag */
 78   
     public static final short ACC_SUPER = 32;
 79   
     /** synchronized access flag */
 80   
     public static final short ACC_SYNCHRONIZED = 32;
 81   
     /** volatile access flag */
 82   
     public static final short ACC_VOLATILE = 64;
 83   
     /** transient access flag */
 84   
     public static final short ACC_TRANSIENT = 128;
 85   
     /** native access flag */
 86   
     public static final short ACC_NATIVE = 256;
 87   
     /** interface access flag */
 88   
     public static final short ACC_INTERFACE = 512;
 89   
     /** abstract access flag */
 90   
     public static final short ACC_ABSTRACT = 1024;
 91   
     /** strict access flag */
 92   
     public static final short ACC_STRICT = 2048;
 93   
 
 94   
     /** private constructor */
 95  0
     private Utils() {
 96   
     }
 97   
 
 98   
     /**
 99   
      * return an UTF8 value from the pool located a a specific index.
 100   
      * @param pool the constant pool to look at
 101   
      * @param index index of the UTF8 value in the constant pool
 102   
      * @return the value of the string if it exists
 103   
      * @throws ClassCastException if the index is not an UTF8 constant.
 104   
      */
 105  638982
     public static String getUTF8Value(ConstantPool pool, int index) {
 106  638982
         return ((Utf8CPInfo) pool.getEntry(index)).getValue();
 107   
     }
 108   
 
 109   
     /**
 110   
      * parse all parameters from a descritor into fields of java name.
 111   
      * @param descriptor of a method.
 112   
      * @return the parameter list of a given method descriptor. Each string
 113   
      * represent a java object with its fully qualified classname or the
 114   
      * primitive name such as int, long, ...
 115   
      */
 116  53
     public static String[] getMethodParams(String descriptor) {
 117  53
         int i = 0;
 118  53
         if (descriptor.charAt(i) != '(') {
 119  0
             throw new IllegalArgumentException("Method descriptor should start with a '('");
 120   
         }
 121  53
         Vector params = new Vector();
 122  53
         StringBuffer param = new StringBuffer();
 123  53
         i++;
 124  ?
         while ((i = descriptor2java(descriptor, i, param)) < descriptor.length()) {
 125  63
             params.add(param.substring(0));
 126  63
             param = new StringBuffer();
 127  63
             if (descriptor.charAt(i) == ')') {
 128  33
                 i++;
 129  33
                 break;
 130   
             }
 131   
         }
 132  53
         String[] array = new String[params.size()];
 133  53
         params.copyInto(array);
 134  53
         return array;
 135   
     }
 136   
 
 137   
     /**
 138   
      * return the object type of a return type.
 139   
      * @param descriptor
 140   
      * @return get the return type objet of a given descriptor
 141   
      */
 142  6
     public static String getMethodReturnType(String descriptor) {
 143  6
         int pos = descriptor.indexOf(')');
 144  6
         StringBuffer rettype = new StringBuffer();
 145  6
         descriptor2java(descriptor, pos + 1, rettype);
 146  6
         return rettype.toString();
 147   
     }
 148   
 
 149   
     /**
 150   
      * Parse a single descriptor symbol and returns it java equivalent.
 151   
      * @param descriptor the descriptor symbol.
 152   
      * @param i the index to look at the symbol in the descriptor string
 153   
      * @param sb the stringbuffer to return the java equivalent of the symbol
 154   
      * @return the index after the descriptor symbol
 155   
      */
 156  89
     public static int descriptor2java(String descriptor, int i, StringBuffer sb) {
 157   
         // get the dimension
 158  89
         StringBuffer dim = new StringBuffer();
 159  89
         for (; descriptor.charAt(i) == '['; i++) {
 160  3
             dim.append("[]");
 161   
         }
 162   
         // now get the type
 163  89
         switch (descriptor.charAt(i)) {
 164   
             case 'B':
 165  0
                 sb.append("byte");
 166  0
                 break;
 167   
             case 'C':
 168  0
                 sb.append("char");
 169  0
                 break;
 170   
             case 'D':
 171  0
                 sb.append("double");
 172  0
                 break;
 173   
             case 'F':
 174  0
                 sb.append("float");
 175  0
                 break;
 176   
             case 'I':
 177  24
                 sb.append("int");
 178  24
                 break;
 179   
             case 'J':
 180  0
                 sb.append("long");
 181  0
                 break;
 182   
             case 'S':
 183  0
                 sb.append("short");
 184  0
                 break;
 185   
             case 'Z':
 186  1
                 sb.append("boolean");
 187  1
                 break;
 188   
             case 'V':
 189  16
                 sb.append("void");
 190  16
                 break;
 191   
             case 'L':
 192   
                 // it is a class
 193  28
                 int pos = descriptor.indexOf(';', i + 1);
 194  28
                 String classname = descriptor.substring(i + 1, pos).replace('/', '.');
 195  28
                 sb.append(classname);
 196  28
                 i = pos;
 197  28
                 break;
 198   
             default:
 199   
                 //@todo, yeah this happens because I got things like:
 200   
                 // ()Ljava/lang/Object; and it will return and ) will be here
 201   
                 // think about it.
 202   
 
 203   
                 //ooooops should never happen
 204   
                 //throw new IllegalArgumentException("Invalid descriptor symbol: '" + i + "' in '" + descriptor + "'");
 205   
         }
 206  89
         sb.append(dim.toString());
 207  89
         return ++i;
 208   
     }
 209   
 
 210   
     /**
 211   
      * check for abstract access
 212   
      * @param access_flags access flags
 213   
      */
 214  3
     public static boolean isAbstract(int access_flags) {
 215  3
         return (access_flags & ACC_ABSTRACT) != 0;
 216   
     }
 217   
 
 218   
     /**
 219   
      * check for public access
 220   
      * @param access_flags access flags
 221   
      */
 222  0
     public static boolean isPublic(int access_flags) {
 223  0
         return (access_flags & ACC_PUBLIC) != 0;
 224   
     }
 225   
 
 226   
     /**
 227   
      * check for a static access
 228   
      * @param access_flags access flags
 229   
      */
 230  0
     public static boolean isStatic(int access_flags) {
 231  0
         return (access_flags & ACC_STATIC) != 0;
 232   
     }
 233   
 
 234   
     /**
 235   
      *  check for native access
 236   
      * @param access_flags access flags
 237   
      */
 238  0
     public static boolean isNative(int access_flags) {
 239  0
         return (access_flags & ACC_NATIVE) != 0;
 240   
     }
 241   
 
 242   
     /**
 243   
      * check for class access
 244   
      * @param access_flags access flags
 245   
      */
 246  0
     public static boolean isClass(int access_flags) {
 247  0
         return !isInterface(access_flags);
 248   
     }
 249   
 
 250   
     /**
 251   
      * check for strict access
 252   
      * @param access_flags access flags
 253   
      */
 254  0
     public static boolean isStrict(int access_flags) {
 255  0
         return (access_flags & ACC_STRICT) != 0;
 256   
     }
 257   
 
 258   
     /**
 259   
      * check for interface access
 260   
      * @param access_flags access flags
 261   
      */
 262  8692
     public static boolean isInterface(int access_flags) {
 263  8692
         return (access_flags & ACC_INTERFACE) != 0;
 264   
     }
 265   
 
 266   
     /**
 267   
      * check for private access
 268   
      * @param access_flags access flags
 269   
      */
 270  0
     public static boolean isPrivate(int access_flags) {
 271  0
         return (access_flags & ACC_PRIVATE) != 0;
 272   
     }
 273   
 
 274   
     /**
 275   
      * check for transient flag
 276   
      * @param access_flags access flags
 277   
      */
 278  0
     public static boolean isTransient(int access_flags) {
 279  0
         return (access_flags & ACC_TRANSIENT) != 0;
 280   
     }
 281   
 
 282   
     /**
 283   
      * check for volatile flag
 284   
      * @param access_flags access flags
 285   
      */
 286  0
     public static boolean isVolatile(int access_flags) {
 287  0
         return (access_flags & ACC_VOLATILE) != 0;
 288   
     }
 289   
 
 290   
     /**
 291   
      * check for super flag
 292   
      * @param access_flags access flag
 293   
      */
 294  0
     public static boolean isSuper(int access_flags) {
 295  0
         return (access_flags & ACC_SUPER) != 0;
 296   
     }
 297   
 
 298   
     /**
 299   
      * check for protected flag
 300   
      * @param access_flags access flags
 301   
      */
 302  0
     public static boolean isProtected(int access_flags) {
 303  0
         return (access_flags & ACC_PROTECTED) != 0;
 304   
     }
 305   
 
 306   
     /**
 307   
      * chck for final flag
 308   
      * @param access_flags access flags
 309   
      */
 310  0
     public static boolean isFinal(int access_flags) {
 311  0
         return (access_flags & ACC_FINAL) != 0;
 312   
     }
 313   
 
 314   
     /**
 315   
      * check for synchronized flag
 316   
      * @param access_flags access flags
 317   
      */
 318  0
     public static boolean isSynchronized(int access_flags) {
 319  0
         return (access_flags & ACC_SYNCHRONIZED) != 0;
 320   
     }
 321   
 
 322   
     /**
 323   
      * return the method access flag as java modifiers
 324   
      * @param access_flags access flags
 325   
      * @return the access flags as modifier strings
 326   
      */
 327  0
     public static String getMethodAccess(int access_flags) {
 328  0
         StringBuffer sb = new StringBuffer();
 329  0
         if (isPublic(access_flags)) {
 330  0
             sb.append("public ");
 331  0
         } else if (isPrivate(access_flags)) {
 332  0
             sb.append("private ");
 333  0
         } else if (isProtected(access_flags)) {
 334  0
             sb.append("protected ");
 335   
         }
 336  0
         if (isFinal(access_flags)) {
 337  0
             sb.append("final ");
 338   
         }
 339  0
         if (isStatic(access_flags)) {
 340  0
             sb.append("static ");
 341   
         }
 342  0
         if (isSynchronized(access_flags)) {
 343  0
             sb.append("synchronized ");
 344   
         }
 345  0
         if (isNative(access_flags)) {
 346  0
             sb.append("native ");
 347   
         }
 348  0
         if (isAbstract(access_flags)) {
 349  0
             sb.append("abstract ");
 350   
         }
 351  0
         return sb.toString().trim();
 352   
     }
 353   
 
 354   
     /**
 355   
      * return the field access flag as java modifiers
 356   
      * @param access_flags access flags
 357   
      * @return the access flags as modifier strings
 358   
      */
 359  0
     public static String getFieldAccess(int access_flags) {
 360  0
         StringBuffer sb = new StringBuffer();
 361  0
         if (isPublic(access_flags)) {
 362  0
             sb.append("public ");
 363  0
         } else if (isPrivate(access_flags)) {
 364  0
             sb.append("private ");
 365  0
         } else if (isProtected(access_flags)) {
 366  0
             sb.append("protected ");
 367   
         }
 368  0
         if (isFinal(access_flags)) {
 369  0
             sb.append("final ");
 370   
         }
 371  0
         if (isStatic(access_flags)) {
 372  0
             sb.append("static ");
 373   
         }
 374  0
         if (isVolatile(access_flags)) {
 375  0
             sb.append("volatile ");
 376   
         }
 377  0
         if (isTransient(access_flags)) {
 378  0
             sb.append("transient ");
 379   
         }
 380  0
         return sb.toString().trim();
 381   
     }
 382   
 
 383   
     /**
 384   
      * return the class access flag as java modifiers
 385   
      * @param access_flags access flags
 386   
      * @return the access flags as modifier strings
 387   
      */
 388  0
     public static String getClassAccess(int access_flags) {
 389  0
         StringBuffer sb = new StringBuffer();
 390  0
         if (isPublic(access_flags)) {
 391  0
             sb.append("public ");
 392  0
         } else if (isProtected(access_flags)) {
 393  0
             sb.append("protected ");
 394  0
         } else if (isPrivate(access_flags)) {
 395  0
             sb.append("private ");
 396   
         }
 397  0
         if (isFinal(access_flags)) {
 398  0
             sb.append("final ");
 399   
         }
 400  0
         if (isSuper(access_flags)) {
 401  0
             sb.append("/*super*/ ");
 402   
         }
 403  0
         if (isInterface(access_flags)) {
 404  0
             sb.append("interface ");
 405   
         }
 406  0
         if (isAbstract(access_flags)) {
 407  0
             sb.append("abstract ");
 408   
         }
 409  0
         if (isClass(access_flags)) {
 410  0
             sb.append("class ");
 411   
         }
 412  0
         return sb.toString().trim();
 413   
     }
 414   
 }
 415   
 
 416   
 
 417   
 
 418