KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > reflect > SignatureUtil


1 /*
2  * Copyright (C) 2001 Mika Riekkinen, Joni Suominen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package alt.jiapi.reflect;
20
21 import java.lang.reflect.Method JavaDoc;
22
23 /**
24  * SignatureUtil converts simple names to/from java signature format.
25  *
26  * @author Mika Riekkinen
27  * @author Joni Suominen
28  * @version $Revision: 1.7 $ $Date: 2004/02/22 16:13:37 $
29  */

30 public class SignatureUtil {
31     /**
32      * Convert an array of simple names to an array of signatures.
33      * For example, <br>
34      * 'java.lang.Object' --> 'Ljava/lang/Object;'<br>
35      * 'java.lang.Object[]' --> '[Ljava/lang/Object;'<br>
36      * 'int' --> 'I'<br>
37      * 'int[]' --> '[I'
38      *
39      * @return Simple name converted to signature
40      */

41     public static String JavaDoc[] toSignature(String JavaDoc[] simpleNames) {
42         String JavaDoc[] signatures = new String JavaDoc[simpleNames.length];
43
44         for (int i = 0; i < signatures.length; i++) {
45             signatures[i] = toSignature(simpleNames[i]);
46         }
47
48         return signatures;
49     }
50
51
52     /**
53      * Generates a method signature from Java Reflection method.
54      *
55      * @param m a java.lang.reflect.Method
56      * @return a signature of given method
57      */

58     public static String JavaDoc getSignature(Method JavaDoc m) {
59         Class JavaDoc parameterClasses[] = m.getParameterTypes();
60         Class JavaDoc returnTypeClass = m.getReturnType();
61
62         String JavaDoc parameterNames[] = new String JavaDoc[parameterClasses.length];
63
64         for (int i = 0; i < parameterClasses.length; i++) {
65             parameterNames[i] = parameterClasses[i].getName();
66         }
67         
68         try {
69             return null;
70         } catch (ClassFormatError JavaDoc cfe) {
71             // Don't mind about this. It can never really happen...
72
}
73
74         return null;
75     }
76
77
78     /**
79      * Checks, whether a simple name given represents a primitive type.
80      * For example, <code>java.lang.Object</code> returns false, and
81      * <code>int</code> returns true.
82      *
83      * @param simpleName Simple name to check.
84      * @return true, if simpleName represents primitive type.
85      */

86     public static boolean isPrimitive(String JavaDoc simpleName) {
87         // First trivial check for fully qualified Object names.
88
if (simpleName.indexOf('.') != -1) {
89             return false;
90         }
91
92         if (simpleName.startsWith("int")) {
93             return true;
94         }
95         else if (simpleName.startsWith("long")) {
96             return true;
97         }
98         else if (simpleName.startsWith("char")) {
99             return true;
100         }
101         else if (simpleName.startsWith("boolean")) {
102             return true;
103         }
104         else if (simpleName.startsWith("byte")) {
105             return true;
106         }
107         else if (simpleName.startsWith("float")) {
108             return true;
109         }
110         else if (simpleName.startsWith("double")) {
111             return true;
112         }
113
114         return false;
115     }
116
117     /**
118      * Convert single simple name to signature.
119      */

120     public static String JavaDoc toSignature(String JavaDoc simpleName) {
121         StringBuffer JavaDoc signature = new StringBuffer JavaDoc();
122         boolean isArray = false;
123
124         // Check for array dimensions
125
for (int i = 0; i < simpleName.length(); i++) {
126             if (simpleName.charAt(i) == '[') {
127                 signature.append("[");
128                 isArray = true;
129             }
130         }
131         
132         if (simpleName.startsWith("int")) {
133             signature.append("I");
134         }
135         else if (simpleName.startsWith("long")) {
136             signature.append("J");
137         }
138         else if (simpleName.startsWith("char")) {
139             signature.append("C");
140         }
141         else if (simpleName.startsWith("boolean")) {
142             signature.append("Z");
143         }
144         else if (simpleName.startsWith("byte")) {
145             signature.append("B");
146         }
147         else if (simpleName.startsWith("float")) {
148             signature.append("F");
149         }
150         else if (simpleName.startsWith("double")) {
151             signature.append("D");
152         }
153         else if (simpleName.startsWith("void")) {
154             signature.append("V");
155         }
156         else {
157             // For Objects:
158
signature.append('L');
159             if (isArray) {
160                 simpleName =
161                     simpleName.substring(0, simpleName.indexOf('[')).trim();
162             }
163
164             signature.append(simpleName.replace('.', '/'));
165             signature.append(';');
166         }
167
168         return signature.toString();
169     }
170
171     /**
172      * Convert an array of signatures to an array of simple names.
173      * For example, <br>
174      * 'Ljava/lang/Object;' --> 'java.lang.Object'<br>
175      * '[Ljava/lang/Object;' --> 'java.lang.Object[]'<br>
176      * 'I' --> 'int'<br>
177      * '[I' --> 'int[]'
178      *
179      * @return signature converted to simple name
180      */

181     public static String JavaDoc[] toSimpleName(String JavaDoc[] signatures) {
182         String JavaDoc[] simpleNames = new String JavaDoc[signatures.length];
183
184         for (int i = 0; i < simpleNames.length; i++) {
185             simpleNames[i] = toSimpleName(signatures[i]);
186         }
187
188         return simpleNames;
189     }
190
191     /**
192      * Convert signature to simple name.
193      */

194     public static String JavaDoc toSimpleName(String JavaDoc signature) {
195         int index = signature.lastIndexOf('[');
196         String JavaDoc typeName = null;
197
198         if (index != -1) {
199             if (signature.lastIndexOf(';') != -1) {
200                 typeName = signature.substring(index + 1,
201                                                signature.length() - 1);
202             }
203             else {
204                 typeName = signature.substring(index + 1, signature.length());
205             }
206         }
207         else {
208             if (signature.lastIndexOf(';') != -1) {
209                 typeName = signature.substring(0, signature.length() - 1);
210             }
211             else {
212                 typeName = signature;
213             }
214         }
215
216         char ch = typeName.charAt(0);
217
218         switch (ch) {
219         case 'L':
220             typeName = typeName.substring(1);
221             typeName = typeName.replace('/', '.');
222             break;
223         case 'I':
224             typeName = "int";
225             break;
226         case 'J':
227             typeName = "long";
228             break;
229         case 'C':
230             typeName = "char";
231             break;
232         case 'Z':
233             typeName = "boolean";
234             break;
235         case 'B':
236             typeName = "byte";
237             break;
238         case 'F':
239             typeName = "float";
240             break;
241         case 'D':
242             typeName = "double";
243             break;
244         case 'V':
245             typeName = "void";
246             break;
247         default:
248             System.out.println("error: " + typeName);
249             break;
250         }
251
252         if (index != -1) {
253             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(typeName);
254
255             for (int i = 0; i <= index; i++) {
256                 sb.append("[]");
257             }
258
259             return sb.toString();
260         }
261
262         return typeName;
263     }
264
265     /**
266      * Creates a method signature from return type and parameter types.
267      * All types are in "simple name"-format. (e.g. "java.lang.Object []")
268      *
269      * @param returnType a return type of a method
270      * @param paramTypes method's parameter types
271      * @return a Java VM signature for a method
272      */

273     public static String JavaDoc toMethodSignature(String JavaDoc returnType,
274                                            String JavaDoc[] paramTypes) {
275
276         String JavaDoc signatureReturnType = toSignature(returnType);
277         String JavaDoc[] signatureParamTypes = toSignature(paramTypes);
278
279         StringBuffer JavaDoc signature = new StringBuffer JavaDoc("(");
280         for (int i = 0; i < signatureParamTypes.length; i++) {
281             signature.append(signatureParamTypes[i]);
282         }
283         
284         signature.append(")");
285         signature.append(signatureReturnType);
286
287         return signature.toString();
288     }
289 }
290
291
Popular Tags