1 23 package com.sun.enterprise.util; 24 25 import java.lang.reflect.*; 26 import java.util.Vector ; 27 import java.util.StringTokenizer ; 28 import java.util.Set ; 29 import java.util.HashSet ; 30 import java.util.Hashtable ; 31 import com.sun.enterprise.deployment.FieldDescriptor; 32 33 36 public class TypeUtil { 37 38 private static final char[] digits = { 40 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' 41 }; 42 43 private static Hashtable primitiveClasses_; 45 46 static { 47 primitiveClasses_ = new Hashtable (); 48 primitiveClasses_.put(Character.TYPE.getName(), Character.TYPE); 49 primitiveClasses_.put(Boolean.TYPE.getName(), Boolean.TYPE); 50 primitiveClasses_.put(Byte.TYPE.getName(), Byte.TYPE); 51 primitiveClasses_.put(Integer.TYPE.getName(), Integer.TYPE); 52 primitiveClasses_.put(Long.TYPE.getName(), Long.TYPE); 53 primitiveClasses_.put(Short.TYPE.getName(), Short.TYPE); 54 primitiveClasses_.put(Float.TYPE.getName(), Float.TYPE); 55 primitiveClasses_.put(Double.TYPE.getName(), Double.TYPE); 56 } 57 58 68 public static int intGetChars( 69 int src, 70 char buf[], 71 int offset 72 ) { 73 int power = 1000000000; int this_digit; 75 boolean have_emitted = false; 76 int init_offset = offset; 77 78 if (src == 0) { 80 buf[offset] = digits[0]; 81 return 1; 82 } 83 else if (src < 0) { 84 if (src == Integer.MIN_VALUE) 85 throw new IllegalArgumentException (); 86 87 buf[offset++] = '-'; 89 src = Math.abs(src); 90 } 91 92 while (power > 0) { 94 this_digit = src / power; 95 if (this_digit != 0 || have_emitted) { 96 have_emitted = true; 98 buf[offset++] = digits[this_digit]; 99 } 100 src = src % power; 101 power = power / 10; 102 } 103 return offset - init_offset; 104 } 105 106 107 private static final byte[] charval = { 109 (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5',(byte) '6',(byte) '7',(byte) '8',(byte) '9' 110 }; 111 112 121 public static int intGetBytes( 122 int src, 123 byte buf[], 124 int offset 125 ) { 126 int power = 1000000000; int this_digit; 128 boolean have_emitted = false; 129 int init_offset = offset; 130 131 if (src == 0) { 133 buf[offset] = charval[0]; 134 return 1; 135 } 136 else if (src < 0) { 137 if (src == Integer.MIN_VALUE) 138 throw new IllegalArgumentException (); 139 140 buf[offset++] = (byte) '-'; 142 src = Math.abs(src); 143 } 144 145 while (power > 0) { 147 this_digit = src / power; 148 if (this_digit != 0 || have_emitted) { 149 have_emitted = true; 151 buf[offset++] = charval[this_digit]; 152 } 153 src = src % power; 154 power = power / 10; 155 } 156 return offset - init_offset; 157 } 158 159 160 165 public static int hashCode(String s) 166 { 167 int length = s.length(); 168 int h = 1; 169 170 for (int i = 0; i < length; i++) 171 h = (h * 37) + (int) s.charAt(i); 172 return h; 173 } 174 175 176 180 public static String [] wordWrap(String msg, int widthInChars) { 181 int width = widthInChars; 182 int nextBreak =0; 183 int lastBreak = 0; 184 int length = msg.length(); 185 int lengthLeft = length; 186 boolean breakFound = true; 187 Vector v = new Vector (); 188 int nextNewline = msg.indexOf("\n"); 189 190 while (lengthLeft > width || nextNewline != -1) { 191 nextBreak = nextNewline; 194 195 if (nextBreak == -1 || 197 nextBreak <= lastBreak || 198 nextBreak > lastBreak + width) { 199 nextBreak = msg.lastIndexOf(" ", lastBreak + width); 200 } 201 202 if (nextBreak == -1 || nextBreak <= lastBreak) { 204 nextBreak = lastBreak + width - 1; 205 breakFound = false; 206 if (nextBreak > length) { 207 break; 208 } 209 } 210 211 String substr = msg.substring(lastBreak, nextBreak); 213 v.addElement(substr); 214 lengthLeft -= substr.length(); 215 216 lastBreak = nextBreak; 217 if (breakFound) { 218 ++lastBreak; 219 } 220 breakFound = true; 221 nextNewline = msg.indexOf("\n", lastBreak); 222 } 223 224 v.addElement(msg.substring(lastBreak)); 225 String [] lines = new String [v.size()]; 226 v.copyInto(lines); 227 return lines; 228 } 229 230 231 237 public static String arrayToString(String [] from, String separator) { 238 StringBuffer sb = new StringBuffer (100); 239 String sep = ""; 240 for (int i = 0; i < from.length; i++) { 241 sb.append(sep); 242 sb.append(from[i]); 243 sep = separator; 244 } 245 return sb.toString(); 246 } 247 248 249 255 public static String [] stringToArray(String from, String separator) { 256 if (from == null) { 257 return null; 258 } 259 if (separator == null) { 260 separator = " "; 261 } 262 StringTokenizer toks = new StringTokenizer (from, separator); 263 String [] result = new String [toks.countTokens()]; 264 int i = 0; 265 while (toks.hasMoreTokens()) { 266 result[i++] = toks.nextToken().trim(); 267 } 268 return result; 269 } 270 271 272 275 public static String truncateFloat(float f, int digits) { 276 double factor = Math.pow(10, digits); 277 f = (float)(Math.round(f * factor) / factor); 278 return Float.toString(f); 279 } 280 281 282 287 public static String addCommas(float f) { 288 String floatStr = truncateFloat(f, 0); 289 return addCommas(floatStr); 290 } 291 292 293 298 public static String addCommas(String numStr) { 299 int dotIndex = numStr.lastIndexOf('.'); 300 String n; 301 302 String fraction = ""; 303 if (dotIndex >= 0) { 304 fraction = numStr.substring(dotIndex); 305 n = numStr.substring(0, dotIndex); 306 } else { 307 n = numStr; 308 } 309 310 String val = ""; 311 int lastIndex = 0; 312 for (int i = n.length(); i > 0; i -= 3) { 313 String comma; 314 if (i > 3) { 315 comma = ","; 316 } else { 317 comma = ""; 318 } 319 int start = Math.max(i - 3, 0); 320 val = comma + n.substring(start, i) + val; 321 lastIndex = start; 322 } 323 val = n.substring(0, lastIndex) + val + fraction; 324 return val; 325 } 326 327 328 332 public static boolean isSubclassOf(Class sub, Class sup) { 333 if (sub == sup) { 334 return true; 335 } 336 Class superclass = sub.getSuperclass(); 337 while (superclass != null && superclass != sup) { 338 superclass = superclass.getSuperclass(); 339 } 340 return (superclass != null); 341 } 342 343 348 public static Set getSuperInterfaces(ClassLoader cl, String className, String baseClassName) throws ClassNotFoundException { 349 Set allSuper = new HashSet (); 350 if( !className.equals(baseClassName) ) { 351 Class theClass = cl.loadClass(className); 352 Class [] superInterfaces = theClass.getInterfaces(); 353 354 for(int superIndex = 0; superIndex < superInterfaces.length; superIndex++) { 355 Class currentClass = superInterfaces[superIndex]; 356 String currentClassName = currentClass.getName(); 357 if( !currentClassName.equals(baseClassName) ) { 358 allSuper.add(currentClassName); 359 allSuper.addAll(getSuperInterfaces(cl, currentClassName, baseClassName)); 360 } 361 } } 363 return allSuper; 364 } 365 366 public static Method getMethod(Class declaringClass, ClassLoader loader, 367 String name, String [] paramClassNames) 368 throws Exception 369 { 370 371 Class [] parameterTypes=null; 372 if (paramClassNames!=null) { 373 parameterTypes = new Class [paramClassNames.length]; 374 for(int pIndex = 0; pIndex < parameterTypes.length; pIndex++) { 375 String next = paramClassNames[pIndex]; 376 if( primitiveClasses_.containsKey(next) ) { 377 parameterTypes[pIndex] = 378 (Class ) primitiveClasses_.get(next); 379 } else { 380 parameterTypes[pIndex] = Class.forName(next, true, loader); 381 } 382 } 383 } 384 return declaringClass.getMethod(name, parameterTypes); 385 } 386 387 public static Method getDeclaredMethod(Class declaringClass, ClassLoader loader, 388 String name, String [] paramClassNames) 389 throws Exception 390 { 391 392 Class [] parameterTypes=null; 393 if (paramClassNames!=null) { 394 parameterTypes = new Class [paramClassNames.length]; 395 for(int pIndex = 0; pIndex < parameterTypes.length; pIndex++) { 396 String next = paramClassNames[pIndex]; 397 if( primitiveClasses_.containsKey(next) ) { 398 parameterTypes[pIndex] = 399 (Class ) primitiveClasses_.get(next); 400 } else { 401 parameterTypes[pIndex] = Class.forName(next, true, loader); 402 } 403 } 404 } 405 return declaringClass.getDeclaredMethod(name, parameterTypes); 406 } 407 408 417 public static boolean sameParamTypes(Method m1, Method m2) { 418 419 boolean same = false; 420 421 Class [] pm1 = m1.getParameterTypes(); 422 Class [] pm2 = m2.getParameterTypes(); 423 424 if( (pm1.length == pm2.length) ) { 425 same = true; 426 for(int i = 0; i < pm1.length; i++) { 427 if( pm1[i] != pm2[i] ) { 428 same = false; 429 break; 430 } 431 } 432 } 433 434 return same; 435 } 436 437 445 public static boolean sameMethodSignature(Method m1, Method m2) { 446 447 boolean same = false; 448 449 Class [] pm1 = m1.getParameterTypes(); 450 Class [] pm2 = m2.getParameterTypes(); 451 452 if( (m1.getName().equals(m2.getName())) && 453 (m1.getReturnType() == m2.getReturnType()) ) { 454 same = sameParamTypes(m1, m2); 455 } 456 457 return same; 458 } 459 460 public static Vector getPossibleCmpCmrFields(ClassLoader cl, 461 String className) 462 throws Exception { 463 464 Vector fieldDescriptors = new Vector (); 465 Class theClass = cl.loadClass(className); 466 467 Method[] methods = theClass.getMethods(); 469 470 for(int mIndex = 0; mIndex < methods.length; mIndex++) { 475 Method next = methods[mIndex]; 476 String nextName = next.getName(); 477 int nextModifiers = next.getModifiers(); 478 if( Modifier.isAbstract(nextModifiers) ) { 479 if( nextName.startsWith("get") && 480 nextName.length() > 3 ) { 481 String field = 482 nextName.substring(3,4).toLowerCase() + 483 nextName.substring(4); 484 fieldDescriptors.add(new FieldDescriptor(field)); 485 } 486 } 487 } 488 return fieldDescriptors; 489 } 490 491 494 public static String setterMethodToPropertyName(String setterMethodName) { 495 496 if( (setterMethodName == null) || 497 (setterMethodName.length() <= 3) || 498 !setterMethodName.startsWith("set") ) { 499 throw new IllegalArgumentException ("Invalid setter method name " + 500 setterMethodName); 501 } 502 503 return ( setterMethodName.substring(3, 4).toLowerCase() + 504 setterMethodName.substring(4) ); 505 506 } 507 508 511 public static String propertyNameToSetterMethod(String propertyName) { 512 513 if( (propertyName == null) || 514 (propertyName.length() == 0) ) { 515 throw new IllegalArgumentException ("Invalid property name " + 516 propertyName); 517 } 518 519 return ( "set" + propertyName.substring(0, 1).toUpperCase() + 520 propertyName.substring(1) ); 521 522 } 523 524 } 525 | Popular Tags |