Beanshell研究

 最近项目中用到了ZK  和DWR ,顺便研究一下BEANSHELL.

Beanshell是用Java写成的,官方最新版本,bsh-2.0b4.jar。一个小型的、免费的、可以下载的、嵌入式的Java源代码解释器,具有对象脚本语言特性。BeanShell执行标准Java语句和表达式,另外包括一些脚本命令和语法。它将脚本化对象看作简单闭包方法(simple method closure)来支持,就如同在Perl和JavaScript中的一样。 它具有以下的一些特点:使用Java反射API以提供Java语句和表达式的实时解释执行;可以透明地访问任何Java对象和API;可以在命令行模式、控制台模式、小程序模式和远程线程服务器模式等四种模式下面运行;与在应用程序中一样,可以在小程序中(Applet)正常运行(无需编译器或者类装载器);

JCP接纳了一个新的技术规范进入标准化进程,这个编号为JSR-274的技术规范将把BeanShell引入为Java平台上支持的又一种编程语言。

JSR-274(http://jcp.org/en/jsr/detail?id=274)是由 Patrick Niemeyer提交的技术规范,其目标是将BeanShell脚本语言(http://www.beanshell.org/)规范化为Java虚拟机平台上支持的第三种编程语言。除了Java之外,Java虚拟机还支持Groovy脚本语言。Doug Lea、Apache和Google三个JCP执委会成员对此规范表示了支持。

按照Java最初的设计思路,有很多语言都可以在JVM上运行(详细列表参见http://en.wikipedia.org/wiki/List_of_Java_scripting_languages),但这些语言大多没有流行起来。直到2004年为止,Java平台事实上只有一种编程语言,也就是Java。2004年3月,Groovy(JSR-241)成为了Java平台上的第二种编程语言。

设置环境
l 把;bsh-xx.jar放到$JAVA_HOME/jre/lib/ext文件夹下
l unix: export CLASSPATH=$CLASSPATH:bsh-xx.jar
l windows: set classpath %classpath%;bsh-xx.jar

运行方式:
l 界面UI方式 :java bsh.Console
l 命令行方式 :java bsh.Interpreter
l 运行脚本文件:java bsh.Interpreter filename [ args ]



简单举例:
在classpath中设置好环境变量,打开dos窗口,键入:java bsh.Console命令
出现BeanShell图片代表设置成功,beanshell开始运行





测试内容:
设置变量
foo = "Foo";
four = (2 + 2)*2/2;
打印变量
print( foo + " = " + four );
循环
for (i=0; i<5; i++)
print(i);
在窗口中打印按钮
button = new JButton( "My Button" );
frame = new JFrame( "My Frame" );
frame.getContentPane().add( button, "Center" );
frame.pack();
frame.setVisible(true);

完整代码:
foo = "Foo";
four = (2 + 2)*2/2;
print( foo + " = " + four );

for (i=0; i<5; i++)
print(i);

button = new JButton( "My Button" );
frame = new JFrame( "My Frame" );
frame.getContentPane().add( button, "Center" );
frame.pack();
frame.setVisible(true);


在窗口中输入上面的代码

敲回车执行,运行结果如图


说明:
因为beanshell是松散类型的脚本语言因此可以直接写
foo = "Foo";
four = (2 + 2)*2/2;

print是beanshell提供一种简单的打印命令相当于java中的System.out.println()





其他的beanshell脚本命令
· source(), run() – 读取,或者运行一个脚本文件
· frame() – 显示一个窗口
· load(), save() – 读取或者保存一个脚本对象到文件
· cd(), cat(), dir(), pwd(), etc. 使用Unix下面的命令
· exec() – 运行一个本地的方法
· javap() –使用javap命令.
· setAccessibility() – 设置可以接收private和protected类型的变量
BeanShell命令不一定都是内置的脚本命令,脚本方法会自动从classpath中取方法使用,因此你可以添加你自己的脚本到classpath中来扩充基本的命令






脚本方法
一般的方法:
int addTwoNumbers( int a, int b ) {
return a + b;
}

sum = addTwoNumbers( 5, 7 ); // 12
也可以使用动态的变量类型(无状态)方法
add( a, b ) {
return a + b;
}
foo = add(1, 2); // 3
foo = add(1, “2”); //”12” 只要有一个为字符串全部按照字符串处理,系统不会根据1是数字在前把“2”转换成数字处理(特别注意)
foo = add("Oh", " baby"); // "Oh baby"




实现接口
实现任何接口需要java1.3或者更高
可以使用缺省的java匿名类的语法实现一个接口类,例如:
ActionListener scriptedListener = new ActionListener() {
actionPerformed( event ) { ... }
}
不需要实现接口的所有的方法,只需要实现你使用的方法即可,如果使用你没有实现的方法,beanshell将抛出一个错误,
ml = new MouseListener() {
mousePressed( event ) { ... }
// handle the rest
invoke( name, args ) { print("Method: "+name+" invoked!");
}








脚本对象
使用特殊的关键字this可以创建一个对象(根JS类似)
foo() {
print("foo");
x=5;

bar() {
print("bar");
}

return this;
}

myfoo = foo(); // prints "foo"
print( myfoo.x ); // prints "5"
myfoo.bar(); // prints "bar"

从应用程序中调用BeanShell
创建一个BeanShell的解释器(interpreter)用eval()和source()命令可以对一个字符串求值和运行一个脚本文件
使用set()方法可以给一个对象传入一个变量的参考
使用get()方法可以重新得到一个变量的结果


完整代码:
package cn.com.sparknet.util;

import bsh.*;
import java.util.*;

public class BeanShell {
public static void main(String[] args) {
try {
Interpreter interpreter = new Interpreter(); // 构造一个解释器
interpreter.set("foo", 5); // 设置变量
interpreter.set("date", new Date()); //设置一个时间对象
Date date = (Date) interpreter.get("date"); // 重新得到时间变量
interpreter.println(date.toString()); //打印时间变量
interpreter.eval("bar = foo*10"); // 对一段脚本求值,并得到结果
System.out.println(interpreter.get("bar")); //打印变量
interpreter.source("d://helloWorld.bsh"); // 导入并执行一个脚本文件
}
catch (Exception e) {
//如果发生异常,写入日志文件
Log.error(new BeanShell(), "main", FormatDate.getCurrDate(), e.getMessage());
}
}
}






BeanShell语法
BeanShell是一种最原始的java解释器。
标准的java语法
/*
Standard Java syntax
*/

// Use a hashtable
Hashtable hashtable = new Hashtable();
Date date = new Date();
hashtable.put( "today", date );

// Print the current clock value
print( System.currentTimeMillis() );

// Loop
for (int i=0; i<5; i++)
print(i);

// Pop up a frame with a button in it
JButton button = new JButton( "My Button" );
JFrame frame = new JFrame( "My Frame" );
frame.getContentPane().add( button, "Center" );
frame.pack();
frame.setVisible(true);
松散类型的java语法
/*
Loosely Typed Java syntax
*/

// Use a hashtable
hashtable = new Hashtable();
date = new Date();
hashtable.put( "today", date );

// Print the current clock value
print( System.currentTimeMillis() );

// Loop
for (i=0; i<5; i++)
print(i);

// Pop up a frame with a button in it
button = new JButton( "My Button" );
frame = new JFrame( "My Frame" );
frame.getContentPane().add( button, "Center" );
frame.pack();
frame.setVisible(true);
异常处理
标准的java异常
try {
int i = 1/0;
} catch ( ArithmeticException e ) {
print( e );
}
松散的异常处理(类似JS)
try {
...
} catch ( e ) {
...
}
松散类型变量的作用范围
标准的java程序的变量作用范围是在一个模块中的(在模块中声明的变量),而在松散类型的语言中如果在一个模块中没有指定一个变量的类型,则认为是一个全局变量(只有它以后的代码可以使用该变量,系统在调用该变量的时候自动生成一个全局变量,也就为什么在调用模块之前不能使用该变量的原因)
// Arbitrary code block
{
y = 2; // Untyped variable assigned
int x = 1; // Typed variable assigned
}
print( y ); // 2
print( x ); // Error! x is undefined.

// Same with any block statement: if, while, try/catch, etc.
if ( true ) {
y = 2; // Untyped variable assigned
int x = 1; // Typed variable assigned
}
print( y ); // 2
print( x ); // Error! x is undefined.

同样也使用于for-loop, if-else等循环语句
for( int i=0; i<10; i++ ) { // typed for-init variable
j=42;
}
print( i ); // Error! 'i' is undefined.
print( j ); // 42

for( z=0; z<10; z++ ) { } // untyped for-init variable
print( z ); // 10









方便灵活的语法
标准的java语法
java.awt.Button button = new java.awt.Button();
button.setLabel(“javaButton”);
松散的语法
button = new java.awt.Button();
button.label = "my button";
你也可以使用{}来对一个对象设置属性
b = new java.awt.Button();
b{"label"} = "my button"; // Equivalent to: b.setLabel("my button");

h = new Hashtable();
h{"foo"} = "bar"; // Equivalent to: h.put("foo", "bar");




包装和未包装(box和unbox)
BeanShell自动转为简单类型
i=5;
iw=new Integer(5);
print( i * iw ); // 25
导入类和包
import javax.xml.parsers.*;
import mypackage.MyClass;
超级导入法:
import

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值