aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/javassist/bytecode/SignatureAttribute.java
blob: 0e05f24e8a225347713ea54c05b229adb7e528ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
/*
 * Javassist, a Java-bytecode translator toolkit.
 * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License.  Alternatively, the contents of this file may be used under
 * the terms of the GNU Lesser General Public License Version 2.1 or later,
 * or the Apache License Version 2.0.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 */

package javassist.bytecode;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.ArrayList;
import javassist.CtClass;

/**
 * <code>Signature_attribute</code>.
 */
public class SignatureAttribute extends AttributeInfo {
    /**
     * The name of this attribute <code>"Signature"</code>.
     */
    public static final String tag = "Signature";

    SignatureAttribute(ConstPool cp, int n, DataInputStream in)
        throws IOException
    {
        super(cp, n, in);
    }

    /**
     * Constructs a <code>Signature</code> attribute.
     *
     * @param cp                a constant pool table.
     * @param signature         the signature represented by this attribute.
     */
    public SignatureAttribute(ConstPool cp, String signature) {
        super(cp, tag);
        int index = cp.addUtf8Info(signature);
        byte[] bvalue = new byte[2];
        bvalue[0] = (byte)(index >>> 8);
        bvalue[1] = (byte)index;
        set(bvalue);
    }

    /**
     * Returns the generic signature indicated by <code>signature_index</code>.
     *
     * @see #toClassSignature(String)
     * @see #toMethodSignature(String)
     * @see #toFieldSignature(String)
     */
    public String getSignature() {
        return getConstPool().getUtf8Info(ByteArray.readU16bit(get(), 0));
    }

    /**
     * Sets <code>signature_index</code> to the index of the given generic signature,
     * which is added to a constant pool.
     *
     * @param sig       new signature.
     * @since 3.11
     */
    public void setSignature(String sig) {
        int index = getConstPool().addUtf8Info(sig);
        ByteArray.write16bit(index, info, 0);
    }

    /**
     * Makes a copy.  Class names are replaced according to the
     * given <code>Map</code> object.
     *
     * @param newCp     the constant pool table used by the new copy.
     * @param classnames        pairs of replaced and substituted
     *                          class names.
     */
    public AttributeInfo copy(ConstPool newCp, Map classnames) {
        return new SignatureAttribute(newCp, getSignature());
    }

    void renameClass(String oldname, String newname) {
        String sig = renameClass(getSignature(), oldname, newname);
        setSignature(sig);
    }

    void renameClass(Map classnames) {
        String sig = renameClass(getSignature(), classnames);
        setSignature(sig);
    }

    static String renameClass(String desc, String oldname, String newname) {
        Map map = new java.util.HashMap();
        map.put(oldname, newname);
        return renameClass(desc, map);
    }

    static String renameClass(String desc, Map map) {
        if (map == null)
            return desc;

        StringBuilder newdesc = new StringBuilder();
        int head = 0;
        int i = 0;
        for (;;) {
            int j = desc.indexOf('L', i);
            if (j < 0)
                break;

            StringBuilder nameBuf = new StringBuilder();
            int k = j;
            char c;
            try {
                while ((c = desc.charAt(++k)) != ';') {
                    nameBuf.append(c);
                    if (c == '<') {
                        while ((c = desc.charAt(++k)) != '>')
                            nameBuf.append(c);

                        nameBuf.append(c);
                    }
                }
            }
            catch (IndexOutOfBoundsException e) { break; }
            i = k + 1;
            String name = nameBuf.toString();
            String name2 = (String)map.get(name);
            if (name2 != null) {
                newdesc.append(desc.substring(head, j));
                newdesc.append('L');
                newdesc.append(name2);
                newdesc.append(c);
                head = i;
            }
        }

        if (head == 0)
            return desc;
        else {
            int len = desc.length();
            if (head < len)
                newdesc.append(desc.substring(head, len));

            return newdesc.toString();
        }
    }

    private static boolean isNamePart(int c) {
        return c != ';' && c != '<';
    }

    static private class Cursor {
        int position = 0;

        int indexOf(String s, int ch) throws BadBytecode {
            int i = s.indexOf(ch, position);
            if (i < 0)
                throw error(s);
            else {
                position = i + 1;
                return i;
            }
        }
    }

    /**
     * Class signature.
     */
    public static class ClassSignature {
        TypeParameter[] params;
        ClassType superClass;
        ClassType[] interfaces;

        /**
         * Constructs a class signature.
         *
         * @param params             type parameters.
         * @param superClass         the super class.
         * @param interfaces         the interface types.
         */
        public ClassSignature(TypeParameter[] params, ClassType superClass, ClassType[] interfaces) {
            this.params = params == null ? new TypeParameter[0] : params;
            this.superClass = superClass == null ? ClassType.OBJECT : superClass;
            this.interfaces = interfaces == null ? new ClassType[0] : interfaces;
        }

        /**
         * Constructs a class signature.
         *
         * @param p         type parameters.
         */
        public ClassSignature(TypeParameter[] p) {
            this(p, null, null);
        }

        /**
         * Returns the type parameters.
         *
         * @return a zero-length array if the type parameters are not specified.
         */
        public TypeParameter[] getParameters() {
            return params;
        }

        /**
         * Returns the super class.
         */
        public ClassType getSuperClass() { return superClass; }

        /**
         * Returns the super interfaces.
         *
         * @return a zero-length array if the super interfaces are not specified.
         */
        public ClassType[] getInterfaces() { return interfaces; }

        /**
         * Returns the string representation.
         */
        public String toString() {
            StringBuffer sbuf = new StringBuffer();

            TypeParameter.toString(sbuf, params);
            sbuf.append(" extends ").append(superClass);
            if (interfaces.length > 0) {
                sbuf.append(" implements ");
                Type.toString(sbuf, interfaces);
            }

            return sbuf.toString();
        }

        /**
         * Returns the encoded string representing the method type signature.
         */
        public String encode() {
            StringBuffer sbuf = new StringBuffer();
            if (params.length > 0) {
                sbuf.append('<');
                for (int i = 0; i < params.length; i++)
                    params[i].encode(sbuf);

                sbuf.append('>');
            }

            superClass.encode(sbuf);
            for (int i = 0; i < interfaces.length; i++)
                interfaces[i].encode(sbuf);

            return sbuf.toString();
        }
    }

    /**
     * Method type signature.
     */
    public static class MethodSignature {
        TypeParameter[] typeParams;
        Type[] params;
        Type retType;
        ObjectType[] exceptions;

        /**
         * Constructs a method type signature.  Any parameter can be null
         * to represent <code>void</code> or nothing.
         *
         * @param tp        type parameters.
         * @param params    parameter types.
         * @param ret       a return type, or null if the return type is <code>void</code>.
         * @param ex        exception types.
         */
        public MethodSignature(TypeParameter[] tp, Type[] params, Type ret, ObjectType[] ex) {
            typeParams = tp == null ? new TypeParameter[0] : tp;
            this.params = params == null ? new Type[0] : params;
            retType = ret == null ? new BaseType("void") : ret;
            exceptions = ex == null ? new ObjectType[0] : ex;
        }

        /**
         * Returns the formal type parameters.
         *
         * @return a zero-length array if the type parameters are not specified.
         */
        public TypeParameter[] getTypeParameters() { return typeParams; }

        /**
         * Returns the types of the formal parameters.
         *
         * @return a zero-length array if no formal parameter is taken.
         */
        public Type[] getParameterTypes() { return params; }

        /**
         * Returns the type of the returned value.
         */
        public Type getReturnType() { return retType; }

        /**
         * Returns the types of the exceptions that may be thrown.
         *
         * @return a zero-length array if exceptions are never thrown or
         * the exception types are not parameterized types or type variables.
         */
        public ObjectType[] getExceptionTypes() { return exceptions; }

        /**
         * Returns the string representation.
         */
        public String toString() {
            StringBuffer sbuf = new StringBuffer();

            TypeParameter.toString(sbuf, typeParams);
            sbuf.append(" (");
            Type.toString(sbuf, params);
            sbuf.append(") ");
            sbuf.append(retType);
            if (exceptions.length > 0) {
                sbuf.append(" throws ");
                Type.toString(sbuf, exceptions);
            }

            return sbuf.toString();
        }

        /**
         * Returns the encoded string representing the method type signature.
         */
        public String encode() {
            StringBuffer sbuf = new StringBuffer();
            if (typeParams.length > 0) {
                sbuf.append('<');
                for (int i = 0; i < typeParams.length; i++)
                    typeParams[i].encode(sbuf);

                sbuf.append('>');
            }

            sbuf.append('(');
            for (int i = 0; i < params.length; i++)
                params[i].encode(sbuf);

            sbuf.append(')');
            retType.encode(sbuf);
            if (exceptions.length > 0)
                for (int i = 0; i < exceptions.length; i++) {
                    sbuf.append('^');
                    exceptions[i].encode(sbuf);
                }

            return sbuf.toString();
        }
    }

    /**
     * Formal type parameters.
     *
     * @see TypeArgument
     */
    public static class TypeParameter {
        String name;
        ObjectType superClass;
        ObjectType[] superInterfaces;

        TypeParameter(String sig, int nb, int ne, ObjectType sc, ObjectType[] si) {
            name = sig.substring(nb, ne);
            superClass = sc;
            superInterfaces = si;
        }

        /**
         * Constructs a <code>TypeParameter</code> representing a type parametre
         * like <code>&lt;T extends ... &gt;<code>.
         *
         * @param name      parameter name.
         * @param superClass    an upper bound class-type (or null).
         * @param superInterfaces   an upper bound interface-type (or null).
         */
        public TypeParameter(String name, ObjectType superClass, ObjectType[] superInterfaces) {
            this.name = name;
            this.superClass = superClass;
            if (superInterfaces == null)
                this.superInterfaces = new ObjectType[0];
            else
                this.superInterfaces = superInterfaces;
        }

        /**
         * Constructs a <code>TypeParameter</code> representing a type parameter
         * like <code>&lt;T&gt;<code>.
         *
         * @param name          parameter name.
         */
        public TypeParameter(String name) {
            this(name, null, null);
        }

        /**
         * Returns the name of the type parameter.
         */
        public String getName() {
            return name;
        }

        /**
         * Returns the class bound of this parameter.
         */
        public ObjectType getClassBound() { return superClass; }

        /**
         * Returns the interface bound of this parameter.
         *
         * @return a zero-length array if the interface bound is not specified.
         */
        public ObjectType[] getInterfaceBound() { return superInterfaces; }

        /**
         * Returns the string representation.
         */
        public String toString() {
            StringBuffer sbuf = new StringBuffer(getName());
            if (superClass != null)
                sbuf.append(" extends ").append(superClass.toString());

            int len = superInterfaces.length;
            if (len > 0) {
                for (int i = 0; i < len; i++) {
                    if (i > 0 || superClass != null)
                        sbuf.append(" & ");
                    else
                        sbuf.append(" extends ");

                    sbuf.append(superInterfaces[i].toString());
                }
            }

            return sbuf.toString();
        }

        static void toString(StringBuffer sbuf, TypeParameter[] tp) {
            sbuf.append('<');
            for (int i = 0; i < tp.length; i++) {
                if (i > 0)
                    sbuf.append(", ");

                sbuf.append(tp[i]);
            }

            sbuf.append('>');
        }

        void encode(StringBuffer sb) {
            sb.append(name);
            if (superClass == null)
                sb.append(":Ljava/lang/Object;");
            else {
                sb.append(':');
                superClass.encode(sb);
            }

            for (int i = 0; i < superInterfaces.length; i++) {
                sb.append(':');
                superInterfaces[i].encode(sb);
            }
        }
    }

    /**
     * Type argument.
     *
     * @see TypeParameter
     */
    public static class TypeArgument {
        ObjectType arg;
        char wildcard;

        TypeArgument(ObjectType a, char w) {
            arg = a;
            wildcard = w;
        }

        /**
         * Constructs a <code>TypeArgument</code>.
         * A type argument is <code>&lt;String&gt;</code>, <code>&lt;int[]&gt;</code>,
         * or a type variable <code>&lt;T&gt;</code>, etc.
         *
         * @param t         a class type, an array type, or a type variable.  
         */
        public TypeArgument(ObjectType t) {
            this(t, ' ');
        }

        /**
         * Constructs a <code>TypeArgument</code> representing <code>&lt;?&gt;</code>.
         */
        public TypeArgument() {
            this(null, '*');
        }

        /**
         * A factory method constructing a <code>TypeArgument</code> with an upper bound.
         * It represents <code>&lt;? extends ... &gt;</code> 
         * 
         * @param t     an upper bound type.
         */
        public static TypeArgument subclassOf(ObjectType t) {
            return new TypeArgument(t, '+');
        }

        /**
         * A factory method constructing a <code>TypeArgument</code> with an lower bound.
         * It represents <code>&lt;? super ... &gt;</code>
         * 
         * @param t     an lower bbound type.
         */
        public static TypeArgument superOf(ObjectType t) {
            return new TypeArgument(t, '-');
        }

        /**
         * Returns the kind of this type argument.
         *
         * @return <code>' '</code> (not-wildcard), <code>'*'</code> (wildcard), <code>'+'</code> (wildcard with
         * upper bound), or <code>'-'</code> (wildcard with lower bound). 
         */
        public char getKind() { return wildcard; }

        /**
         * Returns true if this type argument is a wildcard type
         * such as <code>?</code>, <code>? extends String</code>, or <code>? super Integer</code>.
         */
        public boolean isWildcard() { return wildcard != ' '; }

        /**
         * Returns the type represented by this argument
         * if the argument is not a wildcard type.  Otherwise, this method
         * returns the upper bound (if the kind is '+'),
         * the lower bound (if the kind is '-'), or null (if the upper or lower
         * bound is not specified). 
         */
        public ObjectType getType() { return arg; }

        /**
         * Returns the string representation.
         */
        public String toString() {
            if (wildcard == '*')
                return "?";

            String type = arg.toString();
            if (wildcard == ' ')
                return type;
            else if (wildcard == '+')
                return "? extends " + type;
            else
                return "? super " + type;
        }

        static void encode(StringBuffer sb, TypeArgument[] args) {
            sb.append('<');
            for (int i = 0; i < args.length; i++) {
                TypeArgument ta = args[i];
                if (ta.isWildcard())
                    sb.append(ta.wildcard);

                if (ta.getType() != null)
                    ta.getType().encode(sb);
            }

            sb.append('>');
        }
    }

    /**
     * Primitive types and object types.
     */
    public static abstract class Type {
        abstract void encode(StringBuffer sb);
        static void toString(StringBuffer sbuf, Type[] ts) {
            for (int i = 0; i < ts.length; i++) {
                if (i > 0)
                    sbuf.append(", ");

                sbuf.append(ts[i]);
            }
        }

        /**
         * Returns the type name in the JVM internal style.
         * For example, if the type is a nested class {@code foo.Bar.Baz},
         * then {@code foo.Bar$Baz} is returned.
         */
        public String jvmTypeName() { return toString(); }
    }

    /**
     * Primitive types.
     */
    public static class BaseType extends Type {
        char descriptor;
        BaseType(char c) { descriptor = c; }

        /**
         * Constructs a <code>BaseType</code>.
         *
         * @param typeName      <code>void</code>, <code>int</code>, ... 
         */
        public BaseType(String typeName) {
            this(Descriptor.of(typeName).charAt(0));
        }

        /**
         * Returns the descriptor representing this primitive type.
         *
         * @see javassist.bytecode.Descriptor
         */
        public char getDescriptor() { return descriptor; }

        /**
         * Returns the <code>CtClass</code> representing this
         * primitive type. 
         */
        public CtClass getCtlass() {
            return Descriptor.toPrimitiveClass(descriptor);
        }

        /**
         * Returns the string representation.
         */
        public String toString() {
            return Descriptor.toClassName(Character.toString(descriptor));
        }

        void encode(StringBuffer sb) {
            sb.append(descriptor);
        }
    }

    /**
     * Class types, array types, and type variables.
     * This class is also used for representing a field type.
     */
    public static abstract class ObjectType extends Type {
        /**
         * Returns the encoded string representing the object type signature.
         */
        public String encode() {
            StringBuffer sb = new StringBuffer();
            encode(sb);
            return sb.toString();
        }
    }

    /**
     * Class types.
     */
    public static class ClassType extends ObjectType {
        String name;
        TypeArgument[] arguments;

        static ClassType make(String s, int b, int e,
                              TypeArgument[] targs, ClassType parent) {
            if (parent == null)
                return new ClassType(s, b, e, targs);
            else
                return new NestedClassType(s, b, e, targs, parent);
        }

        ClassType(String signature, int begin, int end, TypeArgument[] targs) {
            name = signature.substring(begin, end).replace('/', '.');
            arguments = targs;
        }

        /**
         * A class type representing <code>java.lang.Object</code>.
         */
        public static ClassType OBJECT = new ClassType("java.lang.Object", null); 

        /**
         * Constructs a <code>ClassType</code>.  It represents
         * the name of a non-nested class.
         *
         * @param className     a fully qualified class name.
         * @param args          type arguments or null.
         */
        public ClassType(String className, TypeArgument[] args) {
            name = className;
            arguments = args;
        }

        /**
         * Constructs a <code>ClassType</code>.  It represents
         * the name of a non-nested class.
         *
         * @param className     a fully qualified class name.
         */
        public ClassType(String className) {
            this(className, null);
        }

        /**
         * Returns the class name.
         */
        public String getName() {
            return name;
        }

        /**
         * Returns the type arguments.
         *
         * @return null if no type arguments are given to this class.
         */
        public TypeArgument[] getTypeArguments() { return arguments; }

        /**
         * If this class is a member of another class, returns the 
         * class in which this class is declared.
         *
         * @return null if this class is not a member of another class.
         */
        public ClassType getDeclaringClass() { return null; }

        /**
         * Returns the string representation.
         */
        public String toString() {
            StringBuffer sbuf = new StringBuffer();
            ClassType parent = getDeclaringClass();
            if (parent != null)
                sbuf.append(parent.toString()).append('.');

            sbuf.append(name);
            if (arguments != null) {
                sbuf.append('<');
                int n = arguments.length;
                for (int i = 0; i < n; i++) {
                    if (i > 0)
                        sbuf.append(", ");

                    sbuf.append(arguments[i].toString());
                }

                sbuf.append('>');
            }

            return sbuf.toString();
        }

        /**
         * Returns the type name in the JVM internal style.
         * For example, if the type is a nested class {@code foo.Bar.Baz},
         * then {@code foo.Bar$Baz} is returned.
         */
        public String jvmTypeName() {
            StringBuffer sbuf = new StringBuffer();
            ClassType parent = getDeclaringClass();
            if (parent != null)
                sbuf.append(parent.jvmTypeName()).append('$');

            sbuf.append(name);
            if (arguments != null) {
                sbuf.append('<');
                int n = arguments.length;
                for (int i = 0; i < n; i++) {
                    if (i > 0)
                        sbuf.append(", ");

                    sbuf.append(arguments[i].toString());
                }

                sbuf.append('>');
            }

            return sbuf.toString();
        }

        void encode(StringBuffer sb) {
            sb.append('L');
            encode2(sb);
            sb.append(';');
        }

        void encode2(StringBuffer sb) {
            ClassType parent = getDeclaringClass();
            if (parent != null) {
                parent.encode2(sb);
                sb.append('$');
            }

            sb.append(name.replace('.', '/'));
            if (arguments != null)
                TypeArgument.encode(sb, arguments);
        }
    }

    /**
     * Nested class types.
     */
    public static class NestedClassType extends ClassType {
        ClassType parent;
        NestedClassType(String s, int b, int e,
                        TypeArgument[] targs, ClassType p) {
            super(s, b, e, targs);
            parent = p;
        }

        /**
         * Constructs a <code>NestedClassType</code>.
         *
         * @param parent        the class surrounding this class type.
         * @param className     a simple class name.  It does not include
         *                      a package name or a parent's class name.
         * @param args          type parameters or null.
         */
        public NestedClassType(ClassType parent, String className, TypeArgument[] args) {
            super(className, args);
            this.parent = parent;
        }

        /**
         * Returns the class that declares this nested class.
         * This nested class is a member of that declaring class.
         */
        public ClassType getDeclaringClass() { return parent; }
    }

    /**
     * Array types.
     */
    public static class ArrayType extends ObjectType {
        int dim;
        Type componentType;

        /**
         * Constructs an <code>ArrayType</code>.
         *
         * @param d         dimension.
         * @param comp      the component type.
         */
        public ArrayType(int d, Type comp) {
            dim = d;
            componentType = comp;
        }

        /**
         * Returns the dimension of the array. 
         */
        public int getDimension() { return dim; }

        /**
         * Returns the component type.
         */
        public Type getComponentType() {
            return componentType;
        }

        /**
         * Returns the string representation.
         */
        public String toString() {
            StringBuffer sbuf = new StringBuffer(componentType.toString());
            for (int i = 0; i < dim; i++)
                sbuf.append("[]");

            return sbuf.toString();
        }

        void encode(StringBuffer sb) {
            for (int i = 0; i < dim; i++)
                sb.append('[');

            componentType.encode(sb);
        }
    }

    /**
     * Type variables.
     */
    public static class TypeVariable extends ObjectType {
        String name;

        TypeVariable(String sig, int begin, int end) {
            name = sig.substring(begin, end);
        }

        /**
         * Constructs a <code>TypeVariable</code>.
         *
         * @param name      the name of a type variable.
         */
        public TypeVariable(String name) {
            this.name = name;
        }

        /**
         * Returns the variable name.
         */
        public String getName() {
            return name;
        }

        /**
         * Returns the string representation.
         */
        public String toString() {
            return name;
        }

        void encode(StringBuffer sb) {
            sb.append('T').append(name).append(';');
        }
    }

    /**
     * Parses the given signature string as a class signature.
     *
     * @param  sig                  the signature obtained from the <code>SignatureAttribute</code>
     *                              of a <code>ClassFile</code>.
     * @return  a tree-like data structure representing a class signature.  It provides
     *          convenient accessor methods.
     * @throws BadBytecode          thrown when a syntactical error is found.
     * @see #getSignature()
     * @since 3.5
     */
    public static ClassSignature toClassSignature(String sig) throws BadBytecode {
        try {
            return parseSig(sig);
        }
        catch (IndexOutOfBoundsException e) {
            throw error(sig);
        }
    }

    /**
     * Parses the given signature string as a method type signature.
     *
     * @param  sig                  the signature obtained from the <code>SignatureAttribute</code>
     *                              of a <code>MethodInfo</code>.
     * @return  @return  a tree-like data structure representing a method signature.  It provides
     *          convenient accessor methods.
     * @throws BadBytecode          thrown when a syntactical error is found.
     * @see #getSignature()
     * @since 3.5
     */
    public static MethodSignature toMethodSignature(String sig) throws BadBytecode {
        try {
            return parseMethodSig(sig);
        }
        catch (IndexOutOfBoundsException e) {
            throw error(sig);
        }
    }

    /**
     * Parses the given signature string as a field type signature.
     *
     * @param  sig                  the signature string obtained from the <code>SignatureAttribute</code>
     *                              of a <code>FieldInfo</code>.
     * @return the field type signature.
     * @throws BadBytecode          thrown when a syntactical error is found.
     * @see #getSignature()
     * @since 3.5
     */
    public static ObjectType toFieldSignature(String sig) throws BadBytecode {
        try {
            return parseObjectType(sig, new Cursor(), false);
        }
        catch (IndexOutOfBoundsException e) {
            throw error(sig);
        }
    }

    /**
     * Parses the given signature string as a type signature.
     * The type signature is either the field type signature or a base type
     * descriptor including <code>void</code> type. 
     *
     * @throws BadBytecode		thrown when a syntactical error is found.
     * @since 3.18
     */
    public static Type toTypeSignature(String sig) throws BadBytecode {
    	try {
    		return parseType(sig, new Cursor());
    	}
    	catch (IndexOutOfBoundsException e) {
            throw error(sig);
        }
    }

    private static ClassSignature parseSig(String sig)
        throws BadBytecode, IndexOutOfBoundsException
    {
        Cursor cur = new Cursor();
        TypeParameter[] tp = parseTypeParams(sig, cur);
        ClassType superClass = parseClassType(sig, cur);
        int sigLen = sig.length();
        ArrayList ifArray = new ArrayList();
        while (cur.position < sigLen && sig.charAt(cur.position) == 'L')
            ifArray.add(parseClassType(sig, cur));

        ClassType[] ifs
            = (ClassType[])ifArray.toArray(new ClassType[ifArray.size()]);
        return new ClassSignature(tp, superClass, ifs);
    }

    private static MethodSignature parseMethodSig(String sig)
        throws BadBytecode
    {
        Cursor cur = new Cursor();
        TypeParameter[] tp = parseTypeParams(sig, cur);
        if (sig.charAt(cur.position++) != '(')
            throw error(sig);

        ArrayList params = new ArrayList();
        while (sig.charAt(cur.position) != ')') {
            Type t = parseType(sig, cur);
            params.add(t);
        }

        cur.position++;
        Type ret = parseType(sig, cur);
        int sigLen = sig.length();
        ArrayList exceptions = new ArrayList();
        while (cur.position < sigLen && sig.charAt(cur.position) == '^') {
            cur.position++;
            ObjectType t = parseObjectType(sig, cur, false);
            if (t instanceof ArrayType)
                throw error(sig);

            exceptions.add(t);
        }

        Type[] p = (Type[])params.toArray(new Type[params.size()]);
        ObjectType[] ex = (ObjectType[])exceptions.toArray(new ObjectType[exceptions.size()]);
        return new MethodSignature(tp, p, ret, ex);
    }

    private static TypeParameter[] parseTypeParams(String sig, Cursor cur)
        throws BadBytecode
    {
        ArrayList typeParam = new ArrayList();
        if (sig.charAt(cur.position) == '<') {
            cur.position++;
            while (sig.charAt(cur.position) != '>') {
                int nameBegin = cur.position; 
                int nameEnd = cur.indexOf(sig, ':');
                ObjectType classBound = parseObjectType(sig, cur, true);
                ArrayList ifBound = new ArrayList();
                while (sig.charAt(cur.position) == ':') {
                    cur.position++;
                    ObjectType t = parseObjectType(sig, cur, false);
                    ifBound.add(t);
                }

                TypeParameter p = new TypeParameter(sig, nameBegin, nameEnd,
                        classBound, (ObjectType[])ifBound.toArray(new ObjectType[ifBound.size()]));
                typeParam.add(p);
            }

            cur.position++;
        }

        return (TypeParameter[])typeParam.toArray(new TypeParameter[typeParam.size()]);
    }

    private static ObjectType parseObjectType(String sig, Cursor c, boolean dontThrow)
        throws BadBytecode
    {
        int i;
        int begin = c.position;
        switch (sig.charAt(begin)) {
        case 'L' :
            return parseClassType2(sig, c, null);
        case 'T' :
            i = c.indexOf(sig, ';');
            return new TypeVariable(sig, begin + 1, i);
        case '[' :
            return parseArray(sig, c);
        default :
            if (dontThrow)
                return null;
            else
                throw error(sig);
        }
    }

    private static ClassType parseClassType(String sig, Cursor c)
        throws BadBytecode
    {
        if (sig.charAt(c.position) == 'L')
            return parseClassType2(sig, c, null);
        else
            throw error(sig);
    }

    private static ClassType parseClassType2(String sig, Cursor c, ClassType parent)
        throws BadBytecode
    {
        int start = ++c.position;
        char t;
        do {
            t = sig.charAt(c.position++);
        } while (t != '$' && t != '<' && t != ';');
        int end = c.position - 1;
        TypeArgument[] targs;
        if (t == '<') {
            targs = parseTypeArgs(sig, c);
            t = sig.charAt(c.position++);
        }
        else
            targs = null;

        ClassType thisClass = ClassType.make(sig, start, end, targs, parent);
        if (t == '$' || t == '.') {
            c.position--;
            return parseClassType2(sig, c, thisClass);
        }
        else
            return thisClass; 
    }

    private static TypeArgument[] parseTypeArgs(String sig, Cursor c) throws BadBytecode {
        ArrayList args = new ArrayList();
        char t;
        while ((t = sig.charAt(c.position++)) != '>') {
            TypeArgument ta;
            if (t == '*' )
                ta = new TypeArgument(null, '*');
            else {
                if (t != '+' && t != '-') {
                    t = ' ';
                    c.position--;
                }

                ta = new TypeArgument(parseObjectType(sig, c, false), t);
            }

            args.add(ta);
        }

        return (TypeArgument[])args.toArray(new TypeArgument[args.size()]);
    }

    private static ObjectType parseArray(String sig, Cursor c) throws BadBytecode {
        int dim = 1;
        while (sig.charAt(++c.position) == '[')
            dim++;

        return new ArrayType(dim, parseType(sig, c));
    }

    private static Type parseType(String sig, Cursor c) throws BadBytecode {
        Type t = parseObjectType(sig, c, true);
        if (t == null)
            t = new BaseType(sig.charAt(c.position++));

        return t;
    }

    private static BadBytecode error(String sig) {
        return new BadBytecode("bad signature: " + sig);
    }
}