Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Kenneth A. Kousen - Making Java Groovy - 2014.pdf
Скачиваний:
50
Добавлен:
19.03.2016
Размер:
15.36 Mб
Скачать

Code-level integration

This chapter covers

Calling Groovy scripts from Java using JSR 223

Calling Groovy scripts from Java using Groovy library classes

In chapter 1 I reviewed many of Java’s arguable weaknesses and drawbacks and suggested ways that Groovy might help ameliorate them. Because that chapter was intended to be introductory, I only suggested how Groovy can help, without showing a lot of code examples.

This chapter begins an examination of Java and Groovy integration in detail. In this chapter I’ll start using Groovy and Java together in fundamental ways, without worrying about frameworks or addressing any particular use case. A guide to the techniques discussed in this chapter is shown in figure 3.1.

3.1Integrating Java with other languages

Combining Java with other languages has always been a challenge. Java historically hasn’t played well with others.1 The only API designed from the beginning for Java

1 Of course, this is true of most languages.

46

www.it-ebooks.info

 

 

Integrating Java with other languages

47

Java

 

JSR 223

 

 

 

 

ScriptEngine

 

 

Java +

Eval

GroovyShell

Java and

 

Groovy

and binding

Groovy classes

 

 

 

Figure 3.1 Guide to integration features. Groovy can be accessed with Java classes alone using the JSR 223 script engine. If you are willing to add some Groovy library classes to Java, the Eval, GroovyShell, and Binding classes make working with scripts easy. The best way to combine Groovy and Java is using classes for both languages.

to call functions written in other languages is JNI, the Java Native Interface, which is awkward to use even in the best of circumstances.2 The past few years, however, have seen the rise of entire families of languages that compile directly to bytecodes that run on the JVM, from Groovy to Scala to Clojure, as well as bridge languages like Jython or JRuby that allow you to run code written in Python or Ruby on the JVM. From the point of view of these “alternative” JVM-based languages, Java’s real contribution isn’t the language itself, but rather the virtual machine and the associated Java libraries. JVM-based languages take advantage of the Java infrastructure and try to handle any Java-specific drawbacks.

JVM Ultimately, Java’s biggest contribution isn’t the language; it’s the virtual machine.

Whenever a new capability is integrated into Java’s basic infrastructure, a Java Specification Request (JSR) is created to provide a standard implementation mechanism. In the integration case, the JSR in question is JSR 223, Scripting for the Java Platform (http://jcp.org/en/jsr/detail?id=223). The purpose of the JSR is to allow other (presumably scripting) languages to be invoked from Java. Although most of this book will assume that you’re mixing Java and Groovy on a class-by-class basis, for the sake of completeness I’ll review here how to call a Groovy script from Java, both using the JSR technique and using library classes provided by Groovy for that purpose.

Groovy is much closer to Java than the script integration story suggests, however. As I’ll demonstrate in the section on calling Java from Groovy rather than the other way around, virtually every Groovy program of any size uses Java already. Groovy code can instantiate a Java class, call a method Groovy added to it (the so-called

2Once, back in the late 1990s, I had to build a Java Swing user interface in front of an engineering system written in Fortran. I used JNI to go from Java to C and then from C to Fortran. The results were like putting a notch into a wooden beam and saying, “I want you to break right here.”

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]