bsh.BshMethod Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bsh Show documentation
Show all versions of bsh Show documentation
BeanShell is a small, free, embeddable Java source interpreter with object scripting language features,
written in Java. BeanShell dynamically executes standard Java syntax and extends it with common scripting
conveniences such as loose types, commands, and method closures like those in Perl and JavaScript.
The newest version!
/*****************************************************************************
* *
* This file is part of the BeanShell Java Scripting distribution. *
* Documentation and updates may be found at http://www.beanshell.org/ *
* *
* Sun Public License Notice: *
* *
* The contents of this file are subject to the Sun Public License Version *
* 1.0 (the "License"); you may not use this file except in compliance with *
* the License. A copy of the License is available at http://www.sun.com *
* *
* The Original Code is BeanShell. The Initial Developer of the Original *
* Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
* (C) 2000. All Rights Reserved. *
* *
* GNU Public License Notice: *
* *
* Alternatively, the contents of this file may be used under the terms of *
* the GNU Lesser General Public License (the "LGPL"), in which case the *
* provisions of LGPL are applicable instead of those above. If you wish to *
* allow use of your version of this file only under the terms of the LGPL *
* and not to allow others to use your version of this file under the SPL, *
* indicate your decision by deleting the provisions above and replace *
* them with the notice and other provisions required by the LGPL. If you *
* do not delete the provisions above, a recipient may use your version of *
* this file under either the SPL or the LGPL. *
* *
* Patrick Niemeyer ([email protected]) *
* Author of Learning Java, O'Reilly & Associates *
* http://www.pat.net/~pat/ *
* *
*****************************************************************************/
package bsh;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
/**
This represents an instance of a bsh method declaration in a particular
namespace. This is a thin wrapper around the BSHMethodDeclaration
with a pointer to the declaring namespace.
When a method is located in a subordinate namespace or invoked from an
arbitrary namespace it must nontheless execute with its 'super' as the
context in which it was declared.
*/
/*
Note: this method incorrectly caches the method structure. It needs to
be cleared when the classloader changes.
*/
public class BshMethod
implements java.io.Serializable
{
/*
This is the namespace in which the method is set.
It is a back-reference for the node, which needs to execute under this
namespace. It is not necessary to declare this transient, because
we can only be saved as part of our namespace anyway... (currently).
*/
NameSpace declaringNameSpace;
// Begin Method components
Modifiers modifiers;
private String name;
private Class creturnType;
// Arguments
private String [] paramNames;
private int numArgs;
private Class [] cparamTypes;
// Scripted method body
BSHBlock methodBody;
// Java Method, for a BshObject that delegates to a real Java method
private Method javaMethod;
private Object javaObject;
// End method components
BshMethod(
BSHMethodDeclaration method,
NameSpace declaringNameSpace, Modifiers modifiers )
{
this( method.name, method.returnType, method.paramsNode.getParamNames(),
method.paramsNode.paramTypes, method.blockNode, declaringNameSpace,
modifiers );
}
BshMethod(
String name, Class returnType, String [] paramNames,
Class [] paramTypes, BSHBlock methodBody,
NameSpace declaringNameSpace, Modifiers modifiers
) {
this.name = name;
this.creturnType = returnType;
this.paramNames = paramNames;
if ( paramNames != null )
this.numArgs = paramNames.length;
this.cparamTypes = paramTypes;
this.methodBody = methodBody;
this.declaringNameSpace = declaringNameSpace;
this.modifiers = modifiers;
}
/*
Create a BshMethod that delegates to a real Java method upon invocation.
This is used to represent imported object methods.
*/
BshMethod( Method method, Object object )
{
this( method.getName(), method.getReturnType(), null/*paramNames*/,
method.getParameterTypes(), null/*method.block*/,
null/*declaringNameSpace*/, null/*modifiers*/ );
this.javaMethod = method;
this.javaObject = object;
}
/**
Get the argument types of this method.
loosely typed (untyped) arguments will be represented by null argument
types.
*/
/*
Note: bshmethod needs to re-evaluate arg types here
This is broken.
*/
public Class [] getParameterTypes() { return cparamTypes; }
public String [] getParameterNames() { return paramNames; }
/**
Get the return type of the method.
@return Returns null for a loosely typed return value,
Void.TYPE for a void return type, or the Class of the type.
*/
/*
Note: bshmethod needs to re-evaluate the method return type here.
This is broken.
*/
public Class getReturnType() { return creturnType; }
public Modifiers getModifiers() { return modifiers; }
public String getName() { return name; }
/**
Invoke the declared method with the specified arguments and interpreter
reference. This is the simplest form of invoke() for BshMethod
intended to be used in reflective style access to bsh scripts.
*/
public Object invoke(
Object[] argValues, Interpreter interpreter )
throws EvalError
{
return invoke( argValues, interpreter, null, null, false );
}
/**
Invoke the bsh method with the specified args, interpreter ref,
and callstack.
callerInfo is the node representing the method invocation
It is used primarily for debugging in order to provide access to the
text of the construct that invoked the method through the namespace.
@param callerInfo is the BeanShell AST node representing the method
invocation. It is used to print the line number and text of
errors in EvalError exceptions. If the node is null here error
messages may not be able to point to the precise location and text
of the error.
@param callstack is the callstack. If callstack is null a new one
will be created with the declaring namespace of the method on top
of the stack (i.e. it will look for purposes of the method
invocation like the method call occurred in the declaring
(enclosing) namespace in which the method is defined).
*/
public Object invoke(
Object[] argValues, Interpreter interpreter, CallStack callstack,
SimpleNode callerInfo )
throws EvalError
{
return invoke( argValues, interpreter, callstack, callerInfo, false );
}
/**
Invoke the bsh method with the specified args, interpreter ref,
and callstack.
callerInfo is the node representing the method invocation
It is used primarily for debugging in order to provide access to the
text of the construct that invoked the method through the namespace.
@param callerInfo is the BeanShell AST node representing the method
invocation. It is used to print the line number and text of
errors in EvalError exceptions. If the node is null here error
messages may not be able to point to the precise location and text
of the error.
@param callstack is the callstack. If callstack is null a new one
will be created with the declaring namespace of the method on top
of the stack (i.e. it will look for purposes of the method
invocation like the method call occurred in the declaring
(enclosing) namespace in which the method is defined).
@param overrideNameSpace
When true the method is executed in the namespace on the top of the
stack instead of creating its own local namespace. This allows it
to be used in constructors.
*/
Object invoke(
Object[] argValues, Interpreter interpreter, CallStack callstack,
SimpleNode callerInfo, boolean overrideNameSpace )
throws EvalError
{
if ( argValues != null )
for (int i=0; i