idltypesutil.java

来自「JAVA 所有包」· Java 代码 · 共 581 行 · 第 1/2 页

JAVA
581
字号
/* * @(#)IDLTypesUtil.java	1.7 06/08/12 *  * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.corba.se.impl.presentation.rmi ;import java.lang.reflect.Method;import java.lang.reflect.Field;import java.util.Set;import java.util.HashSet;import java.util.Iterator;/** * Utility class for testing RMI/IDL Types as defined in * Section 1.2 of The Java Language to IDL Mapping.  Note that * these are static checks only.  Runtime checks, such as those * described in Section 1.2.3, #3, are not covered. */public final class IDLTypesUtil {    private static final String GET_PROPERTY_PREFIX = "get";    private static final String SET_PROPERTY_PREFIX = "set";    private static final String IS_PROPERTY_PREFIX  = "is";    public static final int VALID_TYPE   = 0;    public static final int INVALID_TYPE = 1;    /* rmic -iiop does not correctly implement the clause in 1.3.4.3      * about is<NAME>/get<NAME> conflicts.  The spec says that     * is<NAME> is the property and get<NAME> is left alone,     * but rmic does the opposite.  We will follow rmic in this,     * but it's easy to change.     */    public static final boolean FOLLOW_RMIC = true ;    /**     * Validate a class to ensure it conforms to the rules for a     * Java RMI/IIOP interface.     *     * @throws IDLTypeException if not a valid RMI/IIOP interface.     */    public void validateRemoteInterface(Class c) throws IDLTypeException    {        if( c == null ) {            throw new IllegalArgumentException();        }         if( !c.isInterface() ) {            String msg = "Class " + c + " must be a java interface.";            throw new IDLTypeException(msg);        }        if( !java.rmi.Remote.class.isAssignableFrom(c) ) {            String msg = "Class " + c + " must extend java.rmi.Remote, " +                "either directly or indirectly.";            throw new IDLTypeException(msg);        }        // Get all methods, including super-interface methods.        Method[] methods = c.getMethods();                for(int i = 0; i < methods.length; i++) {            Method next = methods[i];            validateExceptions(next);        }        	// Removed because of bug 4989053        // validateDirectInterfaces(c);        validateConstants(c);        return;    }    public boolean isRemoteInterface(Class c)         {        boolean remoteInterface = true;        try {            validateRemoteInterface(c);        } catch(IDLTypeException ite) {            remoteInterface = false;        }        return remoteInterface;    }    /**     * Section 1.2.2 Primitive Types     */     public boolean isPrimitive(Class c)     {        if( c == null ) {            throw new IllegalArgumentException();        }         return c.isPrimitive();    }    /**     * Section 1.2.4     */    public boolean isValue(Class c)     {        if( c == null ) {            throw new IllegalArgumentException();        }         return             (!c.isInterface() &&             java.io.Serializable.class.isAssignableFrom(c) &&             !java.rmi.Remote.class.isAssignableFrom(c));    }    /**     * Section 1.2.5     */    public boolean isArray(Class c)     {        boolean arrayType = false;        if( c == null ) {            throw new IllegalArgumentException();        }         if( c.isArray() ) {            Class componentType = c.getComponentType();            arrayType =                (isPrimitive(componentType) || isRemoteInterface(componentType) ||                 isEntity(componentType) || isException(componentType) || 		 isValue(componentType) || isObjectReference(componentType) );        }        return arrayType;    }    /**     * Section 1.2.6     */    public boolean isException(Class c)     {        if( c == null ) {            throw new IllegalArgumentException();        }         // Must be a checked exception, not including RemoteException or        // its subclasses.        return isCheckedException(c) && !isRemoteException(c) && isValue(c);    }    public boolean isRemoteException(Class c)    {        if( c == null ) {            throw new IllegalArgumentException();        } 	return java.rmi.RemoteException.class.isAssignableFrom(c) ;    }    public boolean isCheckedException(Class c)     {        if( c == null ) {            throw new IllegalArgumentException();        }         return Throwable.class.isAssignableFrom(c) &&            !RuntimeException.class.isAssignableFrom(c) &&            !Error.class.isAssignableFrom(c) ;    }    /**     * Section 1.2.7     */     public boolean isObjectReference(Class c)     {        if( c == null ) {            throw new IllegalArgumentException();        }         return (c.isInterface() &&                 org.omg.CORBA.Object.class.isAssignableFrom(c));    }    /**     * Section 1.2.8     */     public boolean isEntity(Class c)    {        if( c == null ) {            throw new IllegalArgumentException();        }         Class superClass = c.getSuperclass();        return (!c.isInterface() &&                (superClass != null) &&                 (org.omg.CORBA.portable.IDLEntity.class.isAssignableFrom(c)));    }    /**     * Return true if given method is legal property accessor as defined in      * Section 1.3.4.3 of Java2IDL spec.     */    public boolean isPropertyAccessorMethod(Method m, Class c) {                String methodName = m.getName();        Class returnType  = m.getReturnType();        Class[] parameters = m.getParameterTypes();        Class[] exceptionTypes = m.getExceptionTypes();        String propertyType = null;        if( methodName.startsWith(GET_PROPERTY_PREFIX) ) {            if((parameters.length == 0) && (returnType != Void.TYPE) &&		!readHasCorrespondingIsProperty(m, c)) {                propertyType = GET_PROPERTY_PREFIX;            }                   } else if( methodName.startsWith(SET_PROPERTY_PREFIX) ) {                        if((returnType == Void.TYPE) && (parameters.length == 1)) {                if (hasCorrespondingReadProperty(m, c, GET_PROPERTY_PREFIX) ||                    hasCorrespondingReadProperty(m, c, IS_PROPERTY_PREFIX)) {                    propertyType = SET_PROPERTY_PREFIX;                }            }        } else if( methodName.startsWith(IS_PROPERTY_PREFIX) ) {            if((parameters.length == 0) && (returnType == Boolean.TYPE) &&		!isHasCorrespondingReadProperty(m, c)) {                propertyType = IS_PROPERTY_PREFIX;                         }        }        // Some final checks that apply to all properties.          if( propertyType != null ) {            if(!validPropertyExceptions(m) ||                (methodName.length() <= propertyType.length())) {                propertyType = null;            }                                               }        return (propertyType != null);    }    private boolean hasCorrespondingReadProperty        (Method writeProperty, Class c, String readPropertyPrefix)     {        String writePropertyMethodName = writeProperty.getName();        Class[] writePropertyParameters = writeProperty.getParameterTypes();        boolean foundReadProperty = false;        try {                        // Look for a valid corresponding Read property            String readPropertyMethodName =                 writePropertyMethodName.replaceFirst                    (SET_PROPERTY_PREFIX, readPropertyPrefix);            Method readPropertyMethod = c.getMethod(readPropertyMethodName,                                                     new Class[] {});            foundReadProperty =                 ( isPropertyAccessorMethod(readPropertyMethod, c) &&                  (readPropertyMethod.getReturnType() ==                    writePropertyParameters[0]) );        } catch(Exception e) {            // ignore. this means we didn't find a corresponding get property.        }        return foundReadProperty;    }    private boolean readHasCorrespondingIsProperty(Method readProperty, 	Class c)     {	if (FOLLOW_RMIC)	    return false ;        String readPropertyMethodName = readProperty.getName();        boolean foundIsProperty = false;        try {                        // Look for a valid corresponding Is property            String isPropertyMethodName =                 readPropertyMethodName.replaceFirst(GET_PROPERTY_PREFIX, 		    IS_PROPERTY_PREFIX);            Method isPropertyMethod = c.getMethod( isPropertyMethodName,                                                     new Class[] {});            foundIsProperty = isPropertyAccessorMethod(isPropertyMethod, 		c) ;         } catch(Exception e) {            // ignore. this means we didn't find a corresponding Is property.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?