Atitit. bsh & #160; BeanShell & #160; Dynamic scripts use java, beanshell

Source: Internet
Author: User

The dynamic scripts of atitit. bsh BeanShell use java and beanshell.

The dynamic script of atitit. bsh BeanShell uses java

 

 

1.1. BeanShell is a small and free JAVA source code interpreter

Supports object-based scripting and can be embedded into JAVA source code.

It can also be embedded into the JAVA source code. It can dynamically execute the JAVA source code and extend some features of the script language for it. features such as weak types, imperative types, closure functions, and so on, like JavaScript and perl, all come together.

 

BeanShell can understand standard JAVA statements, expressions, and method declarations. The statement and expression content can be: Variable, declaration, assignment, method call, loop, condition, and so on.
You must strictly use them in Java programs, but in BeanShell, you can use them in loosely typed mode. That is to say, you can be lazy when writing a script without declaring the variable type (both the original data type and object can be used ). If you try to use the wrong variable type, BeanShell will give an error.

Author: nickname: old wow's paw (full name: Attilax Akbar Al Rapanui Attila Akba Arla Panui) Name: AI long, EMAIL: 1466519819@qq.com

Reprinted please indicate Source: http://www.cnblogs.com/attilax/

 

2. Running Mode:

L UI mode: java bsh. Console

L command line: java bsh. Interpreter

L run the script file: java bsh. Interpreter filename [args]

3. BeanShell will become the third programming language on the Java platform

JCP accepts a new technical specification into the standardization process, which is numbered as a JSR-274 and will introduce BeanShell into another programming language supported on the Java platform.

JSR-274 (http://jcp.org/en/jsr/detail? Id = 274) is a technical specification submitted by Patrick Niemeyer whose goal is to normalize BeanShell scripting language (http://www.beanshell.org/) into a third programming language supported on the Java Virtual Machine Platform. In addition to Java, Java virtual machines also support the Groovy scripting language. Doug Lea, Apache, and Google members of the jcp Executive Committee expressed support for this specification.

According to the original design idea of Java, many languages can run on JVM (see http://en.wikipedia.org/wiki/List_of_Java_scripting_languages for a detailed list), but these languages are mostly not popular. Until 2004, the Java platform actually had only one programming language, namely Java. In March 2004, Groovy (JSR-241) became the second programming language on the Java platform.

3.1. Define method3.2. Scripted Methods

You can declare and use methods in BeanShell just as you wocould 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 (){

Actionreceivmed (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. 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 source code interpreter pai_baidu Baike .htm

BeanShell Quick Start --- Java application script engine-Love program-BlogJava.htm

Quick Start.htm

Embedding BeanShell in Your Application.htm

BeanShell Commands Documentation.htm

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.