PageRenderTime 83ms CodeModel.GetById 41ms app.highlight 7ms RepoModel.GetById 33ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/bsh/BSHMethodDeclaration.java

#
Java | 146 lines | 65 code | 18 blank | 63 comment | 10 complexity | d79311864dd42335cf6deda1c24c05a4 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.util.Vector;
 38
 39/*
 40	This shouldn't have to be public.	
 41	We should add to bsh.This allowing us to invoke a method.
 42	If we do that we should probably use it in Reflect.java
 43
 44	Note: caching of the structure is done in BshMethod
 45	no caching need be done here or in formal param, etc.
 46*/
 47class BSHMethodDeclaration extends SimpleNode
 48{
 49	String name;
 50	BSHFormalParameters params;
 51	BSHBlock block;
 52	// Unsafe caching of type here.
 53	Object returnType; 	// null (none), Primitive.VOID, or a Class
 54
 55	Modifiers modifiers;
 56	int numThrows = 0;
 57
 58	BSHMethodDeclaration(int id) { super(id); }
 59
 60	/**
 61		Evaluate the declaration of the method.  That is, determine the
 62		structure of the method and install it into the caller's namespace.
 63	*/
 64	public Object eval( CallStack callstack, Interpreter interpreter )
 65		throws EvalError
 66	{
 67		if ( block == null ) 
 68			evalNodes( callstack, interpreter );
 69
 70		// Install an *instance* of this method in the namespace.
 71		// See notes in BshMethod 
 72
 73// This is not good...
 74// need a way to update eval without re-installing...
 75// so that we can re-eval params, etc. when classloader changes
 76// look into this
 77
 78		NameSpace namespace = callstack.top();
 79		BshMethod bshMethod = new BshMethod( this, namespace, modifiers );
 80		try {
 81			namespace.setMethod( name, bshMethod );
 82		} catch ( UtilEvalError e ) {
 83			throw e.toEvalError(this,callstack);
 84		}
 85
 86		return Primitive.VOID;
 87	}
 88
 89	private void evalNodes( CallStack callstack, Interpreter interpreter ) 
 90		throws EvalError
 91	{
 92		// We will allow methods to be re-written.
 93		/*  
 94		if( namespace has method )
 95			throw new EvalError(
 96			"Method: " + name + " already defined in scope", this);
 97		*/
 98
 99		Object firstNode = jjtGetChild(0);
100		int firstThrows = 1;
101		if ( firstNode instanceof BSHReturnType )
102		{
103			returnType = ((BSHReturnType)firstNode).getReturnType( 
104				callstack, interpreter );
105			params = (BSHFormalParameters)jjtGetChild(1);
106			block = (BSHBlock)jjtGetChild(2+numThrows); // skip throws
107			++firstThrows;
108		}
109		else
110		{
111			params = (BSHFormalParameters)jjtGetChild(0);
112			block = (BSHBlock)jjtGetChild(1+numThrows); // skip throws
113		}
114		
115		// validate that the throws names are class names
116		for(int i=firstThrows; i<numThrows+firstThrows; i++)
117			((BSHAmbiguousName)jjtGetChild(i)).toClass( 
118				callstack, interpreter );
119
120		params.eval( callstack, interpreter );
121
122		// if strictJava mode, check for loose parameters and return type
123		if ( interpreter.getStrictJava() )
124		{
125			for(int i=0; i<params.argTypes.length; i++)
126				if ( params.argTypes[i] == null )
127					// Warning: Null callstack here.  Don't think we need
128					// a stack trace to indicate how we sourced the method.
129					throw new EvalError(
130				"(Strict Java Mode) Undeclared argument type, parameter: " +
131					params.argNames[i] + " in method: " 
132					+ name, this, null );
133
134			if ( returnType == null )
135				// Warning: Null callstack here.  Don't think we need
136				// a stack trace to indicate how we sourced the method.
137				throw new EvalError(
138				"(Strict Java Mode) Undeclared return type for method: "
139					+ name, this, null );
140		}
141	}
142
143	public String toString() {
144		return "MethodDeclaration: "+name;
145	}
146}