J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.rmi.rmic.newrmic.jrmp; |
| 27 | |
| 28 | import com.sun.javadoc.ClassDoc; |
| 29 | import com.sun.javadoc.MethodDoc; |
| 30 | import com.sun.javadoc.Parameter; |
| 31 | import com.sun.javadoc.Type; |
| 32 | |
| 33 | /** |
| 34 | * Provides static utility methods. |
| 35 | * |
| 36 | * WARNING: The contents of this source file are not part of any |
| 37 | * supported API. Code that depends on them does so at its own risk: |
| 38 | * they are subject to change or removal without notice. |
| 39 | * |
| 40 | * @author Peter Jones |
| 41 | **/ |
| 42 | final class Util { |
| 43 | |
| 44 | private Util() { throw new AssertionError(); } |
| 45 | |
| 46 | /** |
| 47 | * Returns the binary name of the class or interface represented |
| 48 | * by the specified ClassDoc. |
| 49 | **/ |
| 50 | static String binaryNameOf(ClassDoc cl) { |
| 51 | String flat = cl.name().replace('.', '$'); |
| 52 | String packageName = cl.containingPackage().name(); |
| 53 | return packageName.equals("") ? flat : packageName + "." + flat; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the method descriptor for the specified method. |
| 58 | * |
| 59 | * See section 4.3.3 of The Java Virtual Machine Specification |
| 60 | * Second Edition for the definition of a "method descriptor". |
| 61 | **/ |
| 62 | static String methodDescriptorOf(MethodDoc method) { |
| 63 | String desc = "("; |
| 64 | Parameter[] parameters = method.parameters(); |
| 65 | for (int i = 0; i < parameters.length; i++) { |
| 66 | desc += typeDescriptorOf(parameters[i].type()); |
| 67 | } |
| 68 | desc += ")" + typeDescriptorOf(method.returnType()); |
| 69 | return desc; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns the descriptor for the specified type, as appropriate |
| 74 | * for either a parameter or return type in a method descriptor. |
| 75 | **/ |
| 76 | private static String typeDescriptorOf(Type type) { |
| 77 | String desc; |
| 78 | ClassDoc classDoc = type.asClassDoc(); |
| 79 | if (classDoc == null) { |
| 80 | /* |
| 81 | * Handle primitive types. |
| 82 | */ |
| 83 | String name = type.typeName(); |
| 84 | if (name.equals("boolean")) { |
| 85 | desc = "Z"; |
| 86 | } else if (name.equals("byte")) { |
| 87 | desc = "B"; |
| 88 | } else if (name.equals("char")) { |
| 89 | desc = "C"; |
| 90 | } else if (name.equals("short")) { |
| 91 | desc = "S"; |
| 92 | } else if (name.equals("int")) { |
| 93 | desc = "I"; |
| 94 | } else if (name.equals("long")) { |
| 95 | desc = "J"; |
| 96 | } else if (name.equals("float")) { |
| 97 | desc = "F"; |
| 98 | } else if (name.equals("double")) { |
| 99 | desc = "D"; |
| 100 | } else if (name.equals("void")) { |
| 101 | desc = "V"; |
| 102 | } else { |
| 103 | throw new AssertionError( |
| 104 | "unrecognized primitive type: " + name); |
| 105 | } |
| 106 | } else { |
| 107 | /* |
| 108 | * Handle non-array reference types. |
| 109 | */ |
| 110 | desc = "L" + binaryNameOf(classDoc).replace('.', '/') + ";"; |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Handle array types. |
| 115 | */ |
| 116 | int dimensions = type.dimension().length() / 2; |
| 117 | for (int i = 0; i < dimensions; i++) { |
| 118 | desc = "[" + desc; |
| 119 | } |
| 120 | |
| 121 | return desc; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns a reader-friendly string representation of the |
| 126 | * specified method's signature. Names of reference types are not |
| 127 | * package-qualified. |
| 128 | **/ |
| 129 | static String getFriendlyUnqualifiedSignature(MethodDoc method) { |
| 130 | String sig = method.name() + "("; |
| 131 | Parameter[] parameters = method.parameters(); |
| 132 | for (int i = 0; i < parameters.length; i++) { |
| 133 | if (i > 0) { |
| 134 | sig += ", "; |
| 135 | } |
| 136 | Type paramType = parameters[i].type(); |
| 137 | sig += paramType.typeName() + paramType.dimension(); |
| 138 | } |
| 139 | sig += ")"; |
| 140 | return sig; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Returns true if the specified type is void. |
| 145 | **/ |
| 146 | static boolean isVoid(Type type) { |
| 147 | return type.asClassDoc() == null && type.typeName().equals("void"); |
| 148 | } |
| 149 | } |