PageRenderTime 408ms CodeModel.GetById 398ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/bsh/BSHAllocationExpression.java

#
Java | 187 lines | 116 code | 22 blank | 49 comment | 12 complexity | 039efced481790c5ea3f36eaa5b504c4 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;
 38import java.lang.reflect.InvocationTargetException;
 39
 40/**
 41	New object, new array, or inner class style allocation with body.
 42*/
 43class BSHAllocationExpression extends SimpleNode
 44{
 45    BSHAllocationExpression(int id) { super(id); }
 46
 47    public Object eval( CallStack callstack, Interpreter interpreter) 
 48		throws EvalError
 49    {
 50        // type is either a class name or a primitive type
 51        SimpleNode type = (SimpleNode)jjtGetChild(0);
 52
 53        // args is either constructor arguments or array dimensions
 54        SimpleNode args = (SimpleNode)jjtGetChild(1);
 55
 56        if ( type instanceof BSHAmbiguousName )
 57        {
 58            BSHAmbiguousName name = (BSHAmbiguousName)type;
 59
 60            if(args instanceof BSHArguments)
 61                return objectAllocation(name, (BSHArguments)args, 
 62					callstack, interpreter );
 63            else
 64                return objectArrayAllocation(name, (BSHArrayDimensions)args, 
 65					callstack, interpreter );
 66        }
 67        else
 68            return primitiveArrayAllocation((BSHPrimitiveType)type,
 69                (BSHArrayDimensions)args, callstack, interpreter );
 70    }
 71
 72    private Object objectAllocation(
 73		BSHAmbiguousName nameNode, BSHArguments argumentsNode, 
 74		CallStack callstack, Interpreter interpreter 
 75	) 
 76		throws EvalError
 77    {
 78		NameSpace namespace = callstack.top();
 79        Class type = nameNode.toClass(namespace);
 80
 81		/* toClass throws this
 82        if (type == null)
 83            throw new EvalError(
 84				"Class " + nameNode.getName(namespace) + " not found.", this);
 85		*/
 86
 87        Object[] args = argumentsNode.getArguments(callstack, interpreter);
 88        if(args == null)
 89            throw new EvalError("Trying to new a class...?", this);
 90
 91		// Is an inner class style object allocation
 92		boolean hasBody = jjtGetNumChildren() > 2;
 93
 94		if ( hasBody ) {
 95        	BSHBlock body = (BSHBlock)jjtGetChild(2);
 96			return constructWithBody( 
 97				type, args, body, callstack, interpreter );
 98		} else
 99			return constructObject( type, args );
100    }
101
102	private Object constructObject( Class type, Object[] args ) 
103		throws EvalError
104	{
105        try {
106            return Reflect.constructObject(type, args);
107        } catch(ReflectError e) {
108            throw new EvalError("Constructor error: " + e.getMessage(), this);
109        } catch(InvocationTargetException e) {
110            Interpreter.debug("The constructor threw an exception:\n\t" +
111                e.getTargetException());
112            throw new TargetError(
113				"Object constructor", e.getTargetException(), this, true);
114        }
115	}
116
117	private Object constructWithBody( 
118		Class type, Object[] args, BSHBlock body,
119		CallStack callstack, Interpreter interpreter ) 
120		throws EvalError
121	{
122		if ( ! type.isInterface() )
123			throw new EvalError(
124				"BeanShell cannot extend class types: "+ type );
125
126		NameSpace namespace = callstack.top();
127// Maybe we should swap in local namespace for the top?
128// who is the caller?
129		NameSpace local = new NameSpace(namespace, "anonymous block object");
130		callstack.push(local);
131		body.eval( callstack, interpreter, true );
132		callstack.pop();
133		return local.getThis(interpreter).getInterface( type );
134	}
135
136// combine part of this with primitiveArrayAllocation
137    private Object objectArrayAllocation(
138		BSHAmbiguousName nameNode, BSHArrayDimensions dimensionsNode, 
139		CallStack callstack, Interpreter interpreter 
140	) 
141		throws EvalError
142    {
143		NameSpace namespace = callstack.top();
144        Class type = nameNode.toClass(namespace);
145        if(type == null)
146            throw new EvalError(
147				"Class " + nameNode.getName(namespace) + " not found.", this);
148
149		// dimensionsNode can return either an intialized version or none.
150        Object result = dimensionsNode.eval( type, callstack, interpreter );
151        if(result != Primitive.VOID)
152            return result;
153		else
154			return arrayNewInstance( type, dimensionsNode );
155    }
156
157// combine part of this with objectArrayAllocation
158    private Object primitiveArrayAllocation(
159		BSHPrimitiveType typeNode, BSHArrayDimensions dimensionsNode, 
160		CallStack callstack, Interpreter interpreter 
161	) 
162		throws EvalError
163    {
164        Class type = typeNode.getType();
165
166		// dimensionsNode can return either an intialized version or none.
167        Object result = dimensionsNode.eval( type, callstack, interpreter );
168        if (result != Primitive.VOID) 
169            return result;
170
171		return arrayNewInstance( type, dimensionsNode );
172    }
173
174	private Object arrayNewInstance( 
175		Class type, BSHArrayDimensions dimensionsNode )
176		throws EvalError
177	{
178        try {
179            return Array.newInstance(type, dimensionsNode.dimensions);
180        } catch( NegativeArraySizeException e1) {
181			throw new TargetError("Negative Array Size", e1);
182        } catch(Exception e) {
183            throw new EvalError("Can't construct primitive array: " +
184                e.getMessage(), this);
185        }
186	}
187}