atitit.bsh BeanShell 的動態指令碼使用java,beanshell
atitit.bsh BeanShell 的動態指令碼使用java
1.1. BeanShell是一個小巧免費的JAVA源碼解譯器
,支援對象式的指令碼語言特性,亦可嵌入到JAVA原始碼中。
亦可嵌入到JAVA原始碼中,能動態執行JAVA原始碼並為其擴充了指令碼語言的一些特性,像JavaScript和perl那樣的弱類型、命令式、閉包函數等等特性都不在話下
BeanShell能理解標準的JAVA語句,運算式,和方法宣告。語句和運算式的內容可以是:變數,宣告,賦值,方法調用,迴圈,條件等。
在 Java程式中你必須嚴格的使用它們,但在BeanShell中,你可以用“寬鬆類型”(loosely typed)的方式來使用它們。也就是說,你可以在寫指令碼時偷懶,不進行變數類型的宣告(在未經處理資料類型和對象都可以)。如果你試著用錯變數類 型,BeanShell將會給出一個錯誤。
作者:: 綽號:老哇的爪子 ( 全名::Attilax Akbar Al Rapanui 阿提拉克斯 阿克巴 阿爾 拉帕努伊 ) 漢字名:艾龍, EMAIL:1466519819@qq.com
轉載請註明來源: http://www.cnblogs.com/attilax/
2. 運行方式:
l 介面UI方式 :java bsh.Console
l 命令列方式 :java bsh.Interpreter
l運行指令檔:java bsh.Interpreter filename [ args ]
3. BeanShell將成為Java平台上的第三種程式設計語言
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平台上的第二種程式設計語言。
3.1. Define method3.2. Scripted Methods
You can declare and use methods in BeanShell just as you would in a Java class.
|
int addTwoNumbers( int a, int b ) {
return a + b;
}
sum = addTwoNumbers( 5, 7 ); // 12 |
Bsh methods may also allow dynamic (loose) argument and return types.
|
add( a, b ) {
return a + b;
}
foo = add(1, 2); // 3
foo = add("Oh", " baby"); // "Oh baby" |
3.3. Implementing Interfaces
Note: implementing arbitrary interfaces requires BeanShell be run under a Java 1.3 or higher environment.
You can use the standard Java anonymous inner class syntax to implement an interface type with a script. For example:
|
ActionListener scriptedListener = new ActionListener() {
actionPerformed( event ) { ... }
} |
You don't have to script all of the methods of an interface. You can opt to script only those that you intend to call if you want to. The calling code will simply throw an exception if it tries to invoke a method that isn't defined. If you wish to override the behavior of a large number of methods - say to produce a "dummy" adapter for logging - you can implement a special method signature: invoke(name, args) in your scripted object. The invoke() method is called to handle any undefined method invocations:
|
ml = new MouseListener() {
mousePressed( event ) { ... }
// handle the rest
invoke( name, args ) { print("Method: "+name+" invoked!");
} |
4. Refe
BeanShell(JAVA源碼解譯器)_百度百科.htm
BeanShell快速入門---Java應用程式指令碼引擎 - Love program - BlogJava.htm
Quick Start.htm
Embedding BeanShell in Your Application.htm
BeanShell Commands Documentation.htm