© Adam L. Davis 2019
Adam L. DavisLearning Groovy 3https://doi.org/10.1007/978-1-4842-5058-7_3

3. Tools

Adam L. Davis1 
(1)
New York, NY, USA
 

In addition to groovy, the Groovy installation comes with several helpful tools, covered in this chapter.

Console

1   groovyConsole

The Groovy Console is a quick and easy way to try things in Groovy visually without the overhead of a complete IDE.

Whenever you have an idea you want to try out quickly, open the Groovy Console, type some code, and then press Ctrl+R to run it. After reading your output and changing the code, press Ctrl+W to clear the output and Ctrl+R again to run the code. Once you get used to those two shortcuts, the Groovy Console might become an indispensable development tool.

It also has the ability (among other things) to inspect the AST (Abstract Syntax Tree) of your code, the internal representation of the code used by the compiler. Use Script - Inspect Ast or Ctrl+T to open the Groovy AST Browser.

You can provide a classpath to be available at runtime to the Groovy Console using the -cp option. This is useful when you want to refer to other classes you have compiled. For example, in a Linux/OSX environment, you use the following (where “:” is used to separate paths):
1   groovyConsole -cp src/main/groovy/:src/main/resources/ example.groovy

Compilation

1   groovyc

Much like javac, groovyc compiles Groovy code to JVM byte-code (*.class files). It is not strictly necessary to compile before running. You can use the groovy command to run a groovy script file which typically end in .groovy but can have any extension.

To take advantage of the JDK 7+ invoke-dynamic instruction, use the --indy flag.1 This also works with the groovy command.

Invoke-dynamic helps the compiler improve the performance of things like duck-typing, meta-programming, and method-missing calls.

Shell

1   groovysh

The Groovy shell can be used to execute Groovy code in an interactive command shell.

../images/426440_2_En_3_Chapter/426440_2_En_3_Figa_HTML.gif Exercise

Try it out!

Documentation

1   groovydoc

This tool generates documentation from your Groovy code.

Groovy uses the same comment syntax as Java, including the conventions for documenting code.
1   /** This is a documentation comment. */
2   /* This is not */
3   // This is a one-line comment.
Groovy docs can even be accessed as metadata using the groovy.attach.groovydoc command line option.2 For example, using the following at the command line:
groovyc -Dgroovy.attach.groovydoc=true Test.groovy
Given that you put the following code in Test.groovy:
/** My class */ class Test { /** My method */ def method() {} }

You could access those documents from the AST at compile time (this can be useful when writing AST transformations, for example).

Also, Groovy docs can be accessed at runtime using the groovy.attach.runtime.groovydoc command line option. For example, using the following at the command line:
groovyc -Dgroovy.attach.runtime.groovydoc=true Test.groovy
Given the following code in “Test.groovy”:
class Test { /**@ My metadata */ def method() {} }
You can now access the preceding comment metadata using the following code (in Groovy 3):
def data = Test.methods.find{ it.name=='method' }.groovydoc.content
assert data == 'My metadata'
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset