KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bcel > classfile > Utility


1 /*
2  * Copyright 2000-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.bcel.classfile;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.CharArrayReader JavaDoc;
22 import java.io.CharArrayWriter JavaDoc;
23 import java.io.FilterReader JavaDoc;
24 import java.io.FilterWriter JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.PrintStream JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.io.Reader JavaDoc;
29 import java.io.Writer JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Locale JavaDoc;
33 import java.util.zip.GZIPInputStream JavaDoc;
34 import java.util.zip.GZIPOutputStream JavaDoc;
35 import org.apache.bcel.Constants;
36 import org.apache.bcel.util.ByteSequence;
37
38 /**
39  * Utility functions that do not really belong to any class in particular.
40  *
41  * @version $Id: Utility.java 386056 2006-03-15 11:31:56Z tcurdt $
42  * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
43  */

44 public abstract class Utility {
45
46     private static int unwrap( ThreadLocal JavaDoc tl ) {
47         return ((Integer JavaDoc) tl.get()).intValue();
48     }
49
50
51     private static void wrap( ThreadLocal JavaDoc tl, int value ) {
52         tl.set(new Integer JavaDoc(value));
53     }
54
55     private static ThreadLocal JavaDoc consumed_chars = new ThreadLocal JavaDoc() {
56
57         protected Object JavaDoc initialValue() {
58             return new Integer JavaDoc(0);
59         }
60     };/* How many chars have been consumed
61      * during parsing in signatureToString().
62      * Read by methodSignatureToString().
63      * Set by side effect,but only internally.
64      */

65     private static boolean wide = false; /* The `WIDE' instruction is used in the
66      * byte code to allow 16-bit wide indices
67      * for local variables. This opcode
68      * precedes an `ILOAD', e.g.. The opcode
69      * immediately following takes an extra
70      * byte which is combined with the
71      * following byte to form a
72      * 16-bit value.
73      */

74
75
76     /**
77      * Convert bit field of flags into string such as `static final'.
78      *
79      * @param access_flags Access flags
80      * @return String representation of flags
81      */

82     public static final String JavaDoc accessToString( int access_flags ) {
83         return accessToString(access_flags, false);
84     }
85
86
87     /**
88      * Convert bit field of flags into string such as `static final'.
89      *
90      * Special case: Classes compiled with new compilers and with the
91      * `ACC_SUPER' flag would be said to be "synchronized". This is
92      * because SUN used the same value for the flags `ACC_SUPER' and
93      * `ACC_SYNCHRONIZED'.
94      *
95      * @param access_flags Access flags
96      * @param for_class access flags are for class qualifiers ?
97      * @return String representation of flags
98      */

99     public static final String JavaDoc accessToString( int access_flags, boolean for_class ) {
100         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
101         int p = 0;
102         for (int i = 0; p < Constants.MAX_ACC_FLAG; i++) { // Loop through known flags
103
p = pow2(i);
104             if ((access_flags & p) != 0) {
105                 /* Special case: Classes compiled with new compilers and with the
106                  * `ACC_SUPER' flag would be said to be "synchronized". This is
107                  * because SUN used the same value for the flags `ACC_SUPER' and
108                  * `ACC_SYNCHRONIZED'.
109                  */

110                 if (for_class && ((p == Constants.ACC_SUPER) || (p == Constants.ACC_INTERFACE))) {
111                     continue;
112                 }
113                 buf.append(Constants.ACCESS_NAMES[i]).append(" ");
114             }
115         }
116         return buf.toString().trim();
117     }
118
119
120     /**
121      * @return "class" or "interface", depending on the ACC_INTERFACE flag
122      */

123     public static final String JavaDoc classOrInterface( int access_flags ) {
124         return ((access_flags & Constants.ACC_INTERFACE) != 0) ? "interface" : "class";
125     }
126
127
128     /**
129      * Disassemble a byte array of JVM byte codes starting from code line
130      * `index' and return the disassembled string representation. Decode only
131      * `num' opcodes (including their operands), use -1 if you want to
132      * decompile everything.
133      *
134      * @param code byte code array
135      * @param constant_pool Array of constants
136      * @param index offset in `code' array
137      * <EM>(number of opcodes, not bytes!)</EM>
138      * @param length number of opcodes to decompile, -1 for all
139      * @param verbose be verbose, e.g. print constant pool index
140      * @return String representation of byte codes
141      */

142     public static final String JavaDoc codeToString( byte[] code, ConstantPool constant_pool, int index,
143             int length, boolean verbose ) {
144         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(code.length * 20); // Should be sufficient
145
ByteSequence stream = new ByteSequence(code);
146         try {
147             for (int i = 0; i < index; i++) {
148                 codeToString(stream, constant_pool, verbose);
149             }
150             for (int i = 0; stream.available() > 0; i++) {
151                 if ((length < 0) || (i < length)) {
152                     String JavaDoc indices = fillup(stream.getIndex() + ":", 6, true, ' ');
153                     buf.append(indices).append(codeToString(stream, constant_pool, verbose))
154                             .append('\n');
155                 }
156             }
157         } catch (IOException JavaDoc e) {
158             System.out.println(buf.toString());
159             e.printStackTrace();
160             throw new ClassFormatException("Byte code error: " + e);
161         }
162         return buf.toString();
163     }
164
165
166     public static final String JavaDoc codeToString( byte[] code, ConstantPool constant_pool, int index,
167             int length ) {
168         return codeToString(code, constant_pool, index, length, true);
169     }
170
171
172     /**
173      * Disassemble a stream of byte codes and return the
174      * string representation.
175      *
176      * @param bytes stream of bytes
177      * @param constant_pool Array of constants
178      * @param verbose be verbose, e.g. print constant pool index
179      * @return String representation of byte code
180      */

181     public static final String JavaDoc codeToString( ByteSequence bytes, ConstantPool constant_pool,
182             boolean verbose ) throws IOException JavaDoc {
183         short opcode = (short) bytes.readUnsignedByte();
184         int default_offset = 0, low, high, npairs;
185         int index, vindex, constant;
186         int[] match, jump_table;
187         int no_pad_bytes = 0, offset;
188         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(Constants.OPCODE_NAMES[opcode]);
189         /* Special case: Skip (0-3) padding bytes, i.e., the
190          * following bytes are 4-byte-aligned
191          */

192         if ((opcode == Constants.TABLESWITCH) || (opcode == Constants.LOOKUPSWITCH)) {
193             int remainder = bytes.getIndex() % 4;
194             no_pad_bytes = (remainder == 0) ? 0 : 4 - remainder;
195             for (int i = 0; i < no_pad_bytes; i++) {
196                 byte b;
197                 if ((b = bytes.readByte()) != 0) {
198                     System.err.println("Warning: Padding byte != 0 in "
199                             + Constants.OPCODE_NAMES[opcode] + ":" + b);
200                 }
201             }
202             // Both cases have a field default_offset in common
203
default_offset = bytes.readInt();
204         }
205         switch (opcode) {
206             /* Table switch has variable length arguments.
207              */

208             case Constants.TABLESWITCH:
209                 low = bytes.readInt();
210                 high = bytes.readInt();
211                 offset = bytes.getIndex() - 12 - no_pad_bytes - 1;
212                 default_offset += offset;
213                 buf.append("\tdefault = ").append(default_offset).append(", low = ").append(low)
214                         .append(", high = ").append(high).append("(");
215                 jump_table = new int[high - low + 1];
216                 for (int i = 0; i < jump_table.length; i++) {
217                     jump_table[i] = offset + bytes.readInt();
218                     buf.append(jump_table[i]);
219                     if (i < jump_table.length - 1) {
220                         buf.append(", ");
221                     }
222                 }
223                 buf.append(")");
224                 break;
225             /* Lookup switch has variable length arguments.
226              */

227             case Constants.LOOKUPSWITCH: {
228                 npairs = bytes.readInt();
229                 offset = bytes.getIndex() - 8 - no_pad_bytes - 1;
230                 match = new int[npairs];
231                 jump_table = new int[npairs];
232                 default_offset += offset;
233                 buf.append("\tdefault = ").append(default_offset).append(", npairs = ").append(
234                         npairs).append(" (");
235                 for (int i = 0; i < npairs; i++) {
236                     match[i] = bytes.readInt();
237                     jump_table[i] = offset + bytes.readInt();
238                     buf.append("(").append(match[i]).append(", ").append(jump_table[i]).append(")");
239                     if (i < npairs - 1) {
240                         buf.append(", ");
241                     }
242                 }
243                 buf.append(")");
244             }
245                 break;
246             /* Two address bytes + offset from start of byte stream form the
247              * jump target
248              */

249             case Constants.GOTO:
250             case Constants.IFEQ:
251             case Constants.IFGE:
252             case Constants.IFGT:
253             case Constants.IFLE:
254             case Constants.IFLT:
255             case Constants.JSR:
256             case Constants.IFNE:
257             case Constants.IFNONNULL:
258             case Constants.IFNULL:
259             case Constants.IF_ACMPEQ:
260             case Constants.IF_ACMPNE:
261             case Constants.IF_ICMPEQ:
262             case Constants.IF_ICMPGE:
263             case Constants.IF_ICMPGT:
264             case Constants.IF_ICMPLE:
265             case Constants.IF_ICMPLT:
266             case Constants.IF_ICMPNE:
267                 buf.append("\t\t#").append((bytes.getIndex() - 1) + bytes.readShort());
268                 break;
269             /* 32-bit wide jumps
270              */

271             case Constants.GOTO_W:
272             case Constants.JSR_W:
273                 buf.append("\t\t#").append(((bytes.getIndex() - 1) + bytes.readInt()));
274                 break;
275             /* Index byte references local variable (register)
276              */

277             case Constants.ALOAD:
278             case Constants.ASTORE:
279             case Constants.DLOAD:
280             case Constants.DSTORE:
281             case Constants.FLOAD:
282             case Constants.FSTORE:
283             case Constants.ILOAD:
284             case Constants.ISTORE:
285             case Constants.LLOAD:
286             case Constants.LSTORE:
287             case Constants.RET:
288                 if (wide) {
289                     vindex = bytes.readUnsignedShort();
290                     wide = false; // Clear flag
291
} else {
292                     vindex = bytes.readUnsignedByte();
293                 }
294                 buf.append("\t\t%").append(vindex);
295                 break;
296             /*
297              * Remember wide byte which is used to form a 16-bit address in the
298              * following instruction. Relies on that the method is called again with
299              * the following opcode.
300              */

301             case Constants.WIDE:
302                 wide = true;
303                 buf.append("\t(wide)");
304                 break;
305             /* Array of basic type.
306              */

307             case Constants.NEWARRAY:
308                 buf.append("\t\t<").append(Constants.TYPE_NAMES[bytes.readByte()]).append(">");
309                 break;
310             /* Access object/class fields.
311              */

312             case Constants.GETFIELD:
313             case Constants.GETSTATIC:
314             case Constants.PUTFIELD:
315             case Constants.PUTSTATIC:
316                 index = bytes.readUnsignedShort();
317                 buf.append("\t\t").append(
318                         constant_pool.constantToString(index, Constants.CONSTANT_Fieldref)).append(
319                         (verbose ? " (" + index + ")" : ""));
320                 break;
321             /* Operands are references to classes in constant pool
322              */

323             case Constants.NEW:
324             case Constants.CHECKCAST:
325                 buf.append("\t");
326             case Constants.INSTANCEOF:
327                 index = bytes.readUnsignedShort();
328                 buf.append("\t<").append(
329                         constant_pool.constantToString(index, Constants.CONSTANT_Class))
330                         .append(">").append((verbose ? " (" + index + ")" : ""));
331                 break;
332             /* Operands are references to methods in constant pool
333              */

334             case Constants.INVOKESPECIAL:
335             case Constants.INVOKESTATIC:
336             case Constants.INVOKEVIRTUAL:
337                 index = bytes.readUnsignedShort();
338                 buf.append("\t").append(
339                         constant_pool.constantToString(index, Constants.CONSTANT_Methodref))
340                         .append((verbose ? " (" + index + ")" : ""));
341                 break;
342             case Constants.INVOKEINTERFACE:
343                 index = bytes.readUnsignedShort();
344                 int nargs = bytes.readUnsignedByte(); // historical, redundant
345
buf.append("\t").append(
346                         constant_pool
347                                 .constantToString(index, Constants.CONSTANT_InterfaceMethodref))
348                         .append(verbose ? " (" + index + ")\t" : "").append(nargs).append("\t")
349                         .append(bytes.readUnsignedByte()); // Last byte is a reserved space
350
break;
351             /* Operands are references to items in constant pool
352              */

353             case Constants.LDC_W:
354             case Constants.LDC2_W:
355                 index = bytes.readUnsignedShort();
356                 buf.append("\t\t").append(
357                         constant_pool.constantToString(index, constant_pool.getConstant(index)
358                                 .getTag())).append((verbose ? " (" + index + ")" : ""));
359                 break;
360             case Constants.LDC:
361                 index = bytes.readUnsignedByte();
362                 buf.append("\t\t").append(
363                         constant_pool.constantToString(index, constant_pool.getConstant(index)
364                                 .getTag())).append((verbose ? " (" + index + ")" : ""));
365                 break;
366             /* Array of references.
367              */

368             case Constants.ANEWARRAY:
369                 index = bytes.readUnsignedShort();
370                 buf.append("\t\t<").append(
371                         compactClassName(constant_pool.getConstantString(index,
372                                 Constants.CONSTANT_Class), false)).append(">").append(
373                         (verbose ? " (" + index + ")" : ""));
374                 break;
375             /* Multidimensional array of references.
376              */

377             case Constants.MULTIANEWARRAY: {
378                 index = bytes.readUnsignedShort();
379                 int dimensions = bytes.readUnsignedByte();
380                 buf.append("\t<").append(
381                         compactClassName(constant_pool.getConstantString(index,
382                                 Constants.CONSTANT_Class), false)).append(">\t").append(dimensions)
383                         .append((verbose ? " (" + index + ")" : ""));
384             }
385                 break;
386             /* Increment local variable.
387              */

388             case Constants.IINC:
389                 if (wide) {
390                     vindex = bytes.readUnsignedShort();
391                     constant = bytes.readShort();
392                     wide = false;
393                 } else {
394                     vindex = bytes.readUnsignedByte();
395                     constant = bytes.readByte();
396                 }
397                 buf.append("\t\t%").append(vindex).append("\t").append(constant);
398                 break;
399             default:
400                 if (Constants.NO_OF_OPERANDS[opcode] > 0) {
401                     for (int i = 0; i < Constants.TYPE_OF_OPERANDS[opcode].length; i++) {
402                         buf.append("\t\t");
403                         switch (Constants.TYPE_OF_OPERANDS[opcode][i]) {
404                             case Constants.T_BYTE:
405                                 buf.append(bytes.readByte());
406                                 break;
407                             case Constants.T_SHORT:
408                                 buf.append(bytes.readShort());
409                                 break;
410                             case Constants.T_INT:
411                                 buf.append(bytes.readInt());
412                                 break;
413                             default: // Never reached
414
System.err.println("Unreachable default case reached!");
415                                 System.exit(-1);
416                         }
417                     }
418                 }
419         }
420         return buf.toString();
421     }
422
423
424     public static final String JavaDoc codeToString( ByteSequence bytes, ConstantPool constant_pool )
425             throws IOException JavaDoc {
426         return codeToString(bytes, constant_pool, true);
427     }
428
429
430     /**
431      * Shorten long class names, <em>java/lang/String</em> becomes
432      * <em>String</em>.
433      *
434      * @param str The long class name
435      * @return Compacted class name
436      */

437     public static final String JavaDoc compactClassName( String JavaDoc str ) {
438         return compactClassName(str, true);
439     }
440
441
442     /**
443      * Shorten long class name <em>str</em>, i.e., chop off the <em>prefix</em>,
444      * if the
445      * class name starts with this string and the flag <em>chopit</em> is true.
446      * Slashes <em>/</em> are converted to dots <em>.</em>.
447      *
448      * @param str The long class name
449      * @param prefix The prefix the get rid off
450      * @param chopit Flag that determines whether chopping is executed or not
451      * @return Compacted class name
452      */

453     public static final String JavaDoc compactClassName( String JavaDoc str, String JavaDoc prefix, boolean chopit ) {
454         int len = prefix.length();
455         str = str.replace('/', '.'); // Is `/' on all systems, even DOS
456
if (chopit) {
457             // If string starts with `prefix' and contains no further dots
458
if (str.startsWith(prefix) && (str.substring(len).indexOf('.') == -1)) {
459                 str = str.substring(len);
460             }
461         }
462         return str;
463     }
464
465
466     /**
467      * Shorten long class names, <em>java/lang/String</em> becomes
468      * <em>java.lang.String</em>,
469      * e.g.. If <em>chopit</em> is <em>true</em> the prefix <em>java.lang</em>
470      * is also removed.
471      *
472      * @param str The long class name
473      * @param chopit Flag that determines whether chopping is executed or not
474      * @return Compacted class name
475      */

476     public static final String JavaDoc compactClassName( String JavaDoc str, boolean chopit ) {
477         return compactClassName(str, "java.lang.", chopit);
478     }
479
480
481     /**
482      * @return `flag' with bit `i' set to 1
483      */

484     public static final int setBit( int flag, int i ) {
485         return flag | pow2(i);
486     }
487
488
489     /**
490      * @return `flag' with bit `i' set to 0
491      */

492     public static final int clearBit( int flag, int i ) {
493         int bit = pow2(i);
494         return (flag & bit) == 0 ? flag : flag ^ bit;
495     }
496
497
498     /**
499      * @return true, if bit `i' in `flag' is set
500      */

501     public static final boolean isSet( int flag, int i ) {
502         return (flag & pow2(i)) != 0;
503     }
504
505
506     /**
507      * Converts string containing the method return and argument types
508      * to a byte code method signature.
509      *
510      * @param ret Return type of method
511      * @param argv Types of method arguments
512      * @return Byte code representation of method signature
513      */

514     public final static String JavaDoc methodTypeToSignature( String JavaDoc ret, String JavaDoc[] argv )
515             throws ClassFormatException {
516         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("(");
517         String JavaDoc str;
518         if (argv != null) {
519             for (int i = 0; i < argv.length; i++) {
520                 str = getSignature(argv[i]);
521                 if (str.endsWith("V")) {
522                     throw new ClassFormatException("Invalid type: " + argv[i]);
523                 }
524                 buf.append(str);
525             }
526         }
527         str = getSignature(ret);
528         buf.append(")").append(str);
529         return buf.toString();
530     }
531
532
533     /**
534      * @param signature Method signature
535      * @return Array of argument types
536      * @throws ClassFormatException
537      */

538     public static final String JavaDoc[] methodSignatureArgumentTypes( String JavaDoc signature )
539             throws ClassFormatException {
540         return methodSignatureArgumentTypes(signature, true);
541     }
542
543
544     /**
545      * @param signature Method signature
546      * @param chopit Shorten class names ?
547      * @return Array of argument types
548      * @throws ClassFormatException
549      */

550     public static final String JavaDoc[] methodSignatureArgumentTypes( String JavaDoc signature, boolean chopit )
551             throws ClassFormatException {
552         List JavaDoc vec = new ArrayList JavaDoc();
553         int index;
554         try { // Read all declarations between for `(' and `)'
555
if (signature.charAt(0) != '(') {
556                 throw new ClassFormatException("Invalid method signature: " + signature);
557             }
558             index = 1; // current string position
559
while (signature.charAt(index) != ')') {
560                 vec.add(signatureToString(signature.substring(index), chopit));
561                 //corrected concurrent private static field acess
562
index += unwrap(consumed_chars); // update position
563
}
564         } catch (StringIndexOutOfBoundsException JavaDoc e) { // Should never occur
565
throw new ClassFormatException("Invalid method signature: " + signature);
566         }
567         return (String JavaDoc[]) vec.toArray(new String JavaDoc[vec.size()]);
568     }
569
570
571     /**
572      * @param signature Method signature
573      * @return return type of method
574      * @throws ClassFormatException
575      */

576     public static final String JavaDoc methodSignatureReturnType( String JavaDoc signature )
577             throws ClassFormatException {
578         return methodSignatureReturnType(signature, true);
579     }
580
581
582     /**
583      * @param signature Method signature
584      * @param chopit Shorten class names ?
585      * @return return type of method
586      * @throws ClassFormatException
587      */

588     public static final String JavaDoc methodSignatureReturnType( String JavaDoc signature, boolean chopit )
589             throws ClassFormatException {
590         int index;
591         String JavaDoc type;
592         try {
593             // Read return type after `)'
594
index = signature.lastIndexOf(')') + 1;
595             type = signatureToString(signature.substring(index), chopit);
596         } catch (StringIndexOutOfBoundsException JavaDoc e) { // Should never occur
597
throw new ClassFormatException("Invalid method signature: " + signature);
598         }
599         return type;
600     }
601
602
603     /**
604      * Converts method signature to string with all class names compacted.
605      *
606      * @param signature to convert
607      * @param name of method
608      * @param access flags of method
609      * @return Human readable signature
610      */

611     public static final String JavaDoc methodSignatureToString( String JavaDoc signature, String JavaDoc name, String JavaDoc access ) {
612         return methodSignatureToString(signature, name, access, true);
613     }
614
615
616     public static final String JavaDoc methodSignatureToString( String JavaDoc signature, String JavaDoc name,
617             String JavaDoc access, boolean chopit ) {
618         return methodSignatureToString(signature, name, access, chopit, null);
619     }
620
621
622     /**
623      * A return­type signature represents the return value from a method.
624      * It is a series of bytes in the following grammar:
625      *
626      * <return_signature> ::= <field_type> | V
627      *
628      * The character V indicates that the method returns no value. Otherwise, the
629      * signature indicates the type of the return value.
630      * An argument signature represents an argument passed to a method:
631      *
632      * <argument_signature> ::= <field_type>
633      *
634      * A method signature represents the arguments that the method expects, and
635      * the value that it returns.
636      * <method_signature> ::= (<arguments_signature>) <return_signature>
637      * <arguments_signature>::= <argument_signature>*
638      *
639      * This method converts such a string into a Java type declaration like
640      * `void main(String[])' and throws a `ClassFormatException' when the parsed
641      * type is invalid.
642      *
643      * @param signature Method signature
644      * @param name Method name
645      * @param access Method access rights
646      * @return Java type declaration
647      * @throws ClassFormatException
648      */

649     public static final String JavaDoc methodSignatureToString( String JavaDoc signature, String JavaDoc name,
650             String JavaDoc access, boolean chopit, LocalVariableTable vars ) throws ClassFormatException {
651         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("(");
652         String JavaDoc type;
653         int index;
654         int var_index = (access.indexOf("static") >= 0) ? 0 : 1;
655         try { // Read all declarations between for `(' and `)'
656
if (signature.charAt(0) != '(') {
657                 throw new ClassFormatException("Invalid method signature: " + signature);
658             }
659             index = 1; // current string position
660
while (signature.charAt(index) != ')') {
661                 String JavaDoc param_type = signatureToString(signature.substring(index), chopit);
662                 buf.append(param_type);
663                 if (vars != null) {
664                     LocalVariable l = vars.getLocalVariable(var_index);
665                     if (l != null) {
666                         buf.append(" ").append(l.getName());
667                     }
668                 } else {
669                     buf.append(" arg").append(var_index);
670                 }
671                 if ("double".equals(param_type) || "long".equals(param_type)) {
672                     var_index += 2;
673                 } else {
674                     var_index++;
675                 }
676                 buf.append(", ");
677                 //corrected concurrent private static field acess
678
index += unwrap(consumed_chars); // update position
679
}
680             index++; // update position
681
// Read return type after `)'
682
type = signatureToString(signature.substring(index), chopit);
683         } catch (StringIndexOutOfBoundsException JavaDoc e) { // Should never occur
684
throw new ClassFormatException("Invalid method signature: " + signature);
685         }
686         if (buf.length() > 1) {
687             buf.setLength(buf.length() - 2);
688         }
689         buf.append(")");
690         return access + ((access.length() > 0) ? " " : "") + // May be an empty string
691
type + " " + name + buf.toString();
692     }
693
694
695     // Guess what this does
696
private static final int pow2( int n ) {
697         return 1 << n;
698     }
699
700
701     /**
702      * Replace all occurences of <em>old</em> in <em>str</em> with <em>new</em>.
703      *
704      * @param str String to permute
705      * @param old String to be replaced
706      * @param new_ Replacement string
707      * @return new String object
708      */

709     public static final String JavaDoc replace( String JavaDoc str, String JavaDoc old, String JavaDoc new_ ) {
710         int index, old_index;
711         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
712         try {
713             if ((index = str.indexOf(old)) != -1) { // `old' found in str
714
old_index = 0; // String start offset
715
// While we have something to replace
716
while ((index = str.indexOf(old, old_index)) != -1) {
717                     buf.append(str.substring(old_index, index)); // append prefix
718
buf.append(new_); // append replacement
719
old_index = index + old.length(); // Skip `old'.length chars
720
}
721                 buf.append(str.substring(old_index)); // append rest of string
722
str = buf.toString();
723             }
724         } catch (StringIndexOutOfBoundsException JavaDoc e) {