你的位置:首页 > 软件开发 > Java > atitit.bshBeanShell的动态脚本使用java

atitit.bshBeanShell的动态脚本使用java

发布时间:2016-02-18 02:00:14
atitit.bsh BeanShell 的动态脚本使用java 1.1. BeanShell是一个小巧免费的JAVA源码解释器,支持对象式的脚本语言特性,亦可嵌入到JAVA源代码中。亦可嵌入到JAVA源代码中,能动态执行JAVA源代码并为其扩展了脚本语言的 ...

atitit.bsh BeanShell 的动态脚本使用java

 

 

1.1. BeanShell是一个小巧免费的JAVA源码解释器

,支持对象式的脚本语言特性,亦可嵌入到JAVA源代码中。

亦可嵌入到JAVA源代码中,能动态执行JAVA源代码并为其扩展了脚本语言的一些特性,像JavaScript和perl那样的弱类型、命令式、闭包函数等等特性都不在话下

 

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  method

3.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

 


原标题:atitit.bshBeanShell的动态脚本使用java

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。