KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > classfile > util > InternalTypeEnumeration


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.classfile.util;
22
23 import proguard.classfile.*;
24
25
26 /**
27  * An <code>InternalTypeEnumeration</code> provides an enumeration of all
28  * parameter types listed in a given internal descriptor string. The return type
29  * can retrieved separately.
30  * <p>
31  * A <code>InternalTypeEnumeration</code> object can be reused for processing
32  * different subsequent descriptors, by means of the <code>setDescriptor</code>
33  * method.
34  *
35  * @author Eric Lafortune
36  */

37 public class InternalTypeEnumeration
38 {
39     private String JavaDoc descriptor;
40     private int index;
41
42
43     public InternalTypeEnumeration(String JavaDoc descriptor)
44     {
45         setDescriptor(descriptor);
46     }
47
48
49     public InternalTypeEnumeration()
50     {
51     }
52
53
54     void setDescriptor(String JavaDoc descriptor)
55     {
56         this.descriptor = descriptor;
57
58         reset();
59     }
60
61
62     public void reset()
63     {
64         index = descriptor.indexOf(ClassConstants.INTERNAL_METHOD_ARGUMENTS_OPEN) + 1;
65
66         if (index < 1)
67         {
68             throw new IllegalArgumentException JavaDoc("Missing opening parenthesis in descriptor ["+descriptor+"]");
69         }
70     }
71
72
73     public boolean hasMoreTypes()
74     {
75         return descriptor.charAt(index) != ClassConstants.INTERNAL_METHOD_ARGUMENTS_CLOSE;
76     }
77
78
79     public String JavaDoc nextType()
80     {
81         int startIndex = index;
82
83         // Include all leading array characters.
84
while (descriptor.charAt(index) == ClassConstants.INTERNAL_TYPE_ARRAY)
85         {
86             index++;
87         }
88
89         // Class types consist of an entire string.
90
if (descriptor.charAt(index) == ClassConstants.INTERNAL_TYPE_CLASS_START)
91         {
92             index = descriptor.indexOf(ClassConstants.INTERNAL_TYPE_CLASS_END,
93                                        index + 1);
94             if (index < 0)
95             {
96                 throw new IllegalArgumentException JavaDoc("Missing closing class type in descriptor ["+descriptor+"]");
97             }
98         }
99
100         return descriptor.substring(startIndex, ++index);
101     }
102
103
104     public String JavaDoc returnType()
105     {
106         return descriptor.substring(descriptor.indexOf(ClassConstants.INTERNAL_METHOD_ARGUMENTS_CLOSE) + 1);
107     }
108 }
109
Popular Tags