Home

Instantiations, Inc.

        VA Assist product family IBM Solution Exellence Award

 

VA Assist HomeProduct DetailsPurchaseDownloadDocumentationLiteratureTechnical SupportInstantiations Home PageSearch Instantiations' Web


IBM Business Partner

Address & Copyright

Custom Code Formatters
with thanks to Scott Stanchfield

ent_only.jpg (778 bytes) VA Java 3.5 & Above Only

VA Java provides a built-in facility to format Java source code. It offers a variety of customization options that can be used to control the results. If you find the default VA Java code formatter insufficient to your needs, VA Assist provides a mechanism to allow you to substitute your own custom code formatter.

You can replace the VA Java formatter with any other formatter you like as long as it conforms to the same (simple) interface. Basically, you are passed in the string to be formatted and an array of options objects (representing the various options you can set from the VA Java Formatter options page). Once you have that, you can do anything to the string that you want before passing it back . This means that you can any of the following:

  • Do nothing and just pass back the string (e.g., a no-op formatter)
  • Call the default VA Java formatter and just pass back the results (should act
    just as it does now)
  • Call the default VA Java formatter and tweak the results
  • Write your own formatter within VA Java with as much or as little sophistication as you need
  • Call out to any other formatter that you have access to (such as JIndent, JPretty, JStyle, etc.)
  • Automatically add copyright text to every formatted method

Available code formatters

As various custom code formatters become available, they will be listed here:

To create a custom code formatter:

  1. Create a project in your workspace named LFC.
     
  2. Import the lfc.jar file into your LFC project. This is typically found in c:\Program Files\IBM\VisualAge for Java\ide\program\lib\lfc.jar, but that location may vary depending on where you installed VisualAge for Java.
  3. Download sample-code-formatters.zip and unzip it within your VA Java directory (make sure to preserve the directory structure). This should create an \ide\features\com-javadude-code-formatter directory.
     
  4. Add feature JavaDude Custom Code Formatter Samples to your workspace. You can do this by going to File->Quick Start->Features->Add Feature and choose JavaDude Custom Code Formatter Samples. Note that you do not need this feature to run your formatter, but it provides samples you can use as a template when writing your own formatter.


     
  5. Create a new project to contain your custom formatter. You can name this whatever you'd like. For purposes of explanation, we'll assume you name it My Formatter.
     
  6. Copy one of the sample formatter classes from com.javadude.code.formatter into your project. Place it in any package you would like, and give it whatever name you would like. Note that your formatter must have a format() method with the exact signature found in these sample formatters.
     
  7. Create code in your class' format() method to format the passed-in code string and return it. We recommend you do any String modification in a StringBuffer rather than using Strings, as it's more efficient.

    For a simple example of a custom formatter, see com.javadude.code.formatter.SampleCodeFormatter.

    You can call the default IBM VisualAge for Java formatter if you'd like (see the examples above), or you can do all of the formatting yourself.

  8. Add the location of your code to the workspace classpath. Your code can reside in the workspace (mainly for edit/testing), in a JAR file, or in a directory. To set the workspace classpath:
    1. Choose Window->Options->Resources
    2. Press Edit
    3. To add a JAR or Directory, press the appropriate button and select the JAR file or directory in the file chooser.
    4. To add a project from the workspace, add its project_resources directory. For example, if your project were My Formatter, you would add directory c:\Program Files\IBM\VisualAge for Java\ide\project_resources\My Formatter to your workspace classpath.
       
  9. Change the name of the Custom Formatter class on the Window->Options->"Coding | Formatter | Custom" options page.
     
  10. When you press control-w in the editor, your custom formatter will be called!

Note: more detailed information on creating custom code formatters can be found at:

http://javadude.com/vaj/resources/custom-formatters.html


IBM VisualAge Development Products

VisualAge and the VisualAge logo are trademarks of the IBM Corporation.