KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > BSHType


1 /*****************************************************************************
2  * *
3  * This file is part of the BeanShell Java Scripting distribution. *
4  * Documentation and updates may be found at http://www.beanshell.org/ *
5  * *
6  * Sun Public License Notice: *
7  * *
8  * The contents of this file are subject to the Sun Public License Version *
9  * 1.0 (the "License"); you may not use this file except in compliance with *
10  * the License. A copy of the License is available at http://www.sun.com *
11  * *
12  * The Original Code is BeanShell. The Initial Developer of the Original *
13  * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
14  * (C) 2000. All Rights Reserved. *
15  * *
16  * GNU Public License Notice: *
17  * *
18  * Alternatively, the contents of this file may be used under the terms of *
19  * the GNU Lesser General Public License (the "LGPL"), in which case the *
20  * provisions of LGPL are applicable instead of those above. If you wish to *
21  * allow use of your version of this file only under the terms of the LGPL *
22  * and not to allow others to use your version of this file under the SPL, *
23  * indicate your decision by deleting the provisions above and replace *
24  * them with the notice and other provisions required by the LGPL. If you *
25  * do not delete the provisions above, a recipient may use your version of *
26  * this file under either the SPL or the LGPL. *
27  * *
28  * Patrick Niemeyer (pat@pat.net) *
29  * Author of Learning Java, O'Reilly & Associates *
30  * http://www.pat.net/~pat/ *
31  * *
32  *****************************************************************************/

33
34
35 package bsh;
36
37 import java.lang.reflect.Array JavaDoc;
38
39 class BSHType extends SimpleNode
40     implements BshClassManager.Listener
41 {
42     /**
43         baseType is used during evaluation of full type and retained for the
44         case where we are an array type.
45         In the case where we are not an array this will be the same as type.
46     */

47     private Class JavaDoc baseType;
48     /**
49         If we are an array type this will be non zero and indicate the
50         dimensionality of the array. e.g. 2 for String[][];
51     */

52     private int arrayDims;
53
54     /**
55         Internal cache of the type. Cleared on classloader change.
56     */

57     private Class JavaDoc type;
58
59     String JavaDoc descriptor;
60
61     BSHType(int id) {
62         super(id);
63     }
64
65     /**
66         Used by the grammar to indicate dimensions of array types
67         during parsing.
68     */

69     public void addArrayDimension() {
70         arrayDims++;
71     }
72
73     SimpleNode getTypeNode() {
74         return (SimpleNode)jjtGetChild(0);
75     }
76
77     /**
78          Returns a class descriptor for this type.
79          If the type is an ambiguous name (object type) evaluation is
80          attempted through the namespace in order to resolve imports.
81          If it is not found and the name is non-compound we assume the default
82          package for the name.
83     */

84     public String JavaDoc getTypeDescriptor(
85         CallStack callstack, Interpreter interpreter, String JavaDoc defaultPackage )
86     {
87         // return cached type if available
88
if ( descriptor != null )
89             return descriptor;
90
91         String JavaDoc descriptor;
92         // first node will either be PrimitiveType or AmbiguousName
93
SimpleNode node = getTypeNode();
94         if ( node instanceof BSHPrimitiveType )
95             descriptor = getTypeDescriptor( ((BSHPrimitiveType)node).type );
96         else
97         {
98             String JavaDoc clasName = ((BSHAmbiguousName)node).text;
99             BshClassManager bcm = interpreter.getClassManager();
100             // Note: incorrect here - we are using the hack in bsh class
101
// manager that allows lookup by base name. We need to eliminate
102
// this limitation by working through imports. See notes in class
103
// manager.
104
String JavaDoc definingClass = bcm.getClassBeingDefined( clasName );
105
106             Class JavaDoc clas = null;
107             if ( definingClass == null )
108             {
109                 try {
110                     clas = ((BSHAmbiguousName)node).toClass(
111                         callstack, interpreter );
112                 } catch ( EvalError e ) {
113                     //throw new InterpreterError("unable to resolve type: "+e);
114
// ignore and try default package
115
//System.out.println("BSHType: "+node+" class not found");
116
}
117             } else
118                 clasName = definingClass;
119
120             if ( clas != null )
121             {
122                 //System.out.println("found clas: "+clas);
123
descriptor = getTypeDescriptor( clas );
124             }else
125             {
126                 if ( defaultPackage == null || Name.isCompound( clasName ) )
127                     descriptor = "L" + clasName.replace('.','/') + ";";
128                 else
129                     descriptor =
130                         "L"+defaultPackage.replace('.','/')+"/"+clasName + ";";
131             }
132         }
133
134         for(int i=0; i<arrayDims; i++)
135             descriptor = "["+descriptor;
136
137         this.descriptor = descriptor;
138     //System.out.println("BSHType: returning descriptor: "+descriptor);
139
return descriptor;
140     }
141
142     public Class JavaDoc getType( CallStack callstack, Interpreter interpreter )
143         throws EvalError
144     {
145         // return cached type if available
146
if ( type != null )
147             return type;
148
149         // first node will either be PrimitiveType or AmbiguousName
150
SimpleNode node = getTypeNode();
151         if ( node instanceof BSHPrimitiveType )
152             baseType = ((BSHPrimitiveType)node).getType();
153         else
154             baseType = ((BSHAmbiguousName)node).toClass(
155                 callstack, interpreter );
156
157         if ( arrayDims > 0 ) {
158             try {
159                 // Get the type by constructing a prototype array with
160
// arbitrary (zero) length in each dimension.
161
int[] dims = new int[arrayDims]; // int array default zeros
162
Object JavaDoc obj = Array.newInstance(baseType, dims);
163                 type = obj.getClass();
164             } catch(Exception JavaDoc e) {
165                 throw new EvalError("Couldn't construct array type",
166                     this, callstack );
167             }
168         } else
169             type = baseType;
170
171         // hack... sticking to first interpreter that resolves this
172
// see comments on type instance variable
173
interpreter.getClassManager().addListener(this);
174
175         return type;
176     }
177
178     /**
179         baseType is used during evaluation of full type and retained for the
180         case where we are an array type.
181         In the case where we are not an array this will be the same as type.
182     */

183     public Class JavaDoc getBaseType() {
184         return baseType;
185     }
186     /**
187         If we are an array type this will be non zero and indicate the
188         dimensionality of the array. e.g. 2 for String[][];
189     */

190     public int getArrayDims() {
191         return arrayDims;
192     }
193
194     public void classLoaderChanged() {
195         type = null;
196         baseType = null;
197     }
198
199     public static String JavaDoc getTypeDescriptor( Class JavaDoc clas )
200     {
201         if ( clas == Boolean.TYPE ) return "Z";
202         if ( clas == Character.TYPE ) return "C";
203         if ( clas == Byte.TYPE ) return "B";
204         if ( clas == Short.TYPE ) return "S";
205         if ( clas == Integer.TYPE ) return "I";
206         if ( clas == Long.TYPE ) return "J";
207         if ( clas == Float.TYPE ) return "F";
208         if ( clas == Double.TYPE ) return "D";
209         if ( clas == Void.TYPE ) return "V";
210     // Is getName() ok? test with 1.1
211
String JavaDoc name = clas.getName().replace('.','/');
212
213         if ( name.startsWith("[") || name.endsWith(";") )
214             return name;
215         else
216             return "L"+ name.replace('.','/') +";";
217     }
218 }
219
Popular Tags