Importifier

Automatic Expansion of Java Import Statements

News

Introduction

Java provides two forms of import statements:

Import on demand is pure, unadulterated EVIL.

There are several problems with import-on-demand:

The importifier replaces all import statements with exactly the import statements your program requires.

How the tool works

First, it is important that the code you want to importify be error-free. The tool needs to examine the bytecode to determine the correct imports, as well as resolve ambiguities.

Second, you must version your classes and interfaces before running importifier against them. This is a safeguard, just in case importifier decides to have a bad fur day.

Importifier starts by exporting the code you wish to importify. The tool exports .class files to a temporary directory, so it can parse the bytecode.

Next, importifier parses the bytecode to see which classes are used. This is significantly easier than parsing the source code to find out the same information.

After the tool knows which classes are used, it walks through the source code of the class definition to remove the old import statements and add the new, fully-qualified ones. In many cases this is straightforward, but in cases where the short name of the classes are ambiguous (as in List being in both java.awt and java.util in the Java2 platform), importifier examines your old imports to see which full name it should keep.

If the tool sees any inner classes listed in the .class file, it recursively parses the inner classes to determine all imports needed in the containing class.

Distribution

Importifier is packaged as a tool for VisualAge for Java 3.02 and above. Importifier will not work on earlier versions of VisualAge for Java. Note that the latest release has not been tested on versions earlier than 3.5.3 but should work on 3.02.

Installation for VisualAge for Java - Tool Packaging

To use Importifier in VisualAge for Java:

  1. Download importifier.zip from this site.
  2. Unzip importifier.zip to the directory where VisualAge for Java is installed.  For example, if you installed VisualAge for Java in the c:\IBMVJava directory were on your C:\ drive, unzip the importifier.zip to C:\IBMVJava directory. Note that VisualAge for Java version 3.5 and above are typically installed under C:\Program Files\IBM\VisualAge for Java.
  3. Shutdown VisualAge for Java if it is running
  4. Start VisualAge for Java.

This will add the importifier tool to the Tools menu for classes, packages, and projects.

Note: If you are using VisualAge for Java 3.02 on Linux, you will need to download the tool api for linux. You can get this from VisualAge Developer Domain (http://www.software.ibm.com/vadd). The latest version of importifier has not been tested under Linux. Please let me know if you encounter any problems!

Use

VisualAge for Java

To use Importifier (once it is installed)

Command-Line

The importifier tool does not yet support command-line use. I will eventually release a version that includes command-line capability.

Limitations

This tool will not add imports for classes/interfaces from which you only use final variables.

Importifier reads the generated bytcode for the class being importified to figure out what imports are needed. If all you reference are constants (finals) in another class/interface, it won't see that class/interface because the java compiler resolves them, leaving no reference to that class/interface in the bytecode. Bummer...

IMHO, this is a design flaw in Java, but that's another issue... I'll have to see if there's some way I can fix it later, but don't know that I'll have time anytime soon. For now, you may have to add a few imports by hand after running importifier. It will still give you a great start toward freedom from import-on-demand, though.

This tool does not work on classes that exist in a default package! You should only use the tool on classes in a named package!

The tool does not change full-qualifications in the code to short names. I will add this capability in a later version of the tool.

You may need to clean up the code a bit after the tool is done, as it adds a bit of extra space. In many cases the space will be correct, but in some cases you may find there is an extra blank line between the imports and the class definition. Note also that the resulting file might look a bit strange if you had comments on the same lines or near the old import statements.

The tool will only work if the code to importify is error free!

The tool currently creates import statements for classes in the same package as well as classes in package java.lang. A later version of the tool may provide options to omit these imports if desired.

License

Importifier is free for any use other than selling it as a stand-alone product.

Note that Importifier is provided "as-is" without any warrantee or guarantee of suitability for any purpose. Scott Stanchfield cannot be held responsible for any damage caused by its use.

(In other words, use 'em for free at your own risk.)