![]() Home | ![]() Back | ![]() Contents | ![]() Next |
// File: helloWorld.bsh
helloWorld() {
print("Hello World!");
}
|
addClassPath("/home/pat"); // If it's not already in our classpath
importCommands("/mycommands");
|
helloWorld(); // prints "Hello World!" |
// equivalent
importCommands("com.xyz.utils");
importCommands("/com/xyz/utils");
|
// File: helloWorld.bsh
helloWorld() {
print("Hello World!");
}
helloWorld( String msg ) {
print("Hello World: "+msg);
}
|
/**
Implement dir() command.
*/
public static void invoke( Interpreter env, CallStack callstack )
{
String dir = ".";
invoke( env, callstack, dir );
}
/**
Implement dir( String directory ) command.
*/
public static void invoke(
Interpreter env, CallStack callstack, String dir )
{
...
}
|
invoke( String methodName, Object [] arguments ) {
print("You invoked the method: "+ methodName );
}
// invoke() will be called to handle noSuchMethod()
noSuchMethod("foo");
|
| Note: Note that this means that currently scripted commands may only be loaded once and then they are effectively cached. |
fooSetter() {
this.caller.foo=42;
}
|
fooSetter(); print( foo ); // 42 |
foo() { ... }
foo();
|
foo() {
bar() { ... }
...
}
// somewhere
fooObject.bar();
|
The diagram above shows the foo() and bar() scopes, along with the caller's
scope access via 'this.caller'.
This is very useful in writing BeanShell commands.
BeanShell command methods are always loaded into the global
scope. If you refer to 'super' from your command you will simply
get 'global'. Often it is desirable to write commands that explicitly have
side effects in the caller's scope. The ability to do so makes it possible to
write new kinds of commands that have the appearance of being "built-in"
to the language.
A good example of this is the eval() BeanShell command. eval() evaluates
a string as if it were typed in the current context. To do this, it sends
the string to an instance of the BeanShell interpreter. But when it does
so it tells the interpreter to evaluate the string in a specific namespace:
the namespace of the caller; using this.caller.
eval("a=5");
print( a ); // 5
|
eval( String text ) {
this.interpreter.eval( text, this.caller.namespace );
}
|
this.caller.caller...;
|
myCommand() {
// "Step into" the caller's namespace.
setNameSpace( this.caller.namespace );
// work as if we were in the caller's namespace.
}
|
object = object(); // save our namespace savedNameSpace = this.namespace; // step into object's namespace setNameSpace( object.namespace ); // Work in the object's scope a=1; b=2; // step back setNameSpace( savedNameSpace ); print( object.a ); // 1 print( object.b ); // 2 print( a ); // ERROR! undefined |
assert( boolean condition )
{
if ( condition )
print( "Test Passed..." );
else {
print(
"Test FAILED: "
+"Line: "+ this.namespace.getInvocationLine()
+" : "+this.namespace.getInvocationText()
+" : while evaluating file: "+getSourceFileInfo()
);
super.test_failed = true;
}
}
|
absfilename = pathToFile( filename ); |
dir("c:/Windows"); // ok
dir("c:\\Windows"); // ok
|
javap( Date.class ); // use a class type directly javap( new Date() ); // uses class of object javap( "java.util.Date" ); // Uses string name of class javap( java.util.Date ); // Use plain class identifier |
import bsh.ClassIdentifier;
if ( o instanceof ClassIdentifier )
clas = this.namespace.identifierToClass(o);
if ( o instanceof String)
clas = this.namespace.getClass((String)o);
else if ( o instanceof Class )
clas = o;
else
clas = o.getClass();
|
cm = CollectionManager.getCollectionManager();
if ( cm.isBshIterable( myObject ) )
{
BshIterator iterator = cm.getBshIterator( myObject );
while ( iterator.hasNext() )
i = iterator.next();
}
|
![]() Home | ![]() Back | ![]() Contents | ![]() Next |