PageRenderTime 40ms CodeModel.GetById 30ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/bsh/BSHArrayInitializer.java

#
Java | 130 lines | 69 code | 16 blank | 45 comment | 7 complexity | 76ae7763880d7e04a9b51e604f7e604d 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
 39class BSHArrayInitializer extends SimpleNode
 40{
 41    BSHArrayInitializer(int id) { super(id); }
 42
 43    public Object eval( CallStack callstack, Interpreter interpreter )
 44		throws EvalError 
 45	{
 46		throw new EvalError( "Array initializer has no base type.");
 47	}
 48
 49	/**
 50		Construct the array from the initializer syntax.
 51
 52		@param baseType the base class type of the array (no dimensionality)
 53		@param dimensions the top number of dimensions of the array 
 54			e.g. 2 for a String [][];
 55	*/
 56    public Object eval( Class baseType, int dimensions, 
 57						CallStack callstack, Interpreter interpreter ) 
 58		throws EvalError
 59    {
 60        int numInitializers = jjtGetNumChildren();
 61
 62		// allocate the array to store the initializers
 63		int [] dima = new int [dimensions]; // description of the array
 64		// The other dimensions default to zero and are assigned when 
 65		// the values are set.
 66		dima[0] = numInitializers;
 67        Object initializers = 
 68			Array.newInstance( baseType, dima );
 69
 70		// Evaluate the initializers
 71        for (int i = 0; i < numInitializers; i++)
 72        {
 73			SimpleNode node = (SimpleNode)jjtGetChild(i);
 74            Object currentInitializer;
 75			if ( node instanceof BSHArrayInitializer ) {
 76				if ( dimensions < 2 )
 77					throw new EvalError(
 78						"Invalid Location for Intializer, position: "+i, this);
 79            	currentInitializer = 
 80					((BSHArrayInitializer)node).eval( 
 81						baseType, dimensions-1, callstack, interpreter);
 82			} else
 83            	currentInitializer = node.eval( callstack, interpreter);
 84
 85			if ( currentInitializer == Primitive.VOID )
 86				throw new EvalError(
 87					"Void in array initializer, position"+i, this);
 88
 89			// unwrap primitive to the wrapper type
 90			Object value;
 91			if ( currentInitializer instanceof Primitive )
 92				value = ((Primitive)currentInitializer).getValue();
 93			else
 94				value = currentInitializer;
 95
 96			// store the value in the array
 97            try {
 98				Array.set(initializers, i, value);
 99
100            } catch( IllegalArgumentException e ) {
101				Interpreter.debug("illegal arg"+e);
102				throwTypeError( baseType, currentInitializer, i );
103            } catch( ArrayStoreException e ) { // I think this can happen
104				Interpreter.debug("arraystore"+e);
105				throwTypeError( baseType, currentInitializer, i );
106            }
107        }
108
109        return initializers;
110    }
111
112	private void throwTypeError( 
113		Class baseType, Object initializer, int argNum ) 
114		throws EvalError
115	{
116		String lhsType = Reflect.normalizeClassName(baseType);
117
118		String rhsType;
119		if (initializer instanceof Primitive)
120			rhsType = 
121				((Primitive)initializer).getType().getName();
122		else
123			rhsType = Reflect.normalizeClassName(
124				initializer.getClass());
125
126		throw new EvalError ( "Incompatible type: " + rhsType 
127			+" in initializer of array type: "+ baseType
128			+" at position: "+argNum, this );
129	}
130}