1 25 26 27 package org.objectweb.jonas_ejb.lib; 28 29 import java.util.Hashtable ; 30 31 47 public class JavaType { 48 49 private static Hashtable tableDefaultValues = new Hashtable (); 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 65 public static boolean isVoid(Class c) { 66 return (c.equals(java.lang.Void.TYPE)); 67 } 68 69 72 public static String getName(Class c) { 73 String name; 74 if (c.isArray()) { 75 name = getName(c.getComponentType()) + "[]"; 76 } else { 77 name = c.getName().replace('$', '.'); 79 } 80 return (name); 81 } 82 83 87 public static boolean isSerializable(Class 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 .class.isAssignableFrom(c); 95 } 96 return (isSerializable); 97 } 98 99 105 public static boolean isValidForRmi(Class c) { 106 return (JavaType.isSerializable(c) || java.rmi.Remote .class.isAssignableFrom(c)); 107 } 108 109 113 public static boolean isCollecOrEnum(Class c) { 114 return (java.util.Collection .class.isAssignableFrom(c) || java.util.Enumeration .class.isAssignableFrom(c)); 115 } 116 117 123 public static String getSQLGetMethod(Class c) { 124 return (JavaTypesForSQL.getSQLGetMethod(c)); 125 } 126 127 133 public static String getSQLSetMethod(Class c) { 134 return (JavaTypesForSQL.getSQLSetMethod(c)); 135 } 136 137 140 public static boolean isXxxObjectMethod(String name) { 141 return (name.equals("getObject") || name.equals("setObject")); 142 } 143 144 149 public static String getSQLType(Class c) { 150 return (JavaTypesForSQL.getSQLType(c)); 151 } 152 153 158 public static String getDefaultValue(Class c) { 159 String val = null; 160 if (c.isPrimitive()) { 161 val = (String ) tableDefaultValues.get(c); 162 } else { 163 val = new String ("null"); 164 } 165 if (val == null) { 166 throw new Error ("JavaType ERROR: No default value for the class " + c.getName()); 167 } 168 return (val); 169 } 170 171 public static Boolean toObject(boolean val) { 172 return new Boolean (val); 173 } 174 public static Byte toObject(byte val) { 175 return new Byte (val); 176 } 177 public static Short toObject(short val) { 178 return new Short (val); 179 } 180 public static Integer toObject(int val) { 181 return new Integer (val); 182 } 183 public static Long toObject(long val) { 184 return new Long (val); 185 } 186 public static Float toObject(float val) { 187 return new Float (val); 188 } 189 public static Double toObject(double val) { 190 return new Double (val); 191 } 192 public static Character toObject(char val) { 193 return new Character (val); 194 } 195 public static Object toObject(Object val) { 196 return val; 197 } 198 199 210 public static String toStringObject(String name, Class 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 246 class JavaTypesForSQL { 247 248 private static JavaTypesForSQL mTypesForSQL = new JavaTypesForSQL(); 249 private static Hashtable mGets; 250 private static Hashtable mSets; 251 private static Hashtable mSQLTypes; 252 253 private JavaTypesForSQL() { 254 255 mGets = new Hashtable (); 256 mSets = new Hashtable (); 257 mSQLTypes = new Hashtable (); 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.String .class, "getString"); 284 mSets.put(java.lang.String .class, "setString"); 285 mSQLTypes.put(java.lang.String .class, "Types.VARCHAR"); 286 mGets.put(java.sql.Date .class, "getDate"); 287 mSets.put(java.sql.Date .class, "setDate"); 288 mSQLTypes.put(java.sql.Date .class, "Types.DATE"); 289 mGets.put(java.sql.Time .class, "getTime"); 290 mSets.put(java.sql.Time .class, "setTime"); 291 mSQLTypes.put(java.sql.Time .class, "Types.TIME"); 292 mGets.put(java.sql.Timestamp .class, "getTimestamp"); 293 mSets.put(java.sql.Timestamp .class, "setTimestamp"); 294 mSQLTypes.put(java.sql.Timestamp .class, "Types.TIMESTAMP"); 295 mGets.put(java.lang.Boolean .class, "getBoolean"); 296 mSets.put(java.lang.Boolean .class, "setObject"); 297 mSQLTypes.put(java.lang.Boolean .class, "Types.BIT"); 298 mGets.put(java.lang.Integer .class, "getInt"); 299 mSets.put(java.lang.Integer .class, "setObject"); 300 mSQLTypes.put(java.lang.Integer .class, "Types.INTEGER"); 301 mGets.put(java.lang.Long .class, "getLong"); 302 mSets.put(java.lang.Long .class, "setObject"); 303 mSQLTypes.put(java.lang.Long .class, "Types.BIGINT"); 304 mGets.put(java.lang.Float .class, "getFloat"); 305 mSets.put(java.lang.Float .class, "setObject"); 306 mSQLTypes.put(java.lang.Float .class, "Types.REAL"); 307 mGets.put(java.lang.Double .class, "getDouble"); 308 mSets.put(java.lang.Double .class, "setObject"); 309 mSQLTypes.put(java.lang.Double .class, "Types.DOUBLE"); 310 mGets.put(java.lang.Byte .class, "getByte"); 311 mSets.put(java.lang.Byte .class, "setObject"); 312 mSQLTypes.put(java.lang.Byte .class, "Types.TINYINT"); 313 mGets.put(java.lang.Short .class, "getShort"); 314 mSets.put(java.lang.Short .class, "setObject"); 315 mSQLTypes.put(java.lang.Short .class, "Types.SMALLINT"); 316 mGets.put(java.math.BigDecimal .class, "getBigDecimal"); 317 mSets.put(java.math.BigDecimal .class, "setObject"); 318 mSQLTypes.put(java.math.BigDecimal .class, "Types.NUMERIC"); 319 mGets.put(java.math.BigInteger .class, "getBigDecimal"); 320 mSets.put(java.math.BigInteger .class, "setObject"); 321 mSQLTypes.put(java.math.BigInteger .class, "Types.NUMERIC"); 322 } 323 324 330 static String getSQLGetMethod(Class c) { 331 String val = null; 332 val = (String ) mGets.get(c); 333 if (val == null) { 334 if (c.isArray()) { 335 Class component = c.getComponentType(); 337 if (component != null) { 338 if (component.equals(java.lang.Byte.TYPE)) { 339 val = new String ("getBytes"); 340 } else if (JavaType.isSerializable(component)) { 341 val = new String ("getSerializable"); 342 } 343 } 344 } else if (JavaType.isSerializable(c)) { 345 val = new String ("getSerializable"); 347 } 348 } 349 return (val); 350 } 351 352 358 static String getSQLSetMethod(Class c) { 359 String val = null; 360 val = (String ) mSets.get(c); 361 if (val == null) { 362 if (c.isArray()) { 363 Class component = c.getComponentType(); 365 if (component != null) { 366 if (component.equals(java.lang.Byte.TYPE)) { 367 val = new String ("setBytes"); 368 } else if (JavaType.isSerializable(component)) { 369 val = new String ("setSerializable"); 370 } 371 } 372 } else if (JavaType.isSerializable(c)) { 373 val = new String ("setSerializable"); 375 } 376 } 377 return (val); 378 } 379 380 385 static String getSQLType(Class c) { 386 String val = null; 387 val = (String ) mSQLTypes.get(c); 388 if (val == null) { 389 val = new String ("Types.OTHER"); 390 if (c.isArray()) { 391 Class component = c.getComponentType(); 393 if (component != null) { 394 if (component.equals(java.lang.Byte.TYPE)) { 395 val = new String ("Types.VARBINARY"); 396 } else if (JavaType.isSerializable(component)) { 397 val = new String ("Types.VARBINARY"); 398 } 399 } 400 } else if (JavaType.isSerializable(c)) { 401 val = new String ("Types.VARBINARY"); 403 } 404 } 405 return (val); 406 } 407 408 } 409 410 411 412 | Popular Tags |