Hendrik.groovy

This Groovy script finds variables which use the same name as a database table or field name. It writes its findings, as it runs, to the console. It also accumulates a sorted report which it writes at the end of its run.

I wrote this as a test case for Groovy scripting for ProRefactor. A better place for it would be as a rule in Prolint.

I decided to write this after seeing a post on the PEG from Hendrik Demol looking for a way to do this. Assuming he doesn't object, Hendrik gets the Groovy script named after him. :)

The file download can be found at the bottom of this page.

Requirements

  • Latest version of the ProRefactor standalone jar file.
  • Latest version of Proparse.
  • Java 5 or later.

Setting up standalone ProRefactor

See this page, and follow the instructions there, for getting and using the ProRefactor standalone jar file:
Standalone Jar File
See this page, and follow the instructions there, to generate the project settings files necessary for using ProRefactor:
Project Config. Dump

When launching the script, the prorefactor directory, with its project settings files, must be found in the current working directory.

Note that ProRefactor generates large volumes of data files in its prorefactor directory.

Command line

The script expects the source code to be found in the current working directory. That can easily be tweaked if necessary, find this:

    // Look from the current (working) directory down.
    new File('.').eachFileRecurse{ file ->

and change '.' to '/your/directory/' if you don't want to run the script from the top of your source tree.

The basic command line to launch a Groovy script like this via ProRefactor's standalone jar is:

java -Xss2M -Xmx128M -cp prorefactor.jar groovy.ui.GroovyMain hendrik.groovy

You might want to redirect the output to a file, or tweak the output destination of the sorted report output at the end of the run() method in the script.

ProRefactor API documentation

If you want to tweak this script for your own use, you might want to see the ProRefactor API documentation. See the javadoc page for how to find ProRefactor javadoc online.

Peeking at the script

Much of the script is scaffolding, for example, finding and loading the ProRefactor project settings files, iterating through compile unit source files and parsing them, etc. There are methods for building sorted lists of table and field names, and looking up names to see if they are unique in those lists.

The run() method is what gets run when the Groovy script is launched. It iterates through all of the .p, .w, and .cls files in the directory.

The checkSymbols() method does the really interesting work of iterating through all of the symbols (variables, frames, streams, etc) in the compile unit, and checking their names to see if they are also valid table or field names.


AttachmentSize
Hendrik.groovy5.86 KB