PageRenderTime 208ms CodeModel.GetById 196ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/bsh/BSHArrayDimensions.java

#
Java | 137 lines | 65 code | 16 blank | 56 comment | 9 complexity | d6a6a68998ef0e18cb4b532809290bbc MD5 | raw file
  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
 35package bsh;
 36
 37import java.lang.reflect.Array;
 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*/
 43class BSHArrayDimensions extends SimpleNode
 44{
 45	public Class baseType;
 46    private int arrayDims;
 47
 48	/** The Length in each dimension.  This value set by the eval() */
 49	// is it ok to cache this here?
 50	// it can't change, right?
 51    /*transient?why?*/ 
 52	public int [] dimensions;  
 53
 54    BSHArrayDimensions(int id) { super(id); }
 55
 56    public void addArrayDimension() { arrayDims++; }
 57
 58    public Object eval( 
 59			Class type, CallStack callstack, Interpreter interpreter ) 
 60		throws EvalError 
 61	{
 62		Interpreter.debug("array base type = "+type);
 63		baseType = type;
 64		return eval( callstack, interpreter );
 65	}
 66
 67	/**
 68		Evaluate the structure of the array in one of two ways:
 69
 70			a) an initializer exists, evaluate it and return
 71			the fully constructed array object, also record the dimensions
 72			of that array
 73			
 74			b) evaluate and record the lengths in each dimension and 
 75			return void.
 76
 77		The structure of the array dims is maintained in dimensions.
 78	*/
 79    public Object eval( CallStack callstack, Interpreter interpreter )  
 80		throws EvalError
 81    {
 82		SimpleNode child = (SimpleNode)jjtGetChild(0);
 83
 84		if (child instanceof BSHArrayInitializer)
 85		// evaluate the initializer and the dimensions it returns
 86		{
 87			if ( baseType == null )
 88				throw new EvalError( 
 89					"Internal Array Eval err:  unknown base type", this);
 90
 91			Object initValue = ((BSHArrayInitializer)child).eval(
 92				baseType, arrayDims, callstack, interpreter);
 93
 94			Class arrayClass = initValue.getClass();
 95			dimensions = new int[
 96				Reflect.getArrayDimensions(arrayClass) ];
 97
 98			// compare with number of dimensions explicitly specified
 99			if (dimensions.length != arrayDims)
100				throw new EvalError(
101				"Incompatible initializer. Allocation calls for a " + 
102				arrayDims + " dimensional array, but initializer is a " +
103					dimensions.length + " dimensional array", this);
104
105			// fill in dimensions[] lengths
106			Object arraySlice = initValue;
107			for(int i = 0; i < dimensions.length; i++) {
108				dimensions[i] = Array.getLength( arraySlice );
109				if ( dimensions[i] > 0 )
110					arraySlice = Array.get(arraySlice, 0);
111			}
112
113			return initValue;
114		}
115		else 
116		// evaluate the dimensions of the array
117		{
118			dimensions = new int[ jjtGetNumChildren() ];
119			for(int i = 0; i < dimensions.length; i++)
120			{
121				try {
122					Object length = ((SimpleNode)jjtGetChild(i)).eval(
123						callstack, interpreter);
124					dimensions[i] = ((Primitive)length).intValue();
125				}
126				catch(Exception e)
127				{
128					throw new EvalError(
129						"Array index: " + i + 
130						" does not evaluate to an integer", this);
131				}
132			}
133		}
134
135        return Primitive.VOID;
136    }
137}