001: /*
002: * Copyright 2002-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 com.sun.tools.javah;
027:
028: import com.sun.javadoc.*;
029: import java.io.*;
030: import java.util.*;
031:
032: /**
033: * Returns internal type signature.
034: *
035: * @author Sucheta Dambalkar
036: */
037:
038: public class TypeSignature {
039:
040: RootDoc root = null;
041:
042: /* Signature Characters */
043:
044: private static final String SIG_VOID = "V";
045: private static final String SIG_BOOLEAN = "Z";
046: private static final String SIG_BYTE = "B";
047: private static final String SIG_CHAR = "C";
048: private static final String SIG_SHORT = "S";
049: private static final String SIG_INT = "I";
050: private static final String SIG_LONG = "J";
051: private static final String SIG_FLOAT = "F";
052: private static final String SIG_DOUBLE = "D";
053: private static final String SIG_ARRAY = "[";
054: private static final String SIG_CLASS = "L";
055:
056: public TypeSignature(RootDoc root) {
057: this .root = root;
058: }
059:
060: /*
061: * Returns the type signature of a field according to JVM specs
062: */
063: public String getTypeSignature(String javasignature) {
064: return getParamJVMSignature(javasignature);
065: }
066:
067: /*
068: * Returns the type signature of a method according to JVM specs
069: */
070: public String getTypeSignature(String javasignature, Type returnType) {
071:
072: String signature = null; //Java type signature.
073: String typeSignature = null; //Internal type signature.
074: Vector params = new Vector(); //List of parameters.
075: String paramsig = null; //Java parameter signature.
076: String paramJVMSig = null; //Internal parameter signature.
077: String returnSig = null; //Java return type signature.
078: String returnJVMType = null; //Internal return type signature.
079: String dimension = null; //Array dimension.
080:
081: int startIndex = -1;
082: int endIndex = -1;
083: StringTokenizer st = null;
084: int i = 0;
085:
086: // Gets the actual java signature without parentheses.
087: if (javasignature != null) {
088: startIndex = javasignature.indexOf("(");
089: endIndex = javasignature.indexOf(")");
090: }
091:
092: if (((startIndex != -1) && (endIndex != -1))
093: && (startIndex + 1 < javasignature.length())
094: && (endIndex < javasignature.length())) {
095:
096: signature = javasignature.substring(startIndex + 1,
097: endIndex);
098: }
099:
100: // Separates parameters.
101: if (signature != null) {
102: if (signature.indexOf(",") != -1) {
103: st = new StringTokenizer(signature, ",");
104: if (st != null) {
105: while (st.hasMoreTokens()) {
106: params.add(st.nextToken());
107: }
108: }
109: } else {
110: params.add(signature);
111: }
112: }
113:
114: /* JVM type signature. */
115: typeSignature = "(";
116:
117: // Gets indivisual internal parameter signature.
118: while (params.isEmpty() != true) {
119: paramsig = ((String) params.remove(i)).trim();
120: paramJVMSig = getParamJVMSignature(paramsig);
121: if (paramJVMSig != null) {
122: typeSignature += paramJVMSig;
123: }
124: }
125:
126: typeSignature += ")";
127:
128: // Get internal return type signature.
129:
130: returnJVMType = "";
131: if (returnType != null) {
132: dimension = returnType.dimension();
133: }
134:
135: if (dimension != null) {
136:
137: //Gets array dimension of return type.
138: while (dimension.indexOf("[]") != -1) {
139: returnJVMType += "[";
140: int stindex = dimension.indexOf("]") + 1;
141: if (stindex <= dimension.length()) {
142: dimension = dimension.substring(stindex);
143: } else
144: dimension = "";
145: }
146: }
147: if (returnType != null) {
148: returnSig = returnType.qualifiedTypeName();
149: returnJVMType += getComponentType(returnSig);
150: } else {
151: System.out.println("Invalid return type.");
152: }
153:
154: typeSignature += returnJVMType;
155: return typeSignature;
156: }
157:
158: /*
159: * Returns internal signature of a parameter.
160: */
161: private String getParamJVMSignature(String paramsig) {
162: String paramJVMSig = "";
163: String componentType = "";
164:
165: if (paramsig != null) {
166:
167: if (paramsig.indexOf("[]") != -1) {
168: // Gets array dimension.
169: int endindex = paramsig.indexOf("[]");
170: componentType = paramsig.substring(0, endindex);
171: String dimensionString = paramsig.substring(endindex);
172: if (dimensionString != null) {
173: while (dimensionString.indexOf("[]") != -1) {
174: paramJVMSig += "[";
175: int beginindex = dimensionString.indexOf("]") + 1;
176: if (beginindex < dimensionString.length()) {
177: dimensionString = dimensionString
178: .substring(beginindex);
179: } else
180: dimensionString = "";
181: }
182: }
183: } else
184: componentType = paramsig;
185:
186: paramJVMSig += getComponentType(componentType);
187: }
188: return paramJVMSig;
189: }
190:
191: /*
192: * Returns internal signature of a component.
193: */
194: private String getComponentType(String componentType) {
195:
196: String JVMSig = "";
197:
198: if (componentType != null) {
199: if (componentType.equals("void"))
200: JVMSig += SIG_VOID;
201: else if (componentType.equals("boolean"))
202: JVMSig += SIG_BOOLEAN;
203: else if (componentType.equals("byte"))
204: JVMSig += SIG_BYTE;
205: else if (componentType.equals("char"))
206: JVMSig += SIG_CHAR;
207: else if (componentType.equals("short"))
208: JVMSig += SIG_SHORT;
209: else if (componentType.equals("int"))
210: JVMSig += SIG_INT;
211: else if (componentType.equals("long"))
212: JVMSig += SIG_LONG;
213: else if (componentType.equals("float"))
214: JVMSig += SIG_FLOAT;
215: else if (componentType.equals("double"))
216: JVMSig += SIG_DOUBLE;
217: else {
218: if (!componentType.equals("")) {
219: ClassDoc classNameDoc = root
220: .classNamed(componentType);
221:
222: if (classNameDoc == null) {
223: System.out.println("Invalid class type");
224: } else {
225: String classname = classNameDoc.qualifiedName();
226: String newclassname = classname.replace('.',
227: '/');
228: JVMSig += "L";
229: JVMSig += newclassname;
230: JVMSig += ";";
231: }
232: }
233: }
234: }
235: return JVMSig;
236: }
237: }
|