idlnametranslatorimpl.java
来自「JAVA 所有包」· Java 代码 · 共 911 行 · 第 1/3 页
JAVA
911 行
/* * @(#)IDLNameTranslatorImpl.java 1.13 05/11/17 * * 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.security.AccessController;import java.security.PrivilegedAction;import java.lang.reflect.Method;import java.math.BigInteger;import java.util.Map;import java.util.Set;import java.util.HashSet;import java.util.Iterator;import java.util.HashMap;import java.util.StringTokenizer;import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator ;import com.sun.corba.se.impl.presentation.rmi.IDLType ;import com.sun.corba.se.impl.presentation.rmi.IDLTypeException ;import com.sun.corba.se.impl.presentation.rmi.IDLTypesUtil ;import com.sun.corba.se.impl.orbutil.ObjectUtility ;/** * Bidirectional translator between RMI-IIOP interface methods and * and IDL Names. */public class IDLNameTranslatorImpl implements IDLNameTranslator { // From CORBA Spec, Table 6 Keywords. // Note that since all IDL identifiers are case // insensitive, java identifier comparisons to these // will be case insensitive also. private static String[] IDL_KEYWORDS = { "abstract", "any", "attribute", "boolean", "case", "char", "const", "context", "custom", "default", "double", "enum", "exception", "factory", "FALSE", "fixed", "float", "in", "inout", "interface", "long", "module", "native", "Object", "octet", "oneway", "out", "private", "public", "raises", "readonly", "sequence", "short", "string", "struct", "supports", "switch", "TRUE", "truncatable", "typedef", "unsigned", "union", "ValueBase", "valuetype", "void", "wchar", "wstring" }; private static char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; private static final String UNDERSCORE = "_"; // used to mangle java inner class names private static final String INNER_CLASS_SEPARATOR = UNDERSCORE + UNDERSCORE; // used to form IDL array type names private static final String[] BASE_IDL_ARRAY_MODULE_TYPE= new String[] { "org", "omg", "boxedRMI" } ; private static final String BASE_IDL_ARRAY_ELEMENT_TYPE = "seq"; // used to mangling java identifiers that have a leading underscore private static final String LEADING_UNDERSCORE_CHAR = "J"; private static final String ID_CONTAINER_CLASH_CHAR = UNDERSCORE; // separator used between types in a mangled overloaded method name private static final String OVERLOADED_TYPE_SEPARATOR = UNDERSCORE + UNDERSCORE; // string appended to attribute if it clashes with a method name private static final String ATTRIBUTE_METHOD_CLASH_MANGLE_CHARS = UNDERSCORE + UNDERSCORE; // strings prepended to the attribute names in order to form their // IDL names. private static final String GET_ATTRIBUTE_PREFIX = "_get_"; private static final String SET_ATTRIBUTE_PREFIX = "_set_"; private static final String IS_ATTRIBUTE_PREFIX = "_get_"; private static Set idlKeywords_; static { idlKeywords_ = new HashSet(); for(int i = 0; i < IDL_KEYWORDS.length; i++) { String next = (String) IDL_KEYWORDS[i]; // Convert keyword to all caps to ease equality // check. String keywordAllCaps = next.toUpperCase(); idlKeywords_.add(keywordAllCaps); } } // // Instance state // // Remote interface for name translation. private Class[] interf_; // Maps used to hold name translations. These do not need to be // synchronized since the translation is never modified after // initialization. private Map methodToIDLNameMap_; private Map IDLNameToMethodMap_; private Method[] methods_; /** * Return an IDLNameTranslator for the given interface. * * @throws IllegalStateException if given class is not a valid * RMI/IIOP Remote Interface */ public static IDLNameTranslator get( Class interf ) { return new IDLNameTranslatorImpl(new Class[] { interf } ); } /** * Return an IDLNameTranslator for the given interfacex. * * @throws IllegalStateException if given classes are not valid * RMI/IIOP Remote Interfaces */ public static IDLNameTranslator get( Class[] interfaces ) { return new IDLNameTranslatorImpl(interfaces ); } public static String getExceptionId( Class cls ) { // Requirements for this method: // 1. cls must be an exception but not a RemoteException. // 2. If cls has an IDL keyword name, an underscore is prepended (1.3.2.2). // 3. If cls jas a leading underscore, J is prepended (1.3.2.3). // 4. If cls has an illegal IDL ident char, it is mapped to UXXXX where // XXXX is the unicode value in hex of the char (1.3.2.4). // 5. double underscore for inner class (1.3.2.5). // 6. The ID is "IDL:" + name with / separators + ":1.0". IDLType itype = classToIDLType( cls ) ; return itype.getExceptionName() ; } public Class[] getInterfaces() { return interf_; } public Method[] getMethods() { return methods_ ; } public Method getMethod( String idlName ) { return (Method) IDLNameToMethodMap_.get(idlName); } public String getIDLName( Method method ) { return (String) methodToIDLNameMap_.get(method); } /** * Initialize an IDLNameTranslator for the given interface. * * @throws IllegalStateException if given class is not a valid * RMI/IIOP Remote Interface */ private IDLNameTranslatorImpl(Class[] interfaces) { try { IDLTypesUtil idlTypesUtil = new IDLTypesUtil(); for (int ctr=0; ctr<interfaces.length; ctr++) idlTypesUtil.validateRemoteInterface(interfaces[ctr]); interf_ = interfaces; buildNameTranslation(); } catch( IDLTypeException ite) { String msg = ite.getMessage(); IllegalStateException ise = new IllegalStateException(msg); ise.initCause(ite); throw ise; } } private void buildNameTranslation() { // holds method info, keyed by method Map allMethodInfo = new HashMap() ; for (int ctr=0; ctr<interf_.length; ctr++) { Class interf = interf_[ctr] ; IDLTypesUtil idlTypesUtil = new IDLTypesUtil(); final Method[] methods = interf.getMethods(); // Handle the case of a non-public interface! AccessController.doPrivileged(new PrivilegedAction() { public Object run() { Method.setAccessible( methods, true ) ; return null ; } } ) ; // Take an initial pass through all the methods and create some // information that will be used to track the IDL name // transformation. for(int i = 0; i < methods.length; i++) { Method nextMethod = methods[i]; IDLMethodInfo methodInfo = new IDLMethodInfo(); methodInfo.method = nextMethod; if (idlTypesUtil.isPropertyAccessorMethod(nextMethod, interf)) { methodInfo.isProperty = true; String attributeName = idlTypesUtil. getAttributeNameForProperty(nextMethod.getName()); methodInfo.originalName = attributeName; methodInfo.mangledName = attributeName; } else { methodInfo.isProperty = false; methodInfo.originalName = nextMethod.getName(); methodInfo.mangledName = nextMethod.getName(); } allMethodInfo.put(nextMethod, methodInfo); } } // // Perform case sensitivity test first. This applies to all // method names AND attributes. Compare each method name and // attribute to all other method names and attributes. If names // differ only in case, apply mangling as defined in section 1.3.2.7 // of Java2IDL spec. Note that we compare using the original names. // for(Iterator outerIter=allMethodInfo.values().iterator(); outerIter.hasNext();) { IDLMethodInfo outer = (IDLMethodInfo) outerIter.next(); for(Iterator innerIter = allMethodInfo.values().iterator(); innerIter.hasNext();) { IDLMethodInfo inner = (IDLMethodInfo) innerIter.next(); if( (outer != inner) && (!outer.originalName.equals(inner.originalName)) && outer.originalName.equalsIgnoreCase(inner.originalName) ) { outer.mangledName = mangleCaseSensitiveCollision(outer.originalName); break; } } } for(Iterator iter = allMethodInfo.values().iterator(); iter.hasNext();) { IDLMethodInfo next = (IDLMethodInfo) iter.next(); next.mangledName = mangleIdentifier(next.mangledName, next.isProperty); } // // Now check for overloaded method names and apply 1.3.2.6. // for(Iterator outerIter=allMethodInfo.values().iterator(); outerIter.hasNext();) { IDLMethodInfo outer = (IDLMethodInfo) outerIter.next(); if( outer.isProperty ) { continue; } for(Iterator innerIter = allMethodInfo.values().iterator(); innerIter.hasNext();) { IDLMethodInfo inner = (IDLMethodInfo) innerIter.next(); if( (outer != inner) && !inner.isProperty && outer.originalName.equals(inner.originalName) ) { outer.mangledName = mangleOverloadedMethod (outer.mangledName, outer.method); break; } } } // // Now mangle any properties that clash with method names. // for(Iterator outerIter=allMethodInfo.values().iterator();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?