1.1 --- a/src/share/classes/com/sun/corba/se/impl/presentation/rmi/IDLTypesUtil_save.sjava Fri Aug 09 14:24:17 2013 +0100 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,544 +0,0 @@ 1.4 -/* 1.5 - * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 - * 1.8 - * This code is free software; you can redistribute it and/or modify it 1.9 - * under the terms of the GNU General Public License version 2 only, as 1.10 - * published by the Free Software Foundation. Oracle designates this 1.11 - * particular file as subject to the "Classpath" exception as provided 1.12 - * by Oracle in the LICENSE file that accompanied this code. 1.13 - * 1.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 - * version 2 for more details (a copy is included in the LICENSE file that 1.18 - * accompanied this code). 1.19 - * 1.20 - * You should have received a copy of the GNU General Public License version 1.21 - * 2 along with this work; if not, write to the Free Software Foundation, 1.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 - * 1.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 - * or visit www.oracle.com if you need additional information or have any 1.26 - * questions. 1.27 - */ 1.28 - 1.29 -package com.sun.corba.se.impl.presentation.rmi ; 1.30 - 1.31 -import java.lang.reflect.Method; 1.32 -import java.lang.reflect.Field; 1.33 -import java.util.Set; 1.34 -import java.util.HashSet; 1.35 -import java.util.Iterator; 1.36 - 1.37 -/** 1.38 - * Utility class for testing RMI/IDL Types as defined in 1.39 - * Section 1.2 of The Java Language to IDL Mapping. Note that 1.40 - * these are static checks only. Runtime checks, such as those 1.41 - * described in Section 1.2.3, #3, are not covered. 1.42 - */ 1.43 -public class IDLTypesUtil { 1.44 - 1.45 - public static final String JAVA_GET_PROPERTY_PREFIX = "get"; 1.46 - public static final String JAVA_SET_PROPERTY_PREFIX = "set"; 1.47 - public static final String JAVA_IS_PROPERTY_PREFIX = "is"; 1.48 - 1.49 - public static final int VALID_TYPE = 0; 1.50 - public static final int INVALID_TYPE = 1; 1.51 - 1.52 - /** 1.53 - * Validate a class to ensure it conforms to the rules for a 1.54 - * Java RMI/IIOP interface. 1.55 - * 1.56 - * @throws IDLTypeException if not a valid RMI/IIOP interface. 1.57 - */ 1.58 - public void validateRemoteInterface(Class c) throws IDLTypeException 1.59 - { 1.60 - if( c == null ) { 1.61 - throw new IllegalArgumentException(); 1.62 - } 1.63 - 1.64 - if( !c.isInterface() ) { 1.65 - String msg = "Class " + c + " must be a java interface."; 1.66 - throw new IDLTypeException(msg); 1.67 - } 1.68 - 1.69 - if( !java.rmi.Remote.class.isAssignableFrom(c) ) { 1.70 - String msg = "Class " + c + " must extend java.rmi.Remote, " + 1.71 - "either directly or indirectly."; 1.72 - throw new IDLTypeException(msg); 1.73 - } 1.74 - 1.75 - // Get all methods, including super-interface methods. 1.76 - Method[] methods = c.getMethods(); 1.77 - 1.78 - for(int i = 0; i < methods.length; i++) { 1.79 - Method next = methods[i]; 1.80 - validateExceptions(next); 1.81 - } 1.82 - 1.83 - // Removed because of bug 4989053 1.84 - // validateDirectInterfaces(c); 1.85 - validateConstants(c); 1.86 - 1.87 - return; 1.88 - } 1.89 - 1.90 - public boolean isRemoteInterface(Class c) 1.91 - { 1.92 - boolean remoteInterface = true; 1.93 - try { 1.94 - validateRemoteInterface(c); 1.95 - } catch(IDLTypeException ite) { 1.96 - remoteInterface = false; 1.97 - } 1.98 - 1.99 - return remoteInterface; 1.100 - } 1.101 - 1.102 - /** 1.103 - * Section 1.2.2 Primitive Types 1.104 - */ 1.105 - public boolean isPrimitive(Class c) 1.106 - { 1.107 - if( c == null ) { 1.108 - throw new IllegalArgumentException(); 1.109 - } 1.110 - 1.111 - return c.isPrimitive(); 1.112 - } 1.113 - 1.114 - /** 1.115 - * Section 1.2.4 1.116 - */ 1.117 - public boolean isValue(Class c) 1.118 - { 1.119 - if( c == null ) { 1.120 - throw new IllegalArgumentException(); 1.121 - } 1.122 - 1.123 - return 1.124 - (!c.isInterface() && 1.125 - java.io.Serializable.class.isAssignableFrom(c) && 1.126 - !java.rmi.Remote.class.isAssignableFrom(c)); 1.127 - } 1.128 - 1.129 - /** 1.130 - * Section 1.2.5 1.131 - */ 1.132 - public boolean isArray(Class c) 1.133 - { 1.134 - boolean arrayType = false; 1.135 - 1.136 - if( c == null ) { 1.137 - throw new IllegalArgumentException(); 1.138 - } 1.139 - 1.140 - if( c.isArray() ) { 1.141 - Class componentType = c.getComponentType(); 1.142 - arrayType = 1.143 - (isPrimitive(componentType) || isRemoteInterface(componentType) || 1.144 - isEntity(componentType) || isException(componentType) || 1.145 - isValue(componentType) || isObjectReference(componentType) ); 1.146 - } 1.147 - 1.148 - return arrayType; 1.149 - } 1.150 - 1.151 - /** 1.152 - * Section 1.2.6 1.153 - */ 1.154 - public boolean isException(Class c) 1.155 - { 1.156 - if( c == null ) { 1.157 - throw new IllegalArgumentException(); 1.158 - } 1.159 - 1.160 - // Must be a checked exception, not including RemoteException or 1.161 - // its subclasses. 1.162 - return isCheckedException(c) && !isRemoteException(c) && isValue(c); 1.163 - } 1.164 - 1.165 - public boolean isRemoteException(Class c) 1.166 - { 1.167 - if( c == null ) { 1.168 - throw new IllegalArgumentException(); 1.169 - } 1.170 - 1.171 - return java.rmi.RemoteException.class.isAssignableFrom(c) ; 1.172 - } 1.173 - 1.174 - public boolean isCheckedException(Class c) 1.175 - { 1.176 - if( c == null ) { 1.177 - throw new IllegalArgumentException(); 1.178 - } 1.179 - 1.180 - return Throwable.class.isAssignableFrom(c) && 1.181 - !RuntimeException.class.isAssignableFrom(c) && 1.182 - !Error.class.isAssignableFrom(c) ; 1.183 - } 1.184 - 1.185 - /** 1.186 - * Section 1.2.7 1.187 - */ 1.188 - public boolean isObjectReference(Class c) 1.189 - { 1.190 - if( c == null ) { 1.191 - throw new IllegalArgumentException(); 1.192 - } 1.193 - 1.194 - return (c.isInterface() && 1.195 - org.omg.CORBA.Object.class.isAssignableFrom(c)); 1.196 - } 1.197 - 1.198 - /** 1.199 - * Section 1.2.8 1.200 - */ 1.201 - public boolean isEntity(Class c) 1.202 - { 1.203 - if( c == null ) { 1.204 - throw new IllegalArgumentException(); 1.205 - } 1.206 - 1.207 - Class superClass = c.getSuperclass(); 1.208 - return (!c.isInterface() && 1.209 - (superClass != null) && 1.210 - (org.omg.CORBA.portable.IDLEntity.class.isAssignableFrom(c))); 1.211 - } 1.212 - 1.213 - public String javaPropertyPrefixToIDL( String javaPrefix ) 1.214 - { 1.215 - return "_" + javaPrefix + "_" ; 1.216 - } 1.217 - 1.218 - /** 1.219 - * Return the property type if given method is legal property accessor as defined in 1.220 - * Section 1.3.4.3 of Java2IDL spec. Result is one of: JAVA_GET_PROPERTY_PREFIX, 1.221 - * JAVA_SET_PROPERTY_PREFIX, JAVA_IS_PROPERTY_PREFIX. 1.222 - */ 1.223 - public String propertyAccessorMethodType(Method m, Class c) { 1.224 - 1.225 - String methodName = m.getName(); 1.226 - Class returnType = m.getReturnType(); 1.227 - Class[] parameters = m.getParameterTypes(); 1.228 - Class[] exceptionTypes = m.getExceptionTypes(); 1.229 - String propertyType = null; 1.230 - 1.231 - if( methodName.startsWith(JAVA_GET_PROPERTY_PREFIX) ) { 1.232 - 1.233 - if((parameters.length == 0) && (returnType != Void.TYPE) && 1.234 - !hasCorrespondingReadProperty(m, c, JAVA_IS_PROPERTY_PREFIX) { 1.235 - propertyType = JAVA_GET_PROPERTY_PREFIX; 1.236 - } 1.237 - 1.238 - } else if( methodName.startsWith(JAVA_SET_PROPERTY_PREFIX) ) { 1.239 - 1.240 - if((returnType == Void.TYPE) && (parameters.length == 1)) { 1.241 - if (hasCorrespondingReadProperty(m, c, JAVA_GET_PROPERTY_PREFIX) || 1.242 - hasCorrespondingReadProperty(m, c, JAVA_IS_PROPERTY_PREFIX)) { 1.243 - propertyType = JAVA_SET_PROPERTY_PREFIX; 1.244 - } 1.245 - } 1.246 - 1.247 - } else if( methodName.startsWith(JAVA_IS_PROPERTY_PREFIX) ) { 1.248 - if((parameters.length == 0) && (returnType == Boolean.TYPE)) { 1.249 - propertyType = JAVA_IS_PROPERTY_PREFIX; 1.250 - } 1.251 - } 1.252 - 1.253 - // Some final checks that apply to all properties. 1.254 - if( propertyType != null ) { 1.255 - if(!validPropertyExceptions(m) || 1.256 - (methodName.length() <= propertyType.length())) { 1.257 - propertyType = null; 1.258 - } 1.259 - } 1.260 - 1.261 - return propertyType ; 1.262 - } 1.263 - 1.264 - private boolean hasCorrespondingReadProperty 1.265 - (Method writeProperty, Class c, String readPropertyPrefix) { 1.266 - 1.267 - String writePropertyMethodName = writeProperty.getName(); 1.268 - Class[] writePropertyParameters = writeProperty.getParameterTypes(); 1.269 - boolean foundReadProperty = false; 1.270 - 1.271 - try { 1.272 - // Look for a valid corresponding Read property 1.273 - String readPropertyMethodName = 1.274 - writePropertyMethodName.replaceFirst 1.275 - (JAVA_SET_PROPERTY_PREFIX, readPropertyPrefix); 1.276 - Method readPropertyMethod = c.getMethod(readPropertyMethodName, 1.277 - new Class[] {}); 1.278 - foundReadProperty = 1.279 - ((propertyAccessorMethodType(readPropertyMethod, c) != null) && 1.280 - (readPropertyMethod.getReturnType() == 1.281 - writePropertyParameters[0])); 1.282 - } catch(Exception e) { 1.283 - // ignore. this means we didn't find a corresponding get property. 1.284 - } 1.285 - 1.286 - return foundReadProperty; 1.287 - } 1.288 - 1.289 - public String getAttributeNameForProperty(String propertyName) { 1.290 - String attributeName = null; 1.291 - String prefix = null; 1.292 - 1.293 - if( propertyName.startsWith(JAVA_GET_PROPERTY_PREFIX) ) { 1.294 - prefix = JAVA_GET_PROPERTY_PREFIX; 1.295 - } else if( propertyName.startsWith(JAVA_SET_PROPERTY_PREFIX) ) { 1.296 - prefix = JAVA_SET_PROPERTY_PREFIX; 1.297 - } else if( propertyName.startsWith(JAVA_IS_PROPERTY_PREFIX) ) { 1.298 - prefix = JAVA_IS_PROPERTY_PREFIX; 1.299 - } 1.300 - 1.301 - if( (prefix != null) && (prefix.length() < propertyName.length()) ) { 1.302 - String remainder = propertyName.substring(prefix.length()); 1.303 - if( (remainder.length() >= 2) && 1.304 - Character.isUpperCase(remainder.charAt(0)) && 1.305 - Character.isUpperCase(remainder.charAt(1)) ) { 1.306 - // don't set the first letter to lower-case if the 1.307 - // first two are upper-case 1.308 - attributeName = remainder; 1.309 - } else { 1.310 - attributeName = Character.toLowerCase(remainder.charAt(0)) + 1.311 - remainder.substring(1); 1.312 - } 1.313 - } 1.314 - 1.315 - return attributeName; 1.316 - } 1.317 - 1.318 - /** 1.319 - * Return IDL Type name for primitive types as defined in 1.320 - * Section 1.3.3 of Java2IDL spec or null if not a primitive type. 1.321 - */ 1.322 - public IDLType getPrimitiveIDLTypeMapping(Class c) { 1.323 - 1.324 - if( c == null ) { 1.325 - throw new IllegalArgumentException(); 1.326 - } 1.327 - 1.328 - if( c.isPrimitive() ) { 1.329 - if( c == Void.TYPE ) { 1.330 - return new IDLType( c, "void" ) ; 1.331 - } else if( c == Boolean.TYPE ) { 1.332 - return new IDLType( c, "boolean" ) ; 1.333 - } else if( c == Character.TYPE ) { 1.334 - return new IDLType( c, "wchar" ) ; 1.335 - } else if( c == Byte.TYPE ) { 1.336 - return new IDLType( c, "octet" ) ; 1.337 - } else if( c == Short.TYPE ) { 1.338 - return new IDLType( c, "short" ) ; 1.339 - } else if( c == Integer.TYPE ) { 1.340 - return new IDLType( c, "long" ) ; 1.341 - } else if( c == Long.TYPE ) { 1.342 - return new IDLType( c, "long_long" ) ; 1.343 - } else if( c == Float.TYPE ) { 1.344 - return new IDLType( c, "float" ) ; 1.345 - } else if( c == Double.TYPE ) { 1.346 - return new IDLType( c, "double" ) ; 1.347 - } 1.348 - } 1.349 - 1.350 - return null; 1.351 - } 1.352 - 1.353 - /** 1.354 - * Return IDL Type name for special case type mappings as defined in 1.355 - * Table 1-1 of Java2IDL spec or null if given class is not a special 1.356 - * type. 1.357 - */ 1.358 - public IDLType getSpecialCaseIDLTypeMapping(Class c) { 1.359 - 1.360 - if( c == null ) { 1.361 - throw new IllegalArgumentException(); 1.362 - } 1.363 - 1.364 - if( c == java.lang.Object.class ) { 1.365 - return new IDLType( c, new String[] { "java", "lang" }, 1.366 - "Object" ) ; 1.367 - } else if( c == java.lang.String.class ) { 1.368 - return new IDLType( c, new String[] { "CORBA" }, 1.369 - "WStringValue" ) ; 1.370 - } else if( c == java.lang.Class.class ) { 1.371 - return new IDLType( c, new String[] { "javax", "rmi", "CORBA" }, 1.372 - "ClassDesc" ) ; 1.373 - } else if( c == java.io.Serializable.class ) { 1.374 - return new IDLType( c, new String[] { "java", "io" }, 1.375 - "Serializable" ) ; 1.376 - } else if( c == java.io.Externalizable.class ) { 1.377 - return new IDLType( c, new String[] { "java", "io" }, 1.378 - "Externalizable" ) ; 1.379 - } else if( c == java.rmi.Remote.class ) { 1.380 - return new IDLType( c, new String[] { "java", "rmi" }, 1.381 - "Remote" ) ; 1.382 - } else if( c == org.omg.CORBA.Object.class ) { 1.383 - return new IDLType( c, "Object" ) ; 1.384 - } else { 1.385 - return null; 1.386 - } 1.387 - } 1.388 - 1.389 - /** 1.390 - * Implements 1.2.3 #2 and #4 1.391 - */ 1.392 - private void validateExceptions(Method method) throws IDLTypeException { 1.393 - 1.394 - Class[] exceptions = method.getExceptionTypes(); 1.395 - 1.396 - boolean declaresRemoteExceptionOrSuperClass = false; 1.397 - 1.398 - // Section 1.2.3, #2 1.399 - for(int eIndex = 0; eIndex < exceptions.length; eIndex++) { 1.400 - Class exception = exceptions[eIndex]; 1.401 - if( isRemoteExceptionOrSuperClass(exception) ) { 1.402 - declaresRemoteExceptionOrSuperClass = true; 1.403 - break; 1.404 - } 1.405 - } 1.406 - 1.407 - if( !declaresRemoteExceptionOrSuperClass ) { 1.408 - String msg = "Method '" + method + "' must throw at least one " + 1.409 - "exception of type java.rmi.RemoteException or one of its " + 1.410 - "super-classes"; 1.411 - throw new IDLTypeException(msg); 1.412 - } 1.413 - 1.414 - // Section 1.2.3, #4 1.415 - // See also bug 4972402 1.416 - // For all exceptions E in exceptions, 1.417 - // (isCheckedException(E) => (isValue(E) || RemoteException.isAssignableFrom( E ) ) 1.418 - for(int eIndex = 0; eIndex < exceptions.length; eIndex++) { 1.419 - Class exception = exceptions[eIndex]; 1.420 - 1.421 - if (isCheckedException(exception) && !isValue(exception) && 1.422 - !isRemoteException(exception)) 1.423 - { 1.424 - String msg = "Exception '" + exception + "' on method '" + 1.425 - method + "' is not a allowed RMI/IIOP exception type"; 1.426 - throw new IDLTypeException(msg); 1.427 - } 1.428 - } 1.429 - 1.430 - return; 1.431 - } 1.432 - 1.433 - /** 1.434 - * Returns true if the method's throw clause conforms to the exception 1.435 - * restrictions for properties as defined in Section 1.3.4.3 of 1.436 - * Java2IDL spec. This means that for all exceptions E declared on the 1.437 - * method, E isChecked => RemoteException.isAssignableFrom( E ). 1.438 - */ 1.439 - private boolean validPropertyExceptions(Method method) 1.440 - { 1.441 - Class[] exceptions = method.getExceptionTypes(); 1.442 - 1.443 - for(int eIndex = 0; eIndex < exceptions.length; eIndex++) { 1.444 - Class exception = exceptions[eIndex]; 1.445 - 1.446 - if (isCheckedException(exception) && !isRemoteException(exception)) 1.447 - return false ; 1.448 - } 1.449 - 1.450 - return true; 1.451 - } 1.452 - 1.453 - /** 1.454 - * Implements Section 1.2.3, #2. 1.455 - */ 1.456 - private boolean isRemoteExceptionOrSuperClass(Class c) { 1.457 - return 1.458 - ((c == java.rmi.RemoteException.class) || 1.459 - (c == java.io.IOException.class) || 1.460 - (c == java.lang.Exception.class) || 1.461 - (c == java.lang.Throwable.class)); 1.462 - } 1.463 - 1.464 - /** 1.465 - * Implements Section 1.2.3, #5. 1.466 - */ 1.467 - private void validateDirectInterfaces(Class c) throws IDLTypeException { 1.468 - 1.469 - Class[] directInterfaces = c.getInterfaces(); 1.470 - 1.471 - if( directInterfaces.length < 2 ) { 1.472 - return; 1.473 - } 1.474 - 1.475 - Set allMethodNames = new HashSet(); 1.476 - Set currentMethodNames = new HashSet(); 1.477 - 1.478 - for(int i = 0; i < directInterfaces.length; i++) { 1.479 - Class next = directInterfaces[i]; 1.480 - Method[] methods = next.getMethods(); 1.481 - 1.482 - // Comparison is based on method names only. First collect 1.483 - // all methods from current interface, eliminating duplicate 1.484 - // names. 1.485 - currentMethodNames.clear(); 1.486 - for(int m = 0; m < methods.length; m++) { 1.487 - currentMethodNames.add(methods[m].getName()); 1.488 - } 1.489 - 1.490 - // Now check each method against list of all unique method 1.491 - // names processed so far. 1.492 - for(Iterator iter=currentMethodNames.iterator(); iter.hasNext();) { 1.493 - String methodName = (String) iter.next(); 1.494 - if( allMethodNames.contains(methodName) ) { 1.495 - String msg = "Class " + c + " inherits method " + 1.496 - methodName + " from multiple direct interfaces."; 1.497 - throw new IDLTypeException(msg); 1.498 - } else { 1.499 - allMethodNames.add(methodName); 1.500 - } 1.501 - } 1.502 - } 1.503 - 1.504 - return; 1.505 - } 1.506 - 1.507 - /** 1.508 - * Implements 1.2.3 #6 1.509 - */ 1.510 - private void validateConstants(final Class c) 1.511 - throws IDLTypeException { 1.512 - 1.513 - Field[] fields = null; 1.514 - 1.515 - try { 1.516 - fields = (Field[]) 1.517 - java.security.AccessController.doPrivileged 1.518 - (new java.security.PrivilegedExceptionAction() { 1.519 - public java.lang.Object run() throws Exception { 1.520 - return c.getFields(); 1.521 - } 1.522 - }); 1.523 - } catch(java.security.PrivilegedActionException pae) { 1.524 - IDLTypeException ite = new IDLTypeException(); 1.525 - ite.initCause(pae); 1.526 - throw ite; 1.527 - } 1.528 - 1.529 - for(int i = 0; i < fields.length; i++) { 1.530 - Field next = fields[i]; 1.531 - Class fieldType = next.getType(); 1.532 - if( (fieldType != java.lang.String.class) && 1.533 - !isPrimitive(fieldType) ) { 1.534 - String msg = "Constant field '" + next.getName() + 1.535 - "' in class '" + next.getDeclaringClass().getName() + 1.536 - "' has invalid type' " + next.getType() + "'. Constants" + 1.537 - " in RMI/IIOP interfaces can only have primitive" + 1.538 - " types and java.lang.String types."; 1.539 - throw new IDLTypeException(msg); 1.540 - } 1.541 - } 1.542 - 1.543 - 1.544 - return; 1.545 - } 1.546 - 1.547 -}