KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > BSHArrayDimensions


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 /**
40     The name of this class is somewhat misleading. This covers both the case
41     where there is an array initializer and
42 */

43 class BSHArrayDimensions extends SimpleNode
44 {
45     public Class JavaDoc baseType;
46     public int numDefinedDims;
47     public int numUndefinedDims;
48     /**
49         The Length in each defined dimension. This value set by the eval()
50         Since the values can come from Expressions we should be re-eval()d each
51         time.
52     */

53     public int [] definedDimensions;
54
55     BSHArrayDimensions(int id) { super(id); }
56
57     public void addDefinedDimension() { numDefinedDims++; }
58     public void addUndefinedDimension() { numUndefinedDims++; }
59
60     public Object JavaDoc eval(
61             Class JavaDoc type, CallStack callstack, Interpreter interpreter )
62         throws EvalError
63     {
64         if ( Interpreter.DEBUG ) Interpreter.debug("array base type = "+type);
65         baseType = type;
66         return eval( callstack, interpreter );
67     }
68
69     /**
70         Evaluate the structure of the array in one of two ways:
71
72             a) an initializer exists, evaluate it and return
73             the fully constructed array object, also record the dimensions
74             of that array
75             
76             b) evaluate and record the lengths in each dimension and
77             return void.
78
79         The structure of the array dims is maintained in dimensions.
80     */

81     public Object JavaDoc eval( CallStack callstack, Interpreter interpreter )
82         throws EvalError
83     {
84         SimpleNode child = (SimpleNode)jjtGetChild(0);
85
86         /*
87             Child is array initializer. Evaluate it and fill in the
88             dimensions it returns. Initialized arrays are always fully defined
89             (no undefined dimensions to worry about).
90             The syntax uses the undefinedDimension count.
91             e.g. int [][] { 1, 2 };
92         */

93         if (child instanceof BSHArrayInitializer)
94         {
95             if ( baseType == null )
96                 throw new EvalError(
97                     "Internal Array Eval err: unknown base type",
98                     this, callstack );
99
100             Object JavaDoc initValue = ((BSHArrayInitializer)child).eval(
101                 baseType, numUndefinedDims, callstack, interpreter);
102
103             Class JavaDoc arrayClass = initValue.getClass();
104             int actualDimensions = Reflect.getArrayDimensions(arrayClass);
105             definedDimensions = new int[ actualDimensions ];
106
107             // Compare with number of dimensions actually created with the
108
// number specified (syntax uses the undefined ones here)
109
if ( definedDimensions.length != numUndefinedDims )
110                 throw new EvalError(
111                 "Incompatible initializer. Allocation calls for a " +
112                 numUndefinedDims+ " dimensional array, but initializer is a " +
113                     actualDimensions + " dimensional array", this, callstack );
114
115             // fill in definedDimensions [] lengths
116
Object JavaDoc arraySlice = initValue;
117             for ( int i = 0; i < definedDimensions.length; i++ ) {
118                 definedDimensions[i] = Array.getLength( arraySlice );
119                 if ( definedDimensions[i] > 0 )
120                     arraySlice = Array.get(arraySlice, 0);
121             }
122
123             return initValue;
124         }
125         else
126         // Evaluate the defined dimensions of the array
127
{
128             definedDimensions = new int[ numDefinedDims ];
129
130             for(int i = 0; i < numDefinedDims; i++)
131             {
132                 try {
133                     Object JavaDoc length = ((SimpleNode)jjtGetChild(i)).eval(
134                         callstack, interpreter);
135                     definedDimensions[i] = ((Primitive)length).intValue();
136                 }
137                 catch(Exception JavaDoc e)
138                 {
139                     throw new EvalError(
140                         "Array index: " + i +
141                         " does not evaluate to an integer", this, callstack );
142                 }
143             }
144         }
145
146         return Primitive.VOID;
147     }
148 }
149
Popular Tags