KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > eval > ast > instructions > RuntimeSignature


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.eval.ast.instructions;
12
13
14 import org.eclipse.jdt.core.compiler.CharOperation;
15
16 /**
17  * Copy of org.eclipse.jdt.core.Signature. The class is copied here
18  * solely for the purpose of commenting out the line:
19  * CharOperation.replace(result, C_DOLLAR, C_DOT);
20  * in the method toCharArray(char[]).
21  * See Bug 22165
22  */

23 public class RuntimeSignature {
24     public static final char C_BOOLEAN = 'Z';
25     public static final char C_BYTE = 'B';
26     public static final char C_CHAR = 'C';
27     public static final char C_DOUBLE = 'D';
28     public static final char C_FLOAT = 'F';
29     public static final char C_INT = 'I';
30     public static final char C_SEMICOLON = ';';
31     public static final char C_LONG = 'J';
32     public static final char C_SHORT = 'S';
33     public static final char C_VOID = 'V';
34     public static final char C_DOT = '.';
35     public static final char C_DOLLAR = '$';
36     public static final char C_ARRAY = '[';
37     public static final char C_RESOLVED = 'L';
38     public static final char C_UNRESOLVED = 'Q';
39     public static final char C_NAME_END = ';';
40     public static final char C_PARAM_START = '(';
41     public static final char C_PARAM_END = ')';
42     public static final String JavaDoc SIG_BOOLEAN = "Z"; //$NON-NLS-1$
43
public static final String JavaDoc SIG_BYTE = "B"; //$NON-NLS-1$
44
public static final String JavaDoc SIG_CHAR = "C"; //$NON-NLS-1$
45
public static final String JavaDoc SIG_DOUBLE = "D"; //$NON-NLS-1$
46
public static final String JavaDoc SIG_FLOAT = "F"; //$NON-NLS-1$
47
public static final String JavaDoc SIG_INT = "I"; //$NON-NLS-1$
48
public static final String JavaDoc SIG_LONG = "J"; //$NON-NLS-1$
49
public static final String JavaDoc SIG_SHORT = "S"; //$NON-NLS-1$
50
public static final String JavaDoc SIG_VOID = "V"; //$NON-NLS-1$
51
private static final char[] NO_CHAR = new char[0];
52     private static final char[] BOOLEAN = { 'b', 'o', 'o', 'l', 'e', 'a', 'n' };
53     private static final char[] BYTE = { 'b', 'y', 't', 'e' };
54     private static final char[] CHAR = { 'c', 'h', 'a', 'r' };
55     private static final char[] DOUBLE = { 'd', 'o', 'u', 'b', 'l', 'e' };
56     private static final char[] FLOAT = { 'f', 'l', 'o', 'a', 't' };
57     private static final char[] INT = { 'i', 'n', 't' };
58     private static final char[] LONG = { 'l', 'o', 'n', 'g' };
59     private static final char[] SHORT = { 's', 'h', 'o', 'r', 't' };
60     private static final char[] VOID = { 'v', 'o', 'i', 'd' };
61
62     public static String JavaDoc toString(String JavaDoc signature)
63         throws IllegalArgumentException JavaDoc {
64         return new String JavaDoc(toCharArray(signature.toCharArray()));
65     }
66
67     public static char[] toCharArray(char[] signature)
68         throws IllegalArgumentException JavaDoc {
69         try {
70             int sigLength = signature.length;
71
72             if (sigLength == 0 || signature[0] == C_PARAM_START) {
73                 return toCharArray(signature, NO_CHAR, null, true, true);
74             }
75
76             // compute result length
77
int resultLength = 0;
78             int index = -1;
79             while (signature[++index] == C_ARRAY) {
80                 resultLength += 2; // []
81
}
82             switch (signature[index]) {
83                 case C_BOOLEAN :
84                     resultLength += BOOLEAN.length;
85                     break;
86                 case C_BYTE :
87                     resultLength += BYTE.length;
88                     break;
89                 case C_CHAR :
90                     resultLength += CHAR.length;
91                     break;
92                 case C_DOUBLE :
93                     resultLength += DOUBLE.length;
94                     break;
95                 case C_FLOAT :
96                     resultLength += FLOAT.length;
97                     break;
98                 case C_INT :
99                     resultLength += INT.length;
100                     break;
101                 case C_LONG :
102                     resultLength += LONG.length;
103                     break;
104                 case C_SHORT :
105                     resultLength += SHORT.length;
106                     break;
107                 case C_VOID :
108                     resultLength += VOID.length;
109                     break;
110                 case C_RESOLVED :
111                 case C_UNRESOLVED :
112                     int end =
113                         CharOperation.indexOf(C_SEMICOLON, signature, index);
114                     if (end == -1)
115                         throw new IllegalArgumentException JavaDoc();
116                     int start = index + 1;
117                     resultLength += end - start;
118                     break;
119                 default :
120                     throw new IllegalArgumentException JavaDoc();
121             }
122
123             char[] result = new char[resultLength];
124             copyType(signature, 0, result, 0, true);
125
126             /**
127              * Converts '$' separated type signatures into '.' separated type signature.
128              * NOTE: This assumes that the type signature is an inner type signature.
129              * This is true in most cases, but someone can define a non-inner type
130              * name containing a '$'. However to tell the difference, we would have
131              * to resolve the signature, which cannot be done at this point.
132              */

133 // CharOperation.replace(result, C_DOLLAR, C_DOT);
134

135             return result;
136         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
137             throw new IllegalArgumentException JavaDoc();
138         }
139     }
140
141     public static char[] toCharArray(
142         char[] methodSignature,
143         char[] methodName,
144         char[][] parameterNames,
145         boolean fullyQualifyTypeNames,
146         boolean includeReturnType) {
147         try {
148             int firstParen =
149                 CharOperation.indexOf(C_PARAM_START, methodSignature);
150             if (firstParen == -1)
151                 throw new IllegalArgumentException JavaDoc();
152
153             int sigLength = methodSignature.length;
154
155             // compute result length
156

157             // method signature
158
int paramCount = 0;
159             int lastParen = -1;
160             int resultLength = 0;
161             signature : for (int i = firstParen; i < sigLength; i++) {
162                 switch (methodSignature[i]) {
163                     case C_ARRAY :
164                         resultLength += 2; // []
165
continue signature;
166                     case C_BOOLEAN :
167                         resultLength += BOOLEAN.length;
168                         break;
169                     case C_BYTE :
170                         resultLength += BYTE.length;
171                         break;
172                     case C_CHAR :
173                         resultLength += CHAR.length;
174                         break;
175                     case C_DOUBLE :
176                         resultLength += DOUBLE.length;
177                         break;
178                     case C_FLOAT :
179                         resultLength += FLOAT.length;
180                         break;
181                     case C_INT :
182                         resultLength += INT.length;
183                         break;
184                     case C_LONG :
185                         resultLength += LONG.length;
186                         break;
187                     case C_SHORT :
188                         resultLength += SHORT.length;
189                         break;
190                     case C_VOID :
191                         resultLength += VOID.length;
192                         break;
193                     case C_RESOLVED :
194                     case C_UNRESOLVED :
195                         int end =
196                             CharOperation.indexOf(
197                                 C_SEMICOLON,
198                                 methodSignature,
199                                 i);
200                         if (end == -1)
201                             throw new IllegalArgumentException JavaDoc();
202                         int start;
203                         if (fullyQualifyTypeNames) {
204                             start = i + 1;
205                         } else {
206                             start =
207                                 CharOperation.lastIndexOf(
208                                     C_DOT,
209                                     methodSignature,
210                                     i,
211                                     end)
212                                     + 1;
213                             if (start == 0)
214                                 start = i + 1;
215                         }
216                         resultLength += end - start;
217                         i = end;
218                         break;
219                     case C_PARAM_START :
220                         // add space for "("
221
resultLength++;
222                         continue signature;
223                     case C_PARAM_END :
224                         lastParen = i;
225                         if (includeReturnType) {
226                             if (paramCount > 0) {
227                                 // remove space for ", " that was added with last parameter and remove space that is going to be added for ", " after return type
228
// and add space for ") "
229
resultLength -= 2;
230                             } //else
231
// remove space that is going to be added for ", " after return type
232
// and add space for ") "
233
// -> noop
234

235                             // decrement param count because it is going to be added for return type
236
paramCount--;
237                             continue signature;
238                         }
239                         if (paramCount > 0) {
240                             // remove space for ", " that was added with last parameter and add space for ")"
241
resultLength--;
242                         } else {
243                             // add space for ")"
244
resultLength++;
245                         }
246                         break signature;
247                     default :
248                         throw new IllegalArgumentException JavaDoc();
249                 }
250                 resultLength += 2; // add space for ", "
251
paramCount++;
252             }
253
254             // parameter names
255
int parameterNamesLength =
256                 parameterNames == null ? 0 : parameterNames.length;
257             for (int i = 0; i < parameterNamesLength; i++) {
258                 resultLength += parameterNames[i].length + 1;
259                 // parameter name + space
260
}
261
262             // selector
263
int selectorLength = methodName == null ? 0 : methodName.length;
264             resultLength += selectorLength;
265
266             // create resulting char array
267
char[] result = new char[resultLength];
268
269             // returned type
270
int index = 0;
271             if (includeReturnType) {
272                 long pos =
273                     copyType(
274                         methodSignature,
275                         lastParen + 1,
276                         result,
277                         index,
278                         fullyQualifyTypeNames);
279                 index = (int) (pos >>> 32);
280                 result[index++] = ' ';
281             }
282
283             // selector
284
if (methodName != null) {
285                 System.arraycopy(methodName, 0, result, index, selectorLength);
286                 index += selectorLength;
287             }
288
289             // parameters
290
result[index++] = C_PARAM_START;
291             int sigPos = firstParen + 1;
292             for (int i = 0; i < paramCount; i++) {
293                 long pos =
294                     copyType(
295                         methodSignature,
296                         sigPos,
297                         result,
298                         index,
299                         fullyQualifyTypeNames);
300                 index = (int) (pos >>> 32);
301                 sigPos = (int) pos;
302                 if (parameterNames != null) {
303                     result[index++] = ' ';
304                     char[] parameterName = parameterNames[i];
305                     int paramLength = parameterName.length;
306                     System.arraycopy(
307                         parameterName,
308                         0,
309                         result,
310                         index,
311                         paramLength);
312                     index += paramLength;
313                 }
314                 if (i != paramCount - 1) {
315                     result[index++] = ',';
316                     result[index++] = ' ';
317                 }
318             }
319             if (sigPos >= sigLength) {
320                 throw new IllegalArgumentException JavaDoc();
321                 // should be on last paren
322
}
323             result[index++] = C_PARAM_END;
324
325             return result;
326         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
327             throw new IllegalArgumentException JavaDoc();
328         }
329     }
330
331     private static long copyType(
332         char[] signature,
333         int sigPos,
334         char[] dest,
335         int index,
336         boolean fullyQualifyTypeNames) {
337         int arrayCount = 0;
338         loop : while (true) {
339             switch (signature[sigPos++]) {
340                 case C_ARRAY :
341                     arrayCount++;
342                     break;
343                 case C_BOOLEAN :
344                     int length = BOOLEAN.length;
345                     System.arraycopy(BOOLEAN, 0, dest, index, length);
346                     index += length;
347                     break loop;
348                 case C_BYTE :
349                     length = BYTE.length;
350                     System.arraycopy(BYTE, 0, dest, index, length);
351                     index += length;
352                     break loop;
353                 case C_CHAR :
354                     length = CHAR.length;
355                     System.arraycopy(CHAR, 0, dest, index, length);
356                     index += length;
357                     break loop;
358                 case C_DOUBLE :
359                     length = DOUBLE.length;
360                     System.arraycopy(DOUBLE, 0, dest, index, length);
361                     index += length;
362                     break loop;
363                 case C_FLOAT :
364                     length = FLOAT.length;
365                     System.arraycopy(FLOAT, 0, dest, index, length);
366                     index += length;
367                     break loop;
368                 case C_INT :
369                     length = INT.length;
370                     System.arraycopy(INT, 0, dest, index, length);
371                     index += length;
372                     break loop;
373                 case C_LONG :
374                     length = LONG.length;
375                     System.arraycopy(LONG, 0, dest, index, length);
376                     index += length;
377                     break loop;
378                 case C_SHORT :
379                     length = SHORT.length;
380                     System.arraycopy(SHORT, 0, dest, index, length);
381                     index += length;
382                     break loop;
383                 case C_VOID :
384                     length = VOID.length;
385                     System.arraycopy(VOID, 0, dest, index, length);
386                     index += length;
387                     break loop;
388                 case C_RESOLVED :
389                 case C_UNRESOLVED :
390                     int end =
391                         CharOperation.indexOf(C_SEMICOLON, signature, sigPos);
392                     if (end == -1)
393                         throw new IllegalArgumentException JavaDoc();
394                     int start;
395                     if (fullyQualifyTypeNames) {
396                         start = sigPos;
397                     } else {
398                         start =
399                             CharOperation.lastIndexOf(
400                                 C_DOT,
401                                 signature,
402                                 sigPos,
403                                 end)
404                                 + 1;
405                         if (start == 0)
406                             start = sigPos;
407                     }
408                     length = end - start;
409                     System.arraycopy(signature, start, dest, index, length);
410                     sigPos = end + 1;
411                     index += length;
412                     break loop;
413             }
414         }
415         while (arrayCount-- > 0) {
416             dest[index++] = '[';
417             dest[index++] = ']';
418         }
419         return (((long) index) << 32) + sigPos;
420     }
421 }
422
Popular Tags