Eclipse UI and Scripting

Scripting opens up an entire new world for working with ProRefactor and the syntax tree. As of version 1.7.2, the Groovy scripting engine has been added to ProRefactor's bundled libraries.

Using Scripts with ProRefactor in Eclipse

A menu item has been added to the context menu on the Navigator view: ProRefactor -> Tool Devel Utils -> Groovy Console. If you have a .p, .w, or .cls file selected when you launch the Groovy console, then within your console the File variable selectedFile is available for your script.

For example, you could select a .p, launch the Groovy Console, and then do something like this:

import org.prorefactor.core.JPNode
import org.prorefactor.core.TokenTypes
import org.prorefactor.treeparser.ParseUnit

indent = 0
def walker(node) {
	if (!node) return
	println '  '*indent + TokenTypes.getTokenName(node.getType()) + ' ' + node.getText()

ParseUnit pu = new ParseUnit(selectedFile)

To see what variables are available in your Groovy Console, select Actions -> Inspect Variables from the console menu. The following little script shows something similar:

binding.variables.each {key, value ->
  if (key[0] == '_') return
  println "$key : $value"

I found a way to get my own scripts directory into the classpath when using the console:

println this.class.classLoader.classPath


Provides a wrapper for the console. (Download below)

The run() method launches the console and causes this thread to sleep until the console's window is closed. Allows easy interaction with the objects alive at a given point in an application's execution, similar to a debugger session.

Example 1:

new ConsoleWaiter().run()

Example 2:

def waiter = new ConsoleWaiter()
waiter.console.setVariable("node", node)

NOTE: I've run across a documented bug. If launching a GUI Swing application (like the Groovy Console) from Eclipse running under Java 6, the JVM is likely to crash. This doesn't happen with Java 5.

I found that if I am editing .groovy scripts, and testing them from the console, that in some cases the .groovy class files get cached and don't get reloaded. I use the following to force the cache to be cleared. (Substitute MyScript with your own .groovy class name.)