idlnametranslatorimpl.java
来自「JAVA 所有包」· Java 代码 · 共 911 行 · 第 1/3 页
JAVA
911 行
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.mangledName.equals(inner.mangledName) ) { outer.mangledName = outer.mangledName + ATTRIBUTE_METHOD_CLASH_MANGLE_CHARS; break; } } } // // Ensure that no mapped method names clash with mapped name // of container(1.3.2.9). This is a case insensitive comparison. // for (int ctr=0; ctr<interf_.length; ctr++ ) { Class interf = interf_[ctr] ; String mappedContainerName = getMappedContainerName(interf); for(Iterator iter = allMethodInfo.values().iterator(); iter.hasNext();) { IDLMethodInfo next = (IDLMethodInfo) iter.next(); if( !next.isProperty && identifierClashesWithContainer(mappedContainerName, next.mangledName)) { next.mangledName = mangleContainerClash(next.mangledName); } } } // // Populate name translation maps. // methodToIDLNameMap_ = new HashMap(); IDLNameToMethodMap_ = new HashMap(); methods_ = (Method[])allMethodInfo.keySet().toArray( new Method[0] ) ; for(Iterator iter = allMethodInfo.values().iterator(); iter.hasNext();) { IDLMethodInfo next = (IDLMethodInfo) iter.next(); String idlName = next.mangledName; if( next.isProperty ) { String origMethodName = next.method.getName(); String prefix = ""; if( origMethodName.startsWith("get") ) { prefix = GET_ATTRIBUTE_PREFIX; } else if( origMethodName.startsWith("set") ) { prefix = SET_ATTRIBUTE_PREFIX; } else { prefix = IS_ATTRIBUTE_PREFIX; } idlName = prefix + next.mangledName; } methodToIDLNameMap_.put(next.method, idlName); // Final check to see if there are any clashes after all the // manglings have been applied. If so, this is treated as an // invalid interface. Currently, we do a CASE-SENSITIVE // comparison since that matches the rmic behavior. // @@@ Shouldn't this be a case-insensitive check? if( IDLNameToMethodMap_.containsKey(idlName) ) { // @@@ I18N Method clash = (Method) IDLNameToMethodMap_.get(idlName); throw new IllegalStateException("Error : methods " + clash + " and " + next.method + " both result in IDL name '" + idlName + "'"); } else { IDLNameToMethodMap_.put(idlName, next.method); } } return; } /** * Perform all necessary stand-alone identifier mangling operations * on a java identifier that is being transformed into an IDL name. * That is, mangling operations that don't require looking at anything * else but the identifier itself. This covers sections 1.3.2.2, 1.3.2.3, * and 1.3.2.4 of the Java2IDL spec. Method overloading and * case-sensitivity checks are handled elsewhere. */ private static String mangleIdentifier(String identifier) { return mangleIdentifier(identifier, false); } private static String mangleIdentifier(String identifier, boolean attribute) { String mangledName = identifier; // // Apply leading underscore test (1.3.2.3) // This should be done before IDL Keyword clash test, since clashing // IDL keywords are mangled by adding a leading underscore. // if( hasLeadingUnderscore(mangledName) ) { mangledName = mangleLeadingUnderscore(mangledName); } // // Apply IDL keyword clash test (1.3.2.2). // This is not needed for attributes since when the full property // name is composed it cannot clash with an IDL keyword. // (Also, rmic doesn't do it.) // if( !attribute && isIDLKeyword(mangledName) ) { mangledName = mangleIDLKeywordClash(mangledName); } // // Replace illegal IDL identifier characters (1.3.2.4) // for all method names and attributes. // if( !isIDLIdentifier(mangledName) ) { mangledName = mangleUnicodeChars(mangledName); } return mangledName; } // isIDLKeyword and mangleIDLKeywordClash are exposed here so that // IDLType can use them. // // XXX refactoring needed: // 1. Split off isIDLKeywork and mangleIDLKeywordClash (and possibly // other methods) into a utility class. // 2. Move all of classToIDLType to a constructor inside IDLType. // // The problem appears to be that we need all of the code that // performs various checks for name problems and the corresponding // fixes into a utility class. Then we need to see what other // refactorings present themselves. /** * Checks whether a java identifier clashes with an * IDL keyword. Note that this is a case-insensitive * comparison. * * Used to implement section 1.3.2.2 of Java2IDL spec. */ static boolean isIDLKeyword(String identifier) { String identifierAllCaps = identifier.toUpperCase(); return idlKeywords_.contains(identifierAllCaps); } static String mangleIDLKeywordClash(String identifier) { return UNDERSCORE + identifier; } private static String mangleLeadingUnderscore(String identifier) { return LEADING_UNDERSCORE_CHAR + identifier; } /** * Checks whether a java identifier starts with an underscore. * Used to implement section 1.3.2.3 of Java2IDL spec. */ private static boolean hasLeadingUnderscore(String identifier) { return identifier.startsWith(UNDERSCORE); } /** * Implements Section 1.3.2.4 of Java2IDL Mapping. * All non-IDL identifier characters must be replaced * with their Unicode representation. */ static String mangleUnicodeChars(String identifier) { StringBuffer mangledIdentifier = new StringBuffer(); for(int i = 0; i < identifier.length(); i++) { char nextChar = identifier.charAt(i); if( isIDLIdentifierChar(nextChar) ) { mangledIdentifier.append(nextChar); } else { String unicode = charToUnicodeRepresentation(nextChar); mangledIdentifier.append(unicode); } } return mangledIdentifier.toString(); } /** * Implements mangling portion of Section 1.3.2.7 of Java2IDL spec. * This method only deals with the actual mangling. Decision about * whether case-sensitive collision mangling is required is made * elsewhere. * * * "...a mangled name is generated consisting of the original name * followed by an underscore separated list of decimal indices * into the string, where the indices identify all the upper case * characters in the original string. Indices are zero based." * */ String mangleCaseSensitiveCollision(String identifier) { StringBuffer mangledIdentifier = new StringBuffer(identifier); // There is always at least one trailing underscore, whether or // not the identifier has uppercase letters. mangledIdentifier.append(UNDERSCORE); boolean needUnderscore = false; for(int i = 0; i < identifier.length(); i++) { char next = identifier.charAt(i); if( Character.isUpperCase(next) ) { // This bit of logic is needed to ensure that we have // an underscore separated list of indices but no // trailing underscores. Basically, after we have at least // one uppercase letter, we always put an undercore before // printing the next one. if( needUnderscore ) { mangledIdentifier.append(UNDERSCORE); } mangledIdentifier.append(i); needUnderscore = true; } } return mangledIdentifier.toString(); } private static String mangleContainerClash(String identifier) { return identifier + ID_CONTAINER_CLASH_CHAR; } /** * Implements Section 1.3.2.9 of Java2IDL Mapping. Container in this * context means the name of the java Class(excluding package) in which * the identifier is defined. Comparison is case-insensitive. */ private static boolean identifierClashesWithContainer (String mappedContainerName, String identifier) { return identifier.equalsIgnoreCase(mappedContainerName); } /** * Returns Unicode mangling as defined in Section 1.3.2.4 of * Java2IDL spec. * * "For Java identifiers that contain illegal OMG IDL identifier * characters such as '$' or Unicode characters outside of ISO Latin 1, * any such illegal characters are replaced by "U" followed by the * 4 hexadecimal characters(in upper case) representing the Unicode * value. So, the Java name a$b is mapped to aU0024b and * x\u03bCy is mapped to xU03BCy." */ public static String charToUnicodeRepresentation(char c) { int orig = (int) c; StringBuffer hexString = new StringBuffer(); int value = orig; while( value > 0 ) { int div = value / 16; int mod = value % 16; hexString.insert(0, HEX_DIGITS[mod]); value = div; } int numZerosToAdd = 4 - hexString.length(); for(int i = 0; i < numZerosToAdd; i++) { hexString.insert(0, "0"); } hexString.insert(0, "U"); return hexString.toString(); } private static boolean isIDLIdentifier(String identifier) { boolean isIdentifier = true; for(int i = 0; i < identifier.length(); i++) { char nextChar = identifier.charAt(i); // 1st char must be alphbetic. isIdentifier = (i == 0) ? isIDLAlphabeticChar(nextChar) : isIDLIdentifierChar(nextChar); if( !isIdentifier ) { break; } } return isIdentifier;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?