1 31 package org.objectweb.proactive.core.mop; 32 33 import java.lang.reflect.Field ; 34 import java.lang.reflect.Method ; 35 import java.lang.reflect.Modifier ; 36 37 40 41 public abstract class Utils extends Object { 42 43 46 public static final Class JAVA_LANG_NUMBER = silentForName("java.lang.Number"); 47 public static final Class JAVA_LANG_CHARACTER = silentForName("java.lang.Character"); 48 public static final Class JAVA_LANG_BOOLEAN = silentForName("java.lang.Boolean"); 49 public static final Class JAVA_LANG_VOID = silentForName("java.lang.Void"); 50 public static final Class JAVA_LANG_RUNTIMEEXCEPTION = silentForName("java.lang.RuntimeException"); 51 public static final Class JAVA_LANG_EXCEPTION = silentForName("java.lang.Exception"); 52 public static final Class JAVA_LANG_THROWABLE = silentForName("java.lang.Throwable"); 53 public static final String STUB_DEFAULT_PREFIX = "Stub_"; 54 public static final String STUB_DEFAULT_PACKAGE = "pa.stub."; 55 56 59 60 68 static public String getRidOfNative(String in) { 69 String result; 70 int leftindex, rightindex; 71 72 leftindex = in.indexOf("native"); 73 if (leftindex == -1) 74 return in; 75 rightindex = leftindex + 6; 76 77 result = in.substring(0, leftindex) + in.substring(rightindex, in.length()); 78 return result; 79 } 80 81 static public String getRidOfAbstract(String in) { 82 String result; 83 int leftindex, rightindex; 84 85 leftindex = in.indexOf("abstract"); 86 if (leftindex == -1) 87 return in; 88 rightindex = leftindex + 8; 89 90 result = in.substring(0, leftindex) + in.substring(rightindex, in.length()); 91 return result; 92 } 93 94 static public String getRidOfNativeAndAbstract(String in) { 95 String s = in; 96 s = getRidOfAbstract(s); 97 return getRidOfNative(s); 98 } 99 100 113 114 static public boolean checkMethod(Method met) { 115 int modifiers = met.getModifiers(); 116 117 if (Modifier.isFinal(modifiers)) 120 return false; 121 if (Modifier.isStatic(modifiers)) 123 return false; 124 if (!(Modifier.isPublic(modifiers))) 125 return false; 126 if ((met.getName().equals("finalize")) && (met.getParameterTypes().length == 0)) 128 return false; 129 return true; 130 } 131 132 144 145 static public String sourceLikeForm(Class cl) { 146 if (!(cl.isArray())) { 147 151 return cl.getName().replace('$', '.'); 152 } else { 153 int nb = 0; 154 Class current = cl; 155 String result = ""; 156 157 do { 158 current = current.getComponentType(); 159 result = "[]" + result; 160 nb++; 161 } while ((current.getComponentType()) != null); 162 163 result = current.getName() + result; 164 return result; 165 } 166 } 167 168 172 173 static public String nameOfWrapper(Class cl) { 174 String str = cl.getName(); 175 176 if (cl.isPrimitive()) { 177 if (str.equals("int")) 178 return "java.lang.Integer"; 179 else if (str.equals("boolean")) 180 return "java.lang.Boolean"; 181 else if (str.equals("byte")) 182 return "java.lang.Byte"; 183 else if (str.equals("short")) 184 return "java.lang.Short"; 185 else if (str.equals("long")) 186 return "java.lang.Long"; 187 else if (str.equals("float")) 188 return "java.lang.Float"; 189 else if (str.equals("double")) 190 return "java.lang.Double"; 191 else if (str.equals("void")) 192 return "void"; 193 else if (str.equals("char")) 194 return "java.lang.Character"; 195 else { 196 throw new InternalException("Unknown primitive type: " + cl.getName()); 197 } 198 } else { 199 return null; 200 } 201 } 202 203 207 208 public static String getPackageName(String fqnameofclass) { 209 int indexoflastdot; 210 211 indexoflastdot = fqnameofclass.lastIndexOf('.'); 212 213 if (indexoflastdot == -1) { 214 return ""; 215 } else { 216 return fqnameofclass.substring(0, indexoflastdot); 217 } 218 219 } 220 221 224 225 public static String getSimpleName(String fullyQualifiedNameOfClass) { 226 int indexOfLastDot = fullyQualifiedNameOfClass.lastIndexOf('.'); 227 if (indexOfLastDot == -1) { 229 return fullyQualifiedNameOfClass; 230 } else { 231 if (indexOfLastDot == (fullyQualifiedNameOfClass.length() - 1)) 233 return ""; 234 else 235 return fullyQualifiedNameOfClass.substring(indexOfLastDot + 1); 236 } 237 } 238 239 243 public static Class getWrapperClass(Class cl) { 244 if (!(cl.isPrimitive())) 245 return null; 246 String s = Utils.nameOfWrapper(cl); 247 try { 248 return MOP.forName(s); 249 } catch (ClassNotFoundException e) { 250 throw new InternalException("Cannot load wrapper class " + s); 251 } 252 } 253 254 257 public static Class getPrimitiveType(Class cl) { 258 Field cst; 259 if (Utils.isWrapperClass(cl)) { 260 try { 263 cst = cl.getField("TYPE"); 264 return (Class ) cst.get(null); 265 } catch (NoSuchFieldException e) { 266 throw new InternalException("Cannot locate constant TYPE in class " + cl.getName()); 267 } catch (SecurityException e) { 268 throw new InternalException("Access to field TYPE in class " + cl.getName() + " denied"); 269 } catch (IllegalAccessException e) { 270 throw new InternalException("Access to field TYPE in class " + cl.getName() + " denied"); 271 } 272 } else { 273 throw new InternalException("Not a wrapper class: " + cl.getName()); 274 } 275 } 276 277 281 282 public static boolean isWrapperClass(Class cl) { 283 if (Utils.JAVA_LANG_NUMBER.isAssignableFrom(cl)) 284 return true; 285 else if (Utils.JAVA_LANG_BOOLEAN.isAssignableFrom(cl)) 286 return true; 287 else if (Utils.JAVA_LANG_CHARACTER.isAssignableFrom(cl)) 288 return true; 289 else if (Utils.JAVA_LANG_VOID.isAssignableFrom(cl)) 290 return true; 291 else 292 return false; 293 } 294 295 public static String getRelativePath(String className) { 296 String packageName; 297 String fileSeparator; 298 String result; 299 int indexOfDot, indexOfLastDot; 300 301 fileSeparator = System.getProperty("file.separator"); 302 packageName = Utils.getPackageName(className); 303 304 indexOfDot = packageName.indexOf((int) '.', 0); 305 result = ""; 306 indexOfLastDot = 0; 307 308 while (indexOfDot != -1) { 309 310 result = result + fileSeparator + packageName.substring(indexOfLastDot, indexOfDot); 311 indexOfLastDot = indexOfDot + 1; 312 indexOfDot = packageName.indexOf((int) '.', indexOfDot + 1); 313 if (indexOfDot == -1) 314 result = result + fileSeparator + packageName.substring(indexOfLastDot, packageName.length()); 315 } 316 317 if (result.equals("")) 318 result = fileSeparator + packageName; 319 320 return result; 321 } 322 323 328 329 public static boolean isNormalException(Class exc) { 330 boolean result; 331 332 if (Utils.JAVA_LANG_THROWABLE.isAssignableFrom(exc)) { 333 if (Utils.JAVA_LANG_EXCEPTION.isAssignableFrom(exc)) { 335 if (Utils.JAVA_LANG_RUNTIMEEXCEPTION.isAssignableFrom(exc)) 336 result = false; 337 else 338 result = true; 339 } else 340 result = false; 342 } else 343 result = false; 344 345 return result; 346 } 347 348 public static Class decipherPrimitiveType(String str) { 349 if (str.equals("int")) 350 return java.lang.Integer.TYPE; 351 else if (str.equals("boolean")) 352 return java.lang.Boolean.TYPE; 353 else if (str.equals("byte")) 354 return java.lang.Byte.TYPE; 355 else if (str.equals("short")) 356 return java.lang.Short.TYPE; 357 else if (str.equals("long")) 358 return java.lang.Long.TYPE; 359 else if (str.equals("float")) 360 return java.lang.Float.TYPE; 361 else if (str.equals("double")) 362 return java.lang.Double.TYPE; 363 else if (str.equals("void")) 364 return java.lang.Void.TYPE; 365 else if (str.equals("char")) 366 return java.lang.Character.TYPE; 367 368 return null; 369 } 370 371 public static boolean isSuperTypeInArray(String className, Class [] types) { 372 try { 373 Class c = MOP.forName(className); 374 return isSuperTypeInArray(c, types); 375 } catch (ClassNotFoundException e) { 376 throw new InternalException(e); 377 } 378 } 379 380 public static boolean isSuperTypeInArray(Class c, Class [] types) { 381 for (int i = 0; i < types.length; i++) { 382 if (types[i].isAssignableFrom(c)) 383 return true; 384 } 385 return false; 386 } 387 388 public static Object makeDeepCopy(Object source) throws java.io.IOException { 389 java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream (); 390 java.io.ObjectOutputStream oos = new PAObjectOutputStream(baos); 392 oos.writeObject(source); 393 oos.flush(); 394 oos.close(); 395 java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream (baos.toByteArray()); 396 java.io.ObjectInputStream ois = new PAObjectInputStream(bais); 398 try { 399 Object result = ois.readObject(); 400 ois.close(); 401 return result; 402 } catch (ClassNotFoundException e) { 403 throw new java.io.IOException ("ClassNotFoundException e=" + e); 404 } 405 } 406 407 public static String convertClassNameToStubClassName(String classname) { 408 if (classname.length() == 0) { 409 return classname; 410 } 411 int n = classname.lastIndexOf('.'); 412 if (n == -1) { 413 return STUB_DEFAULT_PACKAGE + STUB_DEFAULT_PREFIX + classname; 415 } else { 416 return STUB_DEFAULT_PACKAGE + classname.substring(0, n + 1) + 417 STUB_DEFAULT_PREFIX + classname.substring(n + 1); 418 } 419 } 420 421 public static boolean isStubClassName(String classname) { 422 if (classname.startsWith(STUB_DEFAULT_PACKAGE)) { 423 int index = classname.lastIndexOf("."); 425 if (index != -1) { 426 return classname.substring(index + 1).startsWith(Utils.STUB_DEFAULT_PREFIX); 427 } else { 428 return classname.startsWith(Utils.STUB_DEFAULT_PREFIX); 429 } 430 } else { 431 return false; 432 } 433 } 434 435 public static String convertStubClassNameToClassName(String stubclassname) { 436 if (isStubClassName(stubclassname)) { 437 String temp = stubclassname.substring(Utils.STUB_DEFAULT_PACKAGE.length()); 438 String packageName = Utils.getPackageName(temp); 439 String stubClassSimpleName = Utils.getSimpleName(temp); 440 String classsimplename = stubClassSimpleName.substring(Utils.STUB_DEFAULT_PREFIX.length()); 441 String result; 443 if (packageName.equals("")) { 444 result = classsimplename; 446 } else { 447 result = packageName + "." + classsimplename; 448 } 449 return result; 451 } else { 452 return stubclassname; 453 } 454 } 455 456 private static final Class silentForName(String classname) { 457 try { 458 return MOP.forName(classname); 459 } catch (ClassNotFoundException e) { 460 System.err.println("Static initializer in class org.objectweb.proactive.core.mop.Utils: Cannot load classe " + classname); 461 return null; 462 } 463 } 464 465 } 466 | Popular Tags |