001: /*
002: * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.rmi.rmic.newrmic.jrmp;
027:
028: import com.sun.javadoc.ClassDoc;
029: import com.sun.javadoc.MethodDoc;
030: import com.sun.javadoc.Parameter;
031: import com.sun.javadoc.Type;
032:
033: /**
034: * Provides static utility methods.
035: *
036: * WARNING: The contents of this source file are not part of any
037: * supported API. Code that depends on them does so at its own risk:
038: * they are subject to change or removal without notice.
039: *
040: * @version 1.9, 07/05/05
041: * @author Peter Jones
042: **/
043: final class Util {
044:
045: private Util() {
046: throw new AssertionError();
047: }
048:
049: /**
050: * Returns the binary name of the class or interface represented
051: * by the specified ClassDoc.
052: **/
053: static String binaryNameOf(ClassDoc cl) {
054: String flat = cl.name().replace('.', '$');
055: String packageName = cl.containingPackage().name();
056: return packageName.equals("") ? flat : packageName + "." + flat;
057: }
058:
059: /**
060: * Returns the method descriptor for the specified method.
061: *
062: * See section 4.3.3 of The Java Virtual Machine Specification
063: * Second Edition for the definition of a "method descriptor".
064: **/
065: static String methodDescriptorOf(MethodDoc method) {
066: String desc = "(";
067: Parameter[] parameters = method.parameters();
068: for (int i = 0; i < parameters.length; i++) {
069: desc += typeDescriptorOf(parameters[i].type());
070: }
071: desc += ")" + typeDescriptorOf(method.returnType());
072: return desc;
073: }
074:
075: /**
076: * Returns the descriptor for the specified type, as appropriate
077: * for either a parameter or return type in a method descriptor.
078: **/
079: private static String typeDescriptorOf(Type type) {
080: String desc;
081: ClassDoc classDoc = type.asClassDoc();
082: if (classDoc == null) {
083: /*
084: * Handle primitive types.
085: */
086: String name = type.typeName();
087: if (name.equals("boolean")) {
088: desc = "Z";
089: } else if (name.equals("byte")) {
090: desc = "B";
091: } else if (name.equals("char")) {
092: desc = "C";
093: } else if (name.equals("short")) {
094: desc = "S";
095: } else if (name.equals("int")) {
096: desc = "I";
097: } else if (name.equals("long")) {
098: desc = "J";
099: } else if (name.equals("float")) {
100: desc = "F";
101: } else if (name.equals("double")) {
102: desc = "D";
103: } else if (name.equals("void")) {
104: desc = "V";
105: } else {
106: throw new AssertionError(
107: "unrecognized primitive type: " + name);
108: }
109: } else {
110: /*
111: * Handle non-array reference types.
112: */
113: desc = "L" + binaryNameOf(classDoc).replace('.', '/') + ";";
114: }
115:
116: /*
117: * Handle array types.
118: */
119: int dimensions = type.dimension().length() / 2;
120: for (int i = 0; i < dimensions; i++) {
121: desc = "[" + desc;
122: }
123:
124: return desc;
125: }
126:
127: /**
128: * Returns a reader-friendly string representation of the
129: * specified method's signature. Names of reference types are not
130: * package-qualified.
131: **/
132: static String getFriendlyUnqualifiedSignature(MethodDoc method) {
133: String sig = method.name() + "(";
134: Parameter[] parameters = method.parameters();
135: for (int i = 0; i < parameters.length; i++) {
136: if (i > 0) {
137: sig += ", ";
138: }
139: Type paramType = parameters[i].type();
140: sig += paramType.typeName() + paramType.dimension();
141: }
142: sig += ")";
143: return sig;
144: }
145:
146: /**
147: * Returns true if the specified type is void.
148: **/
149: static boolean isVoid(Type type) {
150: return type.asClassDoc() == null
151: && type.typeName().equals("void");
152: }
153: }
|