BeanShell Simple Java Scripting

Categories:

Recommended

Introduction

This document is about BeanShell. BeanShell is a small, free, embeddable Java source interpreter with object scripting language features, written in Java. BeanShell executes standard Java statements and expressions but also extends Java into the scripting domain with common scripting language conventions and syntax. BeanShell is a natural scripting language for Java.

Scripting vs. Application Languages

Traditionally, the primary difference between a scripting language and a compiled language has been in its type system: the way in which you define and use data elements. You might be thinking that there is a more obvious difference here − that of “interpreted” code vs. compiled code. But the compiler in and of itself does not fundamentally change the way you work with a language. Nor does interpreting a language necessarily make it more useful for what we think of as “scripting”. It is the type system of a language that makes it possible for a compiler to analyze the structure of an application for correctness. Without types, compilation is reduced to just a grammar check and an optimization for speed. From the developer’s perspective, it is also the type system that characterizes the way in which we interact with the code.

Types are good. Without strongly type languages it would be very hard to write large scale systems and make any assertions about their correctness before they are run. But working with types imposes a burden on the developer. Types are labels and labeling things can be tedious. It can be especially tedious during certain kinds of development or special applications where it is flexibility and not program structure that is paramount. There are times where simplicity and ease of use is a more important criterion.

This is not just rationalization to cover some underlying laziness. Productivity affects what people do and more importantly do *not* do in the real world, much more than you might think. There is a lot of important software that exists in the world today only because the cost/benefit ratio in some developer’s mind reached a certain threshold.

Unit testing − one of the foundations of writing good code − is a prime example. Unit tests for well written code are, in general, vitally important as a collective but almost insignificant individually. It’s a “tragedy of the commons” that leads individual developers to repeatedly weigh the importance of writing another unit test with working on “real code”. Give developers have a tool that makes it easy to perform a test with a line or two of code they will probably use it. If, moreover, it is also a tool that they enjoy using during their development process − that saves the time, they will be even more inclined to use it.

Customizability through scripting also opens the door to applications that are more powerful than the sum of their parts. When users can extend, enhance, and add to their applications they use them in new and unexpected ways.

Scripting is powerful.

Tearing Down the Barriers

Traditionally scripting languages have traded in the power of types for simplicity. Most scripting languages distill the type system to just one or a handful of types such as strings, numbers, or simple lists. This is sufficient for many kinds of scripting.

Many scripting languages operate in a loose, unstructured land − a place dominated by text and course−grained tools. As such these scripting languages have evolved sophisticated mechanisms for working with these simple types (regular expressions, pipes, etc.). As a result there has developed a casm between the scripting languages and the application languages created by the collapse of the type system in−between. The scripting languages have remained a separate species, isolated and speaking a different dialect from their brothers the application languages.

BeanShell is a new kind of scripting language. BeanShell begins with the standard Java language and bridges it into the scripting domain in a natural way, but allowing the developer to relaxing types where appropriate. It is possible to write BeanShell scripts that look exactly like Java method code. But it’s also possible to write scripts that look more like a traditional scripting language, while still maintaining the framework of the Java syntax.

BeanShell emulates typed variables and parameters when they are used. This allows you to “seed” your code with strong types where appropriate. You can “shore up” repeatedly used methods as you work on them, migrating them closer to Java. Eventually you may find that you want to compile these methods and maintain them in standard Java. With BeanShell this is easy. BeanShell does not impose a syntactic boundary between your scripts and Java.

But the bridge to Java extends much deeper than simple code similarity. BeanShell is one of a new breed of scripting languages made possible by Java’s advanced reflection capabilities. Since BeanShell can run in the same Java virtual machine as your application, you can freely work with real, live, Java objects − passing them into and out of your scripts. Combined with BeanShell’s ability to implement Java interfaces, you can achieve seamless and simple integration of scripting into your Java applications. BeanShell does not impose a type boundary between your scripts and Java.

Category:

Attribution

Beanshell Simple Java Scripting Manual. beanshell.org. http://www.beanshell.org/manual/bshmanual.pdf

VP Flipbook Maker

Make stunning flipbooks online. Create flipbooks for free with Visual Paradigm. Create online flipbooks, design, publish and share your flipbooks online, try it now!