1 22 package org.jboss.iiop.rmi; 23 24 import java.lang.reflect.Field ; 25 import java.lang.reflect.Member ; 26 import java.lang.reflect.Method ; 27 import java.lang.reflect.Modifier ; 28 import java.util.Arrays ; 29 import java.util.Iterator ; 30 31 44 public class RmiIdlUtil { 45 46 public static boolean hasLegalRMIIIOPArguments(Method method) { 47 48 Class [] params = method.getParameterTypes(); 49 50 for (int i = 0; i < params.length; ++i) 51 if (!isRMIIIOPType(params[i])) 52 return false; 53 54 return true; 55 } 56 57 public static boolean hasLegalRMIIIOPReturnType(Method method) { 58 return isRMIIIOPType(method.getReturnType()); 59 } 60 61 public static boolean hasLegalRMIIIOPExceptionTypes(Method method) { 62 63 70 Iterator it = Arrays.asList(method.getExceptionTypes()).iterator(); 71 72 while (it.hasNext()) { 73 Class exception = (Class )it.next(); 74 75 if (!isRMIIDLExceptionType(exception)) 76 return false; 77 } 78 79 return true; 80 } 81 82 86 public static boolean throwsRemoteException(Method method) { 87 88 Class [] exception = method.getExceptionTypes(); 89 90 for (int i = 0; i < exception.length; ++i) 91 if (exception[i].isAssignableFrom(java.rmi.RemoteException .class)) 92 return true; 93 94 return false; 95 } 96 97 101 public static boolean isStatic(Member member) { 102 return (Modifier.isStatic(member.getModifiers())); 103 } 104 105 108 public static boolean isStatic(Class c) { 109 return (Modifier.isStatic(c.getModifiers())); 110 } 111 112 116 public static boolean isFinal(Member member) { 117 return (Modifier.isFinal(member.getModifiers())); 118 } 119 120 123 public static boolean isFinal(Class c) { 124 return (Modifier.isFinal(c.getModifiers())); 125 } 126 127 131 public static boolean isPublic(Member member) { 132 return (Modifier.isPublic(member.getModifiers())); 133 } 134 135 138 public static boolean isPublic(Class c) { 139 return (Modifier.isPublic(c.getModifiers())); 140 } 141 142 145 public static boolean isAllFieldsPublic(Class c) { 146 try { 147 Field list[] = c.getFields(); 148 for(int i=0; i<list.length; i++) 149 if(!Modifier.isPublic(list[i].getModifiers())) 150 return false; 151 } catch(Exception e) { 152 return false; 153 } 154 return true; 155 } 156 157 160 public static boolean isAbstract(Class c) { 161 return (Modifier.isAbstract(c.getModifiers())); 162 } 163 164 public static boolean isRMIIIOPType(Class type) { 165 166 183 184 189 if (type.isPrimitive()) 190 return true; 191 192 197 if (type.isArray()) 198 return isRMIIIOPType(type.getComponentType()); 199 200 205 if (org.omg.CORBA.Object .class.isAssignableFrom(type)) 206 return true; 207 208 213 if (org.omg.CORBA.portable.IDLEntity .class.isAssignableFrom(type)) 214 return true; 215 216 221 if (isRMIIDLRemoteInterface(type)) 222 return true; 223 224 229 if (isRMIIDLExceptionType(type)) 230 return true; 231 232 237 if (isRMIIDLValueType(type)) 238 return true; 239 240 return false; 241 } 242 243 public static boolean isRMIIDLRemoteInterface(Class type) { 244 245 249 if (!java.rmi.Remote .class.isAssignableFrom(type)) 250 return false; 251 252 Iterator methodIterator = Arrays.asList(type.getMethods()).iterator(); 253 254 while (methodIterator.hasNext()) { 255 Method m = (Method )methodIterator.next(); 256 257 263 if (!throwsRemoteException(m)) { 264 return false; 265 } 266 267 274 Iterator it = Arrays.asList(m.getExceptionTypes()).iterator(); 275 276 while (it.hasNext()) { 277 Class exception = (Class )it.next(); 278 279 if (!isRMIIDLExceptionType(exception)) 280 return false; 281 } 282 } 283 284 290 Iterator fieldIterator = Arrays.asList(type.getFields()).iterator(); 291 292 while (fieldIterator.hasNext()) { 293 294 Field f = (Field )fieldIterator.next(); 295 296 if (f.getType().isPrimitive()) 297 continue; 298 299 if (f.getType().equals(java.lang.String .class)) 300 continue; 301 302 return false; 303 } 304 305 return true; 306 } 307 308 public static boolean isAbstractInterface(Class type) { 309 310 313 if (!type.isInterface()) 314 return false; 315 316 319 if (org.omg.CORBA.Object .class.isAssignableFrom(type)) 320 return false; 321 322 325 if (java.rmi.Remote .class.isAssignableFrom(type)) 326 return false; 327 328 Iterator methodIterator = Arrays.asList(type.getMethods()).iterator(); 329 330 while (methodIterator.hasNext()) { 331 Method m = (Method )methodIterator.next(); 332 333 337 if (!throwsRemoteException(m)) { 338 return false; 339 } 340 341 } 342 343 return true; 344 } 345 346 public static boolean isRMIIDLExceptionType(Class type) { 347 348 354 if (!Throwable .class.isAssignableFrom(type)) 355 return false; 356 357 if (Error .class.isAssignableFrom(type)) 358 return false; 359 360 if (RuntimeException .class.isAssignableFrom(type)) 361 return false; 362 363 if (!isRMIIDLValueType(type)) 364 return false; 365 366 return true; 367 } 368 369 public static boolean isRMIIDLValueType(Class type) { 370 371 377 if (java.rmi.Remote .class.isAssignableFrom(type)) 378 return false; 379 380 381 384 if (org.omg.CORBA.Object .class.isAssignableFrom(type)) 385 return false; 386 387 393 if (type.getDeclaringClass() != null && isStatic(type)) 394 if (!isRMIIDLValueType(type.getDeclaringClass())) 395 return false; 396 397 return true; 398 } 399 400 public static boolean isAbstractValueType(Class type) { 401 402 if (!type.isInterface()) 403 return false; 404 405 if (org.omg.CORBA.Object .class.isAssignableFrom(type)) 406 return false; 407 408 boolean cannotBeRemote = false; 409 boolean cannotBeAbstractInterface = false; 410 411 if (java.rmi.Remote .class.isAssignableFrom(type)) { 412 cannotBeAbstractInterface = true; 413 } 414 else { 415 cannotBeRemote = true; 416 } 417 418 Iterator methodIterator = Arrays.asList(type.getMethods()).iterator(); 419 420 while (methodIterator.hasNext()) { 421 Method m = (Method )methodIterator.next(); 422 423 if (!throwsRemoteException(m)) { 424 cannotBeAbstractInterface = true; 425 cannotBeRemote = true; 426 break; 427 } 428 429 Iterator it = Arrays.asList(m.getExceptionTypes()).iterator(); 430 431 while (it.hasNext()) { 432 Class exception = (Class )it.next(); 433 434 if (!isRMIIDLExceptionType(exception)) { 435 cannotBeRemote = true; 436 break; 437 } 438 } 439 } 440 441 if (!cannotBeRemote) { 442 Iterator fieldIterator = Arrays.asList(type.getFields()).iterator(); 443 444 while (fieldIterator.hasNext()) { 445 Field f = (Field )fieldIterator.next(); 446 447 if (f.getType().isPrimitive()) 448 continue; 449 if (f.getType().equals(java.lang.String .class)) 450 continue; 451 cannotBeRemote = true; 452 break; 453 } 454 } 455 return cannotBeRemote && cannotBeAbstractInterface; 456 } 457 458 public static void rethrowIfCorbaSystemException(Exception e) 459 { 460 if (e instanceof java.rmi.MarshalException ) 461 throw new org.omg.CORBA.MARSHAL (e.toString()); 462 else if (e instanceof java.rmi.NoSuchObjectException ) 463 throw new org.omg.CORBA.OBJECT_NOT_EXIST (e.toString()); 464 else if (e instanceof java.rmi.AccessException ) 465 throw new org.omg.CORBA.NO_PERMISSION (e.toString()); 466 else if (e instanceof javax.transaction.TransactionRequiredException ) 467 throw new org.omg.CORBA.TRANSACTION_REQUIRED (e.toString()); 468 else if (e instanceof javax.transaction.TransactionRolledbackException ) 469 throw new org.omg.CORBA.TRANSACTION_ROLLEDBACK (e.toString()); 470 else if (e instanceof javax.transaction.InvalidTransactionException ) 471 throw new org.omg.CORBA.INVALID_TRANSACTION (e.toString()); 472 } 473 } 474 | Popular Tags |