Clover coverage report - Common - 1.0.0
Coverage timestamp: sam. déc. 27 2003 15:13:46 CET
file stats: LOC: 190   Methods: 4
NCLOC: 131   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ClassTools.java 92,9% 95,2% 100% 94,5%
coverage coverage
 1   
 /*
 2   
  * EJTools, the Enterprise Java Tools
 3   
  *
 4   
  * Distributable under LGPL license.
 5   
  * See terms of license at www.gnu.org.
 6   
  */
 7   
 package org.ejtools.util;
 8   
 
 9   
 import java.util.ArrayList;
 10   
 
 11   
 
 12   
 /**
 13   
  * Utility class to handle Class realted stuff.
 14   
  *
 15   
  * @author    Laurent Etiemble
 16   
  * @version   $Revision: 1.7 $
 17   
  */
 18   
 public class ClassTools
 19   
 {
 20   
    /** Holds the numeric classes */
 21   
    private static ArrayList numericClasses;
 22   
 
 23   
 
 24   
    /**
 25   
     * Pretty print a full qualified class name. Specially useful for object arrays.
 26   
     *
 27   
     * @param className  The fully qualified class name
 28   
     * @return           The pretty class name
 29   
     */
 30  88
    public static String classDisplay(String className)
 31   
    {
 32  88
       String result = className;
 33   
 
 34  88
       if (className == null)
 35   
       {
 36  0
          return null;
 37   
       }
 38   
 
 39  88
       if (className.startsWith("[Z"))
 40   
       {
 41  8
          result = "boolean[]";
 42   
       }
 43  88
       if (className.startsWith("[C"))
 44   
       {
 45  8
          result = "char[]";
 46   
       }
 47  88
       if (className.startsWith("[D"))
 48   
       {
 49  8
          result = "double[]";
 50   
       }
 51  88
       if (className.startsWith("[F"))
 52   
       {
 53  8
          result = "float[]";
 54   
       }
 55  88
       if (className.startsWith("[I"))
 56   
       {
 57  8
          result = "int[]";
 58   
       }
 59  88
       if (className.startsWith("[S"))
 60   
       {
 61  8
          result = "short[]";
 62   
       }
 63  88
       if (className.startsWith("[J"))
 64   
       {
 65  8
          result = "long[]";
 66   
       }
 67  88
       if (className.startsWith("[B"))
 68   
       {
 69  8
          result = "byte[]";
 70   
       }
 71  88
       if (className.startsWith("[L"))
 72   
       {
 73  24
          result = className.substring(2, className.length() - 1) + "[]";
 74   
       }
 75   
 
 76  88
       return result;
 77   
    }
 78   
 
 79   
 
 80   
    /**
 81   
     * Get the class by its name.
 82   
     *
 83   
     * @param fullPathClassName  The full qualified name of the class
 84   
     * @return                   The class if it is found, otherwise null
 85   
     */
 86  96
    public static Class getClass(String fullPathClassName)
 87   
    {
 88   
       // Check if the class is a primitive type
 89  96
       if (fullPathClassName.equals("void"))
 90   
       {
 91  8
          return Void.TYPE;
 92   
       }
 93  88
       if (fullPathClassName.equals("int"))
 94   
       {
 95  8
          return Integer.TYPE;
 96   
       }
 97  80
       if (fullPathClassName.equals("short"))
 98   
       {
 99  8
          return Short.TYPE;
 100   
       }
 101  72
       if (fullPathClassName.equals("long"))
 102   
       {
 103  8
          return Long.TYPE;
 104   
       }
 105  64
       if (fullPathClassName.equals("byte"))
 106   
       {
 107  8
          return Byte.TYPE;
 108   
       }
 109  56
       if (fullPathClassName.equals("char"))
 110   
       {
 111  8
          return Character.TYPE;
 112   
       }
 113  48
       if (fullPathClassName.equals("float"))
 114   
       {
 115  8
          return Float.TYPE;
 116   
       }
 117  40
       if (fullPathClassName.equals("double"))
 118   
       {
 119  8
          return Double.TYPE;
 120   
       }
 121  32
       if (fullPathClassName.equals("boolean"))
 122   
       {
 123  0
          return Boolean.TYPE;
 124   
       }
 125   
 
 126   
       // Try to load the class
 127  32
       Class c = null;
 128  32
       try
 129   
       {
 130  32
          c = Class.forName(fullPathClassName);
 131   
       }
 132   
       catch (Throwable e)
 133   
       {
 134   
          // Ignore it
 135   
       }
 136   
 
 137  32
       return c;
 138   
    }
 139   
 
 140   
 
 141   
    /**
 142   
     * Get the double value from a numeric class
 143   
     *
 144   
     * @param o  Object to transform
 145   
     * @return   The double value
 146   
     */
 147  32
    public static double getValue(Object o)
 148   
    {
 149  32
       if (isNumeric(o.getClass()))
 150   
       {
 151  24
          if (o instanceof Number)
 152   
          {
 153  24
             return ((Number) o).doubleValue();
 154   
          }
 155  0
          return (new Double(o.toString())).doubleValue();
 156   
       }
 157  8
       return 0;
 158   
    }
 159   
 
 160   
 
 161   
    /**
 162   
     * Return whether or not a class is numeric
 163   
     *
 164   
     * @param clazz  The class to test
 165   
     * @return       true if the class is numeric
 166   
     */
 167  136
    public static boolean isNumeric(Class clazz)
 168   
    {
 169  136
       return numericClasses.contains(clazz);
 170   
    }
 171   
 
 172   
    /** fill in with the numeric classes */
 173   
    static
 174   
    {
 175  8
       numericClasses = new ArrayList();
 176  8
       numericClasses.add(Byte.class);
 177  8
       numericClasses.add(Double.class);
 178  8
       numericClasses.add(Float.class);
 179  8
       numericClasses.add(Integer.class);
 180  8
       numericClasses.add(Long.class);
 181  8
       numericClasses.add(Short.class);
 182  8
       numericClasses.add(Byte.TYPE);
 183  8
       numericClasses.add(Double.TYPE);
 184  8
       numericClasses.add(Float.TYPE);
 185  8
       numericClasses.add(Integer.TYPE);
 186  8
       numericClasses.add(Long.TYPE);
 187  8
       numericClasses.add(Short.TYPE);
 188   
    }
 189   
 }
 190