KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > bcel > internal > generic > Type


1 package com.sun.org.apache.bcel.internal.generic;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache BCEL" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache BCEL", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
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 APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import com.sun.org.apache.bcel.internal.Constants;
58 import com.sun.org.apache.bcel.internal.classfile.*;
59 import java.io.*;
60 import java.util.ArrayList JavaDoc;
61
62 /**
63  * Abstract super class for all possible java types, namely basic types
64  * such as int, object types like String and array types, e.g. int[]
65  *
66  * @version $Id: Type.java,v 1.1.1.1 2001/10/29 20:00:28 jvanzyl Exp $
67  * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
68  */

69 public abstract class Type {
70   protected byte type;
71   protected String JavaDoc signature; // signature for the type
72

73   /** Predefined constants
74    */

75   public static final BasicType VOID = new BasicType(Constants.T_VOID);
76   public static final BasicType BOOLEAN = new BasicType(Constants.T_BOOLEAN);
77   public static final BasicType INT = new BasicType(Constants.T_INT);
78   public static final BasicType SHORT = new BasicType(Constants.T_SHORT);
79   public static final BasicType BYTE = new BasicType(Constants.T_BYTE);
80   public static final BasicType LONG = new BasicType(Constants.T_LONG);
81   public static final BasicType DOUBLE = new BasicType(Constants.T_DOUBLE);
82   public static final BasicType FLOAT = new BasicType(Constants.T_FLOAT);
83   public static final BasicType CHAR = new BasicType(Constants.T_CHAR);
84   public static final ObjectType OBJECT = new ObjectType("java.lang.Object");
85   public static final ObjectType STRING = new ObjectType("java.lang.String");
86   public static final ObjectType STRINGBUFFER = new ObjectType("java.lang.StringBuffer");
87   public static final ObjectType THROWABLE = new ObjectType("java.lang.Throwable");
88   public static final Type[] NO_ARGS = new Type[0];
89   public static final ReferenceType NULL = new ReferenceType();
90   public static final Type UNKNOWN = new Type(Constants.T_UNKNOWN,
91                                 "<unknown object>"){};
92
93   protected Type(byte t, String JavaDoc s) {
94     type = t;
95     signature = s;
96   }
97
98   /**
99    * @return signature for given type.
100    */

101   public String JavaDoc getSignature() { return signature; }
102
103   /**
104    * @return type as defined in Constants
105    */

106   public byte getType() { return type; }
107
108   /**
109    * @return stack size of this type (2 for long and double, 0 for void, 1 otherwise)
110    */

111   public int getSize() {
112     switch(type) {
113     case Constants.T_DOUBLE:
114     case Constants.T_LONG: return 2;
115     case Constants.T_VOID: return 0;
116     default: return 1;
117     }
118   }
119
120   /**
121    * @return Type string, e.g. `int[]'
122    */

123   public String JavaDoc toString() {
124     return ((this.equals(Type.NULL) || (type >= Constants.T_UNKNOWN)))? signature :
125       Utility.signatureToString(signature, false);
126   }
127
128   /**
129    * Convert type to Java method signature, e.g. int[] f(java.lang.String x)
130    * becomes (Ljava/lang/String;)[I
131    *
132    * @param return_type what the method returns
133    * @param arg_types what are the argument types
134    * @return method signature for given type(s).
135    */

136   public static String JavaDoc getMethodSignature(Type return_type, Type[] arg_types) {
137     StringBuffer JavaDoc buf = new StringBuffer JavaDoc("(");
138     int length = (arg_types == null)? 0 : arg_types.length;
139
140     for(int i=0; i < length; i++)
141       buf.append(arg_types[i].getSignature());
142
143     buf.append(')');
144     buf.append(return_type.getSignature());
145
146     return buf.toString();
147   }
148
149   private static int consumed_chars=0; // Remember position in string, see getArgumentTypes
150

151   /**
152    * Convert signature to a Type object.
153    * @param signature signature string such as Ljava/lang/String;
154    * @return type object
155    */

156   public static final Type getType(String JavaDoc signature)
157     throws StringIndexOutOfBoundsException JavaDoc
158   {
159     byte type = Utility.typeOfSignature(signature);
160
161     if(type <= Constants.T_VOID) {
162       consumed_chars = 1;
163       return BasicType.getType(type);
164     } else if(type == Constants.T_ARRAY) {
165       int dim=0;
166       do { // Count dimensions
167
dim++;
168       } while(signature.charAt(dim) == '[');
169
170       // Recurse, but just once, if the signature is ok
171
Type t = getType(signature.substring(dim));
172
173       consumed_chars += dim; // update counter
174

175       return new ArrayType(t, dim);
176     } else { // type == T_REFERENCE
177
int index = signature.indexOf(';'); // Look for closing `;'
178

179       if(index < 0)
180     throw new ClassFormatError JavaDoc("Invalid signature: " + signature);
181     
182       consumed_chars = index + 1; // "Lblabla;" `L' and `;' are removed
183

184       return new ObjectType(signature.substring(1, index).replace('/', '.'));
185     }
186   }
187
188   /**
189    * Convert return value of a method (signature) to a Type object.
190    *
191    * @param signature signature string such as (Ljava/lang/String;)V
192    * @return return type
193    */

194   public static Type getReturnType(String JavaDoc signature) {
195     try {
196       // Read return type after `)'
197
int index = signature.lastIndexOf(')') + 1;
198       return getType(signature.substring(index));
199     } catch(StringIndexOutOfBoundsException JavaDoc e) { // Should never occur
200
throw new ClassFormatError JavaDoc("Invalid method signature: " + signature);
201     }
202   }
203
204   /**
205    * Convert arguments of a method (signature) to an array of Type objects.
206    * @param signature signature string such as (Ljava/lang/String;)V
207    * @return array of argument types
208    */

209   public static Type[] getArgumentTypes(String JavaDoc signature) {
210     ArrayList JavaDoc vec = new ArrayList JavaDoc();
211     int index;
212     Type[] types;
213
214     try { // Read all declarations between for `(' and `)'
215
if(signature.charAt(0) != '(')
216     throw new ClassFormatError JavaDoc("Invalid method signature: " + signature);
217
218       index = 1; // current string position
219

220       while(signature.charAt(index) != ')') {
221     vec.add(getType(signature.substring(index)));
222     index += consumed_chars; // update position
223
}
224     } catch(StringIndexOutOfBoundsException JavaDoc e) { // Should never occur
225
throw new ClassFormatError JavaDoc("Invalid method signature: " + signature);
226     }
227     
228     types = new Type[vec.size()];
229     vec.toArray(types);
230     return types;
231   }
232 }
233
Popular Tags