Skip to content

Running Checkstyle Tree on Java files

romani edited this page Jan 22, 2013 · 1 revision

Oficial Checkstyle wiki says us, that if we want to execute Checkstyle build-in AST Viewer on Java file we should to execute in shell something like this:

$ java -classpath checkstyle-5.6-all.jar com.puppycrawl.tools.checkstyle.gui.Main

But it is very uncomfortable when you should to execute such commands in terminal every time you want to see an AST for Java file. So, we have a nice script which can simplify this job:

https://github.com/sevntu-checkstyle/sevntu.checkstyle/blob/master/sevntu-checks/src/main/sh/ast.sh

It shows the Checkstyle AST (abstract synthax tree) for custom Java file using simple built-in Checkstyle GUI.

usage: ./ast.sh <path_to_Java_file>

You can easy bind this script as default shell command using shell alias: simply add this line to the end of your ~/.bashrc file: alias ast='< path_to_sh >'. After this reopen the Terminal window (or open new) and type:

ast <path_to_java_file>

P.S. Experienced Checkstyle developers can install the 'Path tools' plugin into Eclipse and configure it to execute this script on custom Java file by right-clicking on file in Eclipse and choosing Path Tools --> Custom --> <Custom 'Show AST tree' command> HOWTO:

  1. Install 'Path tools' plugin into Eclipse form update-site below: http://pathtools.googlecode.com/svn/trunk/PathToolsUpdateSite/site.xml
  2. In Eclipse open: 'Preferences' --> 'Path Tools' and add the new custom FILE command with name=[Show AST Tree], pattern = [*.java] and command= [${workspace_loc:/sevntu-checks/src/main/sh/ast.sh} "{path}"] (all without [ ] braces)
  3. Right-click on Java file anywhere in Eclipse to execute created command: 'Path Tools' --> 'Custom' --> 'Show AST Tree'

Note, that during the first launch this script downloads the custom checkstyle 5.6 jar from remote server into your /tmp folder. We do not know how to get rid of it or do it another way, maybe next generations of Checkstyle writers will correct this )