KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > util > ClassTools


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 JavaDoc;
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 JavaDoc 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    public static String JavaDoc classDisplay(String JavaDoc className)
31    {
32       String JavaDoc result = className;
33
34       if (className == null)
35       {
36          return null;
37       }
38
39       if (className.startsWith("[Z"))
40       {
41          result = "boolean[]";
42       }
43       if (className.startsWith("[C"))
44       {
45          result = "char[]";
46       }
47       if (className.startsWith("[D"))
48       {
49          result = "double[]";
50       }
51       if (className.startsWith("[F"))
52       {
53          result = "float[]";
54       }
55       if (className.startsWith("[I"))
56       {
57          result = "int[]";
58       }
59       if (className.startsWith("[S"))
60       {
61          result = "short[]";
62       }
63       if (className.startsWith("[J"))
64       {
65          result = "long[]";
66       }
67       if (className.startsWith("[B"))
68       {
69          result = "byte[]";
70       }
71       if (className.startsWith("[L"))
72       {
73          result = className.substring(2, className.length() - 1) + "[]";
74       }
75
76       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    public static Class JavaDoc getClass(String JavaDoc fullPathClassName)
87    {
88       // Check if the class is a primitive type
89
if (fullPathClassName.equals("void"))
90       {
91          return Void.TYPE;
92       }
93       if (fullPathClassName.equals("int"))
94       {
95          return Integer.TYPE;
96       }
97       if (fullPathClassName.equals("short"))
98       {
99          return Short.TYPE;
100       }
101       if (fullPathClassName.equals("long"))
102       {
103          return Long.TYPE;
104       }
105       if (fullPathClassName.equals("byte"))
106       {
107          return Byte.TYPE;
108       }
109       if (fullPathClassName.equals("char"))
110       {
111          return Character.TYPE;
112       }
113       if (fullPathClassName.equals("float"))
114       {
115          return Float.TYPE;
116       }
117       if (fullPathClassName.equals("double"))
118       {
119          return Double.TYPE;
120       }
121       if (fullPathClassName.equals("boolean"))
122       {
123          return Boolean.TYPE;
124       }
125
126       // Try to load the class
127
Class JavaDoc c = null;
128       try
129       {
130          c = Class.forName(fullPathClassName);
131       }
132       catch (Throwable JavaDoc e)
133       {
134          // Ignore it
135
}
136
137       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    public static double getValue(Object JavaDoc o)
148    {
149       if (isNumeric(o.getClass()))
150       {
151          if (o instanceof Number JavaDoc)
152          {
153             return ((Number JavaDoc) o).doubleValue();
154          }
155          return (new Double JavaDoc(o.toString())).doubleValue();
156       }
157       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    public static boolean isNumeric(Class JavaDoc clazz)
168    {
169       return numericClasses.contains(clazz);
170    }
171
172    /** fill in with the numeric classes */
173    static
174    {
175       numericClasses = new ArrayList JavaDoc();
176       numericClasses.add(Byte JavaDoc.class);
177       numericClasses.add(Double JavaDoc.class);
178       numericClasses.add(Float JavaDoc.class);
179       numericClasses.add(Integer JavaDoc.class);
180       numericClasses.add(Long JavaDoc.class);
181       numericClasses.add(Short JavaDoc.class);
182       numericClasses.add(Byte.TYPE);
183       numericClasses.add(Double.TYPE);
184       numericClasses.add(Float.TYPE);
185       numericClasses.add(Integer.TYPE);
186       numericClasses.add(Long.TYPE);
187       numericClasses.add(Short.TYPE);
188    }
189 }
190
Popular Tags