PageRenderTime 59ms CodeModel.GetById 45ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 125 lines | 51 code | 13 blank | 61 comment | 10 complexity | b631aeff1e6bfd14bccf5fb80f6b6355 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	Object returnType; 	// null (none), Primitive.VOID, or a Class
 53
 54	BSHMethodDeclaration(int id)
 55	{
 56		super(id);
 57	}
 58
 59	/**
 60		Evaluate the declaration of the method.  That is, determine the
 61		structure of the method and install it into the caller's namespace.
 62	
 63		Since the structure of the method is only determined by type evaluation
 64		(through eval of BSHFormalParameters) we do not need the interpreter 
 65		or callstack.
 66	*/
 67	public Object eval( NameSpace namespace )  
 68		throws EvalError
 69	{
 70		if ( block == null ) 
 71		{
 72			// We will allow methods to be re-written.
 73			/*  
 74			if( namespace has method )
 75				throw new EvalError(
 76				"Method: " + name + " already defined in scope", this);
 77			*/
 78
 79			if(jjtGetNumChildren() == 3)
 80			{
 81				returnType = 
 82					((BSHReturnType)jjtGetChild(0)).getReturnType( namespace );
 83				params = (BSHFormalParameters)jjtGetChild(1);
 84				block = (BSHBlock)jjtGetChild(2);
 85			}
 86			else
 87			{
 88				params = (BSHFormalParameters)jjtGetChild(0);
 89				block = (BSHBlock)jjtGetChild(1);
 90			}
 91			params.eval( namespace );
 92
 93			// if strictJava mode, check for loose parameters and return type
 94			if ( Interpreter.strictJava )
 95			{
 96				for(int i=0; i<params.argTypes.length; i++)
 97					if ( params.argTypes[i] == null )
 98						throw new EvalError(
 99					"(Strict Java Mode) Undeclared argument type, parameter: " +
100						params.argNames[i] + " in method: " 
101						+ name, this );
102
103				if ( returnType == null )
104					throw new EvalError(
105					"(Strict Java Mode) Undeclared return type for method: "
106						+ name, this );
107			}
108		}
109
110		// Install an *instance* of this method in the namespace.
111		// See notes in BshMethod 
112
113// This is not good...
114// need a way to update eval without re-installing...
115// so that we can re-eval params, etc. when classloader changes
116// look into this
117		namespace.setMethod( name, new BshMethod( this, namespace ) );
118
119		return Primitive.VOID;
120	}
121
122	public String toString() {
123		return "MethodDeclaration: "+name;
124	}
125}