Simple Syntax
BeanShell is a small, free, embeddable Java source interpreter with object scripting language features,
written in Java. BeanShell is a natural scripting language for Java.
This requires the library bsh-2.0b4.jar to be in classpath.
The following example shows accessing bean properties using simplified syntax in beanshell script.
std = new com.bethecoder.tutorials.bean_shell.common.Student ( "Sriram" , 2 , "Chess" ) ;
print ( "Name : " + std.name ) ; //Same as std.getName()
print ( "Age : " + std.age ) ; //Same as std.getAge()
print ( "Kid : " + std.kid ) ; //Same as std.isKid()
std.hobby = "Cricket" ; //Same as std.setHobby(str)
std { "hobby" } = std.hobby + " & Chess" ; //Same as std.setHobby(str)
print ( "Hobby : " + std.hobby ) ; //Same as std.getHobby()
package com.bethecoder.tutorials.bean_shell.common;
public class Student {
private String name;
private int age;
private String hobby;
public Student () {
}
public Student ( String name, int age, String hobby ) {
super () ;
this .name = name;
this .age = age;
this .hobby = hobby;
}
public String getName () {
return name;
}
public void setName ( String name ) {
this .name = name;
}
public int getAge () {
return age;
}
public void setAge ( int age ) {
this .age = age;
}
public String getHobby () {
return hobby;
}
public void setHobby ( String hobby ) {
this .hobby = hobby;
}
public boolean isKid () {
return age < 5 ;
}
public String toString () {
return "Student[name = " + name + ", age = " + age + ", hobby = " + hobby + "]" ;
}
}
package com.bethecoder.tutorials.bean_shell;
import java.io.InputStreamReader;
import bsh.EvalError;
import bsh.Interpreter;
public class SimpleSyntaxTest {
/**
* @param args
* @throws EvalError
*/
public static void main ( String [] args ) throws EvalError {
Interpreter interpreter = new Interpreter () ;
InputStreamReader reader = new InputStreamReader (
SimpleSyntaxTest. class .getClassLoader ()
.getResourceAsStream ( "simple_syntax.bsh" )) ;
interpreter.eval ( reader ) ;
}
}
It gives the following output,
Name : Sriram
Age : 2
Kid : true
Hobby : Cricket & Chess