KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > trove > classfile > MethodDescriptor


1 /* ====================================================================
2  * Trove - Copyright (c) 1997-2000 Walt Disney Internet Group
3  * ====================================================================
4  * The Tea Software License, Version 1.1
5  *
6  * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Walt Disney Internet Group (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@dig.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35  * written permission of the Walt Disney Internet Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.trove.classfile;
54
55 import java.util.List JavaDoc;
56 import java.util.ArrayList JavaDoc;
57
58 /******************************************************************************
59  * This class is used to build method descriptor strings as
60  * defined in <i>The Java Virtual Machine Specification</i>, section 4.3.3.
61  *
62  * @author Brian S O'Neill
63  * @version
64  * <!--$$Revision:--> 22 <!-- $-->, <!--$$JustDate:--> 9/07/00 <!-- $-->
65  */

66 public class MethodDescriptor extends Descriptor {
67     private static TypeDescriptor[] cEmptyParams = new TypeDescriptor[0];
68
69     private String JavaDoc mStr;
70     private TypeDescriptor mRetType;
71     private TypeDescriptor[] mParams;
72     
73     public MethodDescriptor() {
74         this(null, null);
75     }
76     
77     /**
78      * Used to construct a method descriptor for a method with a return
79      * type and parameters.
80      */

81     public MethodDescriptor(TypeDescriptor ret, TypeDescriptor[] params) {
82         if (params == null) {
83             params = cEmptyParams;
84         }
85
86         mStr = generate(ret, params);
87         mRetType = ret;
88         mParams = params;
89     }
90
91     public TypeDescriptor getReturnType() {
92         return mRetType;
93     }
94     
95     public int getParameterCount() {
96         return mParams.length;
97     }
98
99     public TypeDescriptor[] getParameterTypes() {
100         return mParams;
101     }
102
103     public String JavaDoc toString() {
104         return mStr;
105     }
106     
107     public static String JavaDoc generate() {
108         return generate(null, null);
109     }
110     
111     /**
112      * Used to generate a method descriptor for a method with a return
113      * type and parameters.
114      */

115     public static String JavaDoc generate(TypeDescriptor ret,
116                                   TypeDescriptor[] params) {
117
118         StringBuffer JavaDoc desc = new StringBuffer JavaDoc(20);
119         
120         desc.append('(');
121         
122         if (params != null) {
123             for (int i=0; i<params.length; i++) {
124                 desc.append(params[i]);
125             }
126         }
127         
128         desc.append(')');
129         
130         if (ret == null) ret = new TypeDescriptor(Void.TYPE);
131         desc.append(ret);
132         
133         return desc.toString();
134     }
135
136     public static MethodDescriptor parseMethodDesc(String JavaDoc desc)
137         throws IllegalArgumentException JavaDoc {
138
139         MethodDescriptor md;
140         int cursor = 0;
141         try {
142             char c;
143
144             if ((c = desc.charAt(cursor++)) != '(') {
145                 throw new IllegalArgumentException JavaDoc
146                     ("Invalid descriptor: " + desc);
147             }
148
149             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
150             List JavaDoc list = new ArrayList JavaDoc();
151
152             while ((c = desc.charAt(cursor++)) != ')') {
153                 switch (c) {
154                 case 'V':
155                 case 'I':
156                 case 'C':
157                 case 'Z':
158                 case 'D':
159                 case 'F':
160                 case 'J':
161                 case 'B':
162                 case 'S':
163                     buf.append(c);
164                     break;
165                 case '[':
166                     buf.append(c);
167                     continue;
168                 case 'L':
169                     while (true) {
170                         buf.append(c);
171                         if (c == ';') {
172                             break;
173                         }
174                         c = desc.charAt(cursor++);
175                     }
176                     break;
177                 default:
178                     throw new IllegalArgumentException JavaDoc
179                         ("Invalid descriptor: " + desc);
180                 }
181
182                 list.add(TypeDescriptor.parseTypeDesc(buf.toString()));
183                 buf.setLength(0);
184             }
185
186             TypeDescriptor ret =
187                 TypeDescriptor.parseTypeDesc(desc.substring(cursor));
188
189             TypeDescriptor[] tds = new TypeDescriptor[list.size()];
190             tds = (TypeDescriptor[])list.toArray(tds);
191
192             md = new MethodDescriptor(ret, tds);
193         }
194         catch (NullPointerException JavaDoc e) {
195             throw new IllegalArgumentException JavaDoc("Invalid descriptor: " + desc);
196         }
197         catch (IndexOutOfBoundsException JavaDoc e) {
198             throw new IllegalArgumentException JavaDoc("Invalid descriptor: " + desc);
199         }
200
201         return md;
202     }
203 }
204
Popular Tags