KickJava   Java API By Example, From Geeks To Geeks.

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


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  * A <code>DescriptorClassEnumeration</code> provides an enumeration of all
27  * classes mentioned in a given descriptor string.
28  * <p>
29  * A <code>DescriptorClassEnumeration</code> object can be reused for processing
30  * different subsequent descriptors, by means of the <code>setDescriptor</code>
31  * method.
32  *
33  * @author Eric Lafortune
34  */

35 public class DescriptorClassEnumeration
36 {
37     private String JavaDoc descriptor;
38     private int index;
39
40
41     public DescriptorClassEnumeration(String JavaDoc descriptor)
42     {
43         setDescriptor(descriptor);
44     }
45
46
47     DescriptorClassEnumeration()
48     {
49     }
50
51
52     void setDescriptor(String JavaDoc descriptor)
53     {
54         this.descriptor = descriptor;
55
56         reset();
57     }
58
59
60     public void reset()
61     {
62         index = 0;
63     }
64
65
66     /**
67      * Returns the number of classes contained in the descriptor. This
68      * is the number of class names that the enumeration will return.
69      */

70     public int classCount()
71     {
72         int count = 0;
73
74         while (nextClassNameStartIndex() >= 0)
75         {
76             count++;
77
78             nextClassNameEndIndex();
79         }
80
81         index = 0;
82
83         return count;
84     }
85
86
87     public boolean hasMoreClassNames()
88     {
89         return index >= 0 && nextClassNameStartIndex() >= 0;
90     }
91
92
93     public String JavaDoc nextFluff()
94     {
95         int fluffStartIndex = index;
96         int fluffEndIndex = nextClassNameStartIndex() + 1;
97
98         // There may be fluff at the end of the descriptor.
99
if (fluffEndIndex == 0)
100         {
101             fluffEndIndex = descriptor.length();
102         }
103
104         return descriptor.substring(fluffStartIndex, fluffEndIndex);
105     }
106
107
108     public String JavaDoc nextClassName()
109     {
110         int classNameStartIndex = nextClassNameStartIndex() + 1;
111         int classNameEndIndex = nextClassNameEndIndex();
112
113         return descriptor.substring(classNameStartIndex, classNameEndIndex);
114     }
115
116
117     private int nextClassNameStartIndex()
118     {
119         index = descriptor.indexOf(ClassConstants.INTERNAL_TYPE_CLASS_START, index);
120
121         return index;
122     }
123
124
125     private int nextClassNameEndIndex()
126     {
127         while (++index < descriptor.length())
128         {
129             char c = descriptor.charAt(index);
130             if (c == ClassConstants.INTERNAL_TYPE_CLASS_END ||
131                 c == ClassConstants.INTERNAL_TYPE_GENERIC_START)
132             {
133                 return index;
134             }
135         }
136
137         throw new IllegalArgumentException JavaDoc("Missing class name terminator in descriptor ["+descriptor+"]");
138     }
139
140
141
142
143     /**
144      * A main method for testing the class name enumeration.
145      */

146     public static void main(String JavaDoc[] args)
147     {
148         try
149         {
150             System.out.println("Descriptor ["+args[0]+"]");
151             DescriptorClassEnumeration enumeration = new DescriptorClassEnumeration(args[0]);
152             System.out.println("Class count = "+enumeration.classCount());
153             System.out.println(" Fluff: ["+enumeration.nextFluff()+"]");
154             while (enumeration.hasMoreClassNames())
155             {
156                 System.out.println(" Name: ["+enumeration.nextClassName()+"]");
157                 System.out.println(" Fluff: ["+enumeration.nextFluff()+"]");
158             }
159         }
160         catch (Exception JavaDoc ex)
161         {
162             ex.printStackTrace();
163         }
164     }
165 }
166
Popular Tags