KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > lib > JavaType


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JavaType.java,v 1.11 2004/09/17 08:25:02 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26
27 package org.objectweb.jonas_ejb.lib;
28
29 import java.util.Hashtable JavaDoc;
30
31 /**
32  * This class implements methods which permit to get informations about Java Types:
33  * <ul>
34  * <li>
35  * the JDBC Types associated
36  * <li>
37  * the ResultSet.getXXX() methods associated
38  * <li>
39  * the PreparedStatement.setXXX() methods associated
40  * </ul>
41  * <br>
42  * (The Java Type may be or not a primitive type)
43  *
44  * @author Helene Joanin : Initial developer
45  * @author ico.Hoogervorst@nl.compuware.com : Support for BigInteger and avoid cast exceptions with rs.getObject()
46  */

47 public class JavaType {
48
49     // Default values for Java primitive type
50
private static Hashtable JavaDoc tableDefaultValues = new Hashtable JavaDoc();
51     static {
52         tableDefaultValues.put(java.lang.Boolean.TYPE, "false");
53         tableDefaultValues.put(java.lang.Character.TYPE, "'\u0000'");
54         tableDefaultValues.put(java.lang.Byte.TYPE, "(byte)0");
55         tableDefaultValues.put(java.lang.Short.TYPE, "(short)0");
56         tableDefaultValues.put(java.lang.Integer.TYPE, "0");
57         tableDefaultValues.put(java.lang.Long.TYPE, "0L");
58         tableDefaultValues.put(java.lang.Float.TYPE, "0.0f");
59         tableDefaultValues.put(java.lang.Double.TYPE, "0.0d");
60     }
61
62     /**
63      * Returns true if the given type is the type 'void'
64      */

65     public static boolean isVoid(Class JavaDoc c) {
66         return (c.equals(java.lang.Void.TYPE));
67     }
68
69     /**
70      * Returns the name of the given type
71      */

72     public static String JavaDoc getName(Class JavaDoc c) {
73         String JavaDoc name;
74         if (c.isArray()) {
75             name = getName(c.getComponentType()) + "[]";
76         } else {
77             // The '$' in the name of a inner class is no more allowed since JDK 1.4.2
78
name = c.getName().replace('$', '.');
79         }
80         return (name);
81     }
82
83     /**
84      * Returns true if the given class is a Serializable Java Type,
85      * false otherwise.
86      */

87     public static boolean isSerializable(Class JavaDoc c) {
88         boolean isSerializable = false;
89         if (c.isArray()) {
90             isSerializable = isSerializable(c.getComponentType());
91         } else if (c.isPrimitive()) {
92             isSerializable = true;
93         } else {
94             isSerializable = java.io.Serializable JavaDoc.class.isAssignableFrom(c);
95         }
96         return (isSerializable);
97     }
98
99     /**
100      * Returns true if the given class is a valid type for RMI,
101      * false otherwise.
102      * Valid types for RMI are primitive types, remote objects,
103      * and non-remote objects that implement the java.io.Serializable interface
104      */

105     public static boolean isValidForRmi(Class JavaDoc c) {
106         return (JavaType.isSerializable(c) || java.rmi.Remote JavaDoc.class.isAssignableFrom(c));
107     }
108
109     /**
110      * Returns true if the given class implements java.uti.Collection or java.util.Enumeration
111      * false otherwise.
112      */

113     public static boolean isCollecOrEnum(Class JavaDoc c) {
114         return (java.util.Collection JavaDoc.class.isAssignableFrom(c) || java.util.Enumeration JavaDoc.class.isAssignableFrom(c));
115     }
116
117     /**
118      * Returns the name of the getXXX method associated with the given type in
119      * java.sql.ResultSet.
120      * @param c the class object for a Java type.
121      * @return a string representing the name of the get method, null in error case
122      */

123     public static String JavaDoc getSQLGetMethod(Class JavaDoc c) {
124         return (JavaTypesForSQL.getSQLGetMethod(c));
125     }
126
127     /**
128      * Returns the name of the setXXX method associated with the given type in
129      * java.sql.ResultSet.
130      * @param c the class object for a Java type.
131      * @return a string representing the name of the set method, null in error case
132      */

133     public static String JavaDoc getSQLSetMethod(Class JavaDoc c) {
134         return (JavaTypesForSQL.getSQLSetMethod(c));
135     }
136
137     /**
138      * Returns true if the given method name is getObject() or setObject()
139      */

140     public static boolean isXxxObjectMethod(String JavaDoc name) {
141         return (name.equals("getObject") || name.equals("setObject"));
142     }
143
144     /**
145      * Returns the SQL Type mapping the given Java Type.
146      * @param c the class object for a Java type.
147      * @return the SQL Type mapping the given Java Type, (Types.OTHER in error case)
148      */

149     public static String JavaDoc getSQLType(Class JavaDoc c) {
150         return (JavaTypesForSQL.getSQLType(c));
151     }
152
153     /**
154      * Returns the default value of the given Java Type.
155      * @param c the class object for a Java type.
156      * @return the default value of the given Java Type
157      */

158     public static String JavaDoc getDefaultValue(Class JavaDoc c) {
159         String JavaDoc val = null;
160         if (c.isPrimitive()) {
161             val = (String JavaDoc) tableDefaultValues.get(c);
162         } else {
163             val = new String JavaDoc("null");
164         }
165         if (val == null) {
166             throw new Error JavaDoc("JavaType ERROR: No default value for the class " + c.getName());
167         }
168         return (val);
169     }
170
171     public static Boolean JavaDoc toObject(boolean val) {
172         return new Boolean JavaDoc(val);
173     }
174     public static Byte JavaDoc toObject(byte val) {
175         return new Byte JavaDoc(val);
176     }
177     public static Short JavaDoc toObject(short val) {
178         return new Short JavaDoc(val);
179     }
180     public static Integer JavaDoc toObject(int val) {
181         return new Integer JavaDoc(val);
182     }
183     public static Long JavaDoc toObject(long val) {
184         return new Long JavaDoc(val);
185     }
186     public static Float JavaDoc toObject(float val) {
187         return new Float JavaDoc(val);
188     }
189     public static Double JavaDoc toObject(double val) {
190         return new Double JavaDoc(val);
191     }
192     public static Character JavaDoc toObject(char val) {
193         return new Character JavaDoc(val);
194     }
195     public static Object JavaDoc toObject(Object JavaDoc val) {
196         return val;
197     }
198
199     /**
200      * If it is a primitive type, return a new Object constructor,
201      * else return the same object.
202      * There are nine predefined Class objects to represent the eight
203      * primitive types and void. These are created by the Java Virtual Machine,
204      * and have the same names as the primitive types that they represent,
205      * namely boolean, byte, char, short, int, long, float, and double.
206      * @param name name of the var
207      * @param val the object value
208      * @return new object
209      */

210     public static String JavaDoc toStringObject(String JavaDoc name, Class JavaDoc c) {
211         if (c.isPrimitive()) {
212             if (c == Boolean.TYPE) {
213                 return "Boolean.valueOf(" + name + ")";
214             } else if (c == Byte.TYPE) {
215                 return "new Byte(" + name + ")";
216             } else if (c == Character.TYPE) {
217                 return "new Character(" + name + ")";
218             } else if (c == Short.TYPE) {
219                 return "new Short(" + name + ")";
220             } else if (c == Integer.TYPE) {
221                 return "new Integer(" + name + ")";
222             } else if (c == Long.TYPE) {
223                 return "new Long(" + name + ")";
224             } else if (c == Float.TYPE) {
225                 return "new Float(" + name + ")";
226             } else if (c == Double.TYPE) {
227                 return "new Double(" + name + ")";
228             } else {
229                 return name;
230             }
231         } else {
232             return name;
233         }
234     }
235     
236
237 }
238
239
240 /**
241  * This inner class allows to calculate, for a given Java Type:
242  * - the corresponding SQL type,
243  * - the corresponding set method of the java.sql.PreparedStatement,
244  * - the corresponding get method of the java.sql.ResultSet.
245  */

246 class JavaTypesForSQL {
247
248     private static JavaTypesForSQL mTypesForSQL = new JavaTypesForSQL();
249     private static Hashtable JavaDoc mGets;
250     private static Hashtable JavaDoc mSets;
251     private static Hashtable JavaDoc mSQLTypes;
252
253     private JavaTypesForSQL() {
254
255         mGets = new Hashtable JavaDoc();
256         mSets = new Hashtable JavaDoc();
257         mSQLTypes = new Hashtable JavaDoc();
258
259         mGets.put(java.lang.Boolean.TYPE, "getBoolean");
260         mSets.put(java.lang.Boolean.TYPE, "setBoolean");
261         mSQLTypes.put(java.lang.Boolean.TYPE, "Types.BIT");
262         mGets.put(java.lang.Byte.TYPE, "getByte");
263         mSets.put(java.lang.Byte.TYPE, "setByte");
264         mSQLTypes.put(java.lang.Byte.TYPE, "Types.TINYINT");
265         mGets.put(java.lang.Short.TYPE, "getShort");
266         mSets.put(java.lang.Short.TYPE, "setShort");
267         mSQLTypes.put(java.lang.Short.TYPE, "Types.SMALLINT");
268         mGets.put(java.lang.Integer.TYPE, "getInt");
269         mSets.put(java.lang.Integer.TYPE, "setInt");
270         mSQLTypes.put(java.lang.Integer.TYPE, "Types.INTEGER");
271         mGets.put(java.lang.Long.TYPE, "getLong");
272         mSets.put(java.lang.Long.TYPE, "setLong");
273         mSQLTypes.put(java.lang.Long.TYPE, "Types.BIGINT");
274         mGets.put(java.lang.Float.TYPE, "getFloat");
275         mSets.put(java.lang.Float.TYPE, "setFloat");
276         mSQLTypes.put(java.lang.Float.TYPE, "Types.FLOAT");
277         mGets.put(java.lang.Double.TYPE, "getDouble");
278         mSets.put(java.lang.Double.TYPE, "setDouble");
279         mSQLTypes.put(java.lang.Double.TYPE, "Types.DOUBLE");
280         //mGets.put(java.lang.Character.TYPE, "????");
281
//mSets.put(java.lang.Character.TYPE, "????");
282
//mSQLTypes.put(java.lang.Character.TYPE, "???");
283
mGets.put(java.lang.String JavaDoc.class, "getString");
284         mSets.put(java.lang.String JavaDoc.class, "setString");
285         mSQLTypes.put(java.lang.String JavaDoc.class, "Types.VARCHAR");
286         mGets.put(java.sql.Date JavaDoc.class, "getDate");
287         mSets.put(java.sql.Date JavaDoc.class, "setDate");
288         mSQLTypes.put(java.sql.Date JavaDoc.class, "Types.DATE");
289         mGets.put(java.sql.Time JavaDoc.class, "getTime");
290         mSets.put(java.sql.Time JavaDoc.class, "setTime");
291         mSQLTypes.put(java.sql.Time JavaDoc.class, "Types.TIME");
292         mGets.put(java.sql.Timestamp JavaDoc.class, "getTimestamp");
293         mSets.put(java.sql.Timestamp JavaDoc.class, "setTimestamp");
294         mSQLTypes.put(java.sql.Timestamp JavaDoc.class, "Types.TIMESTAMP");
295         mGets.put(java.lang.Boolean JavaDoc.class, "getBoolean");
296         mSets.put(java.lang.Boolean JavaDoc.class, "setObject");
297         mSQLTypes.put(java.lang.Boolean JavaDoc.class, "Types.BIT");
298         mGets.put(java.lang.Integer JavaDoc.class, "getInt");
299         mSets.put(java.lang.Integer JavaDoc.class, "setObject");
300         mSQLTypes.put(java.lang.Integer JavaDoc.class, "Types.INTEGER");
301         mGets.put(java.lang.Long JavaDoc.class, "getLong");
302         mSets.put(java.lang.Long JavaDoc.class, "setObject");
303         mSQLTypes.put(java.lang.Long JavaDoc.class, "Types.BIGINT");
304         mGets.put(java.lang.Float JavaDoc.class, "getFloat");
305         mSets.put(java.lang.Float JavaDoc.class, "setObject");
306         mSQLTypes.put(java.lang.Float JavaDoc.class, "Types.REAL");
307         mGets.put(java.lang.Double JavaDoc.class, "getDouble");
308         mSets.put(java.lang.Double JavaDoc.class, "setObject");
309         mSQLTypes.put(java.lang.Double JavaDoc.class, "Types.DOUBLE");
310         mGets.put(java.lang.Byte JavaDoc.class, "getByte");
311         mSets.put(java.lang.Byte JavaDoc.class, "setObject");
312         mSQLTypes.put(java.lang.Byte JavaDoc.class, "Types.TINYINT");
313         mGets.put(java.lang.Short JavaDoc.class, "getShort");
314         mSets.put(java.lang.Short JavaDoc.class, "setObject");
315         mSQLTypes.put(java.lang.Short JavaDoc.class, "Types.SMALLINT");
316         mGets.put(java.math.BigDecimal JavaDoc.class, "getBigDecimal");
317         mSets.put(java.math.BigDecimal JavaDoc.class, "setObject");
318         mSQLTypes.put(java.math.BigDecimal JavaDoc.class, "Types.NUMERIC");
319         mGets.put(java.math.BigInteger JavaDoc.class, "getBigDecimal");
320         mSets.put(java.math.BigInteger JavaDoc.class, "setObject");
321         mSQLTypes.put(java.math.BigInteger JavaDoc.class, "Types.NUMERIC");
322     }
323
324     /**
325      * Returns the name of the getXXX method associated with the given type
326      * in java.sql.ResultSet.
327      * @param c the class object for a Java type.
328      * @return a string representing the name of the get method, null in error case
329      */

330     static String JavaDoc getSQLGetMethod(Class JavaDoc c) {
331         String JavaDoc val = null;
332         val = (String JavaDoc) mGets.get(c);
333         if (val == null) {
334             if (c.isArray()) {
335                 // See if c is byte[] or serializable[]
336
Class JavaDoc component = c.getComponentType();
337                 if (component != null) {
338                     if (component.equals(java.lang.Byte.TYPE)) {
339                         val = new String JavaDoc("getBytes");
340                     } else if (JavaType.isSerializable(component)) {
341                         val = new String JavaDoc("getSerializable");
342                     }
343                 }
344             } else if (JavaType.isSerializable(c)) {
345                 // See if c is serializable
346
val = new String JavaDoc("getSerializable");
347             }
348         }
349         return (val);
350     }
351
352     /**
353      * Returns the name of the setXXX method associated with the given type
354      * in java.sql.PreparedStatement.
355      * @param c the class object for a Java type.
356      * @return a string representing the name of the set method, null in error case
357      */

358     static String JavaDoc getSQLSetMethod(Class JavaDoc c) {
359         String JavaDoc val = null;
360         val = (String JavaDoc) mSets.get(c);
361         if (val == null) {
362             if (c.isArray()) {
363                 // See if c is byte[] or serializable[]
364
Class JavaDoc component = c.getComponentType();
365                 if (component != null) {
366                     if (component.equals(java.lang.Byte.TYPE)) {
367                         val = new String JavaDoc("setBytes");
368                     } else if (JavaType.isSerializable(component)) {
369                         val = new String JavaDoc("setSerializable");
370                     }
371                 }
372             } else if (JavaType.isSerializable(c)) {
373                 // See if c is serializable
374
val = new String JavaDoc("setSerializable");
375             }
376         }
377         return (val);
378     }
379
380     /**
381      * Returns the SQL Type mapping the given Java Type.
382      * @param c the class object for a Java type.
383      * @return the SQL Type mapping the given Java Type, (Types.OTHER in error case)
384      */

385     static String JavaDoc getSQLType(Class JavaDoc c) {
386         String JavaDoc val = null;
387         val = (String JavaDoc) mSQLTypes.get(c);
388         if (val == null) {
389             val = new String JavaDoc("Types.OTHER");
390             if (c.isArray()) {
391                 // See if c is byte[] or serializable[]
392
Class JavaDoc component = c.getComponentType();
393                 if (component != null) {
394                     if (component.equals(java.lang.Byte.TYPE)) {
395                         val = new String JavaDoc("Types.VARBINARY");
396                     } else if (JavaType.isSerializable(component)) {
397                         val = new String JavaDoc("Types.VARBINARY");
398                     }
399                 }
400             } else if (JavaType.isSerializable(c)) {
401                 // See if c is serializable
402
val = new String JavaDoc("Types.VARBINARY");
403             }
404         }
405         return (val);
406     }
407
408 }
409
410
411
412
Popular Tags