LazyImporter | Version 1.06 (Jan 19, 2004)
By Matt Etheridge <matt@etheridge.org> |
Introduction |
The LazyImporter plug-in provides useful tools for generating, organizing and sorting import statements in a Java source file in JEdit. Actions available include:
This version requires (at least):
Using the LazyImporter Actions |
This section describes each of the tools provided by the LazyImporter plugin in more detail. Each of these actions has a menu item by default, but you can also add keyboard shortcuts for each of these actions (highly recommended, especially for those actions used often). Note that these actions will only work if the currently browsed file in JEdit is a *.java file.
NOTE: some actions work best when the Java file you are working on actually
compiles. These will be pointed out below.
This action attempts to resolve any wildcard (package) imports that are present in the current Java source file. For example, if I had the import:
import java.io.*;and I was using the java.io.File, java.io.FileFilter and java.io.IOException classes in the current Java source file, invoking this action would replace the above wildcard import with the following:
import java.io.File; import java.io.FileFilter; import java.io.IOException;When resolving these wildcard imports, the LazyImporter plugin uses the classpath specified in the LazyImporter ClassPath options panel. This is explained in more detail later.
This action may not resolve the wildcard imports correctly if the class does not compile. For example, if I had two import statements as follows:
import java.util.*; import java.awt.*;and in the current Java source code I was using both java.util.List and java.awt.List, the class will NOT compile because the imports are ambiguous. If this action is invoked in this scenario, unpredictable import resolutions will occur.
NOTE: in this situation you can use the "Import Class Under Cursor"
action to explicitly choose which import statements to add to the class.
This action looks at all explicit imports (these are import statements
that import a class, not a wildcard (.*) package import) that are currently
in the Java source code, and removes those that are not used. As
with resolving wildcard imports, the LazyImporter plugin uses the classpath
specified in the LazyImport ClassPath options panel.
Resolve and Insert Missing Imports
This action attempts to find any import statements that are missing and, if any are found, will insert the import statements into the Java source. The algorithm used to find any missing imports only searches in those packages that are already imported (both explicit and wildcard imports). If classes are being imported from many different packages, this action could take a while.
Of course, if your class compiles it means you have NO missing import statements, so this action is not for that case. This action should generally be used when you are importing multiple classes from packages you already have explicit import statements for. So, if you use another class from these packages, you can use this action to quickly resolve and insert the import statement.
NOTE: you can also use the "Import Class Under Cursor" action to
explicitly import a class.
Convert Imports to Wildcard Imports
This action converts any explicit class imports to wildcard (package) imports. For example, if I had the following imports:
import java.util.Date; import java.util.StringBuffer; import java.io.IOException; import java.io.File;this action would remove the explicit imports and insert the following import statements:
import java.util.*;
import java.io.*;
Find Selected Import In Classpath
If the cursor is currently over an import statement this action, when invoked, displays the element(s) in the classpath where this class could be imported from. The first element in the classpath will be highlighted, as this is the one that will actually be used to load the class. Subsequent classpath elements where this class is found are also listed.
For example, if the cursor was currently anywhere within the bounds of the following import statement:
import java.util.Date;a pop-up message window will appear showing the classpath element that this class was imported from such as "D:\jdk1.3.1\jre\lib\rt.jar" or "D:\classes".
An example pop-up window is shown below:
NOTE: this action currently only works for explicit class imports!
When the cursor is currently over a class name (ie. List, StringBuffer) this action, when invoked, displays a pop-up list of possible imports (if any are found). An import statement can be selected from the popup list by pressing <enter> or double-clicking. The selected import statement will be inserted into the class at the correct position.
An example screenshot of the pop-up list of imports is shown below:
If this action is invoked, and only one possible import statement
is found, this action can be configured insert the import statement without
popping up the list of imports (which would only have one import statement
anyway). This can be configured via the "Miscellaneous Options Panel".
This action is really just a collection of some of the above actions so that they can be performed in one hit, instead of invoking multiple actions. The actions that are performed when you do a QuickImport can be one (or more) of the following actions:
The actions performed during a QuickImport can be specified in the "QuickImport Options Pane".HINT: if you simply want to draw (and hence reorder and group your
imports according to your configuration) you can uncheck all of the above
three actions in the QuickImport Options pane.
LazyImporter Options |
The actions performed by the LazyImporter plugin are highly configurable. These options can be changed using the Utilities -> Global Options -> Plugin Options menu item in jEdit.
This section describes each LazyImporter configuration panel in more detail. The configurtion panels include:
This option panel allows the user to set the classpath used by the LazyImporter when resolving wildcard imports, resolving missing imports, finding the selected import in the classpath and importing the class under the cursor.
There are three main parts to this option panel:
User Defined Class Path
The first part is a text area where the class path can be pasted/typed
in. This is a simple textual version of the classpath - the same
as what you may have as your CLASSPATH environment variable.
System.out.println(System.getProperty("java.class.path")); This will print the classpath to standard out. This string can then be pasted into the user-defined classpath text area. |
NOTE: the ordering is important! When classes are loaded in Java, the classloader always starts looking at the beginning of the classpath!
This part of the option panel also contains an option to choose class path elements (jar / zip files or class directories) using a file chooser. The file chooser will allow you to select multiple files/directories at once. Any files/directories selected will be appended to the end of the user-defined classpath. To open the file chooser, press the "Select" button.
Java Runtime Jar (rt.jar)
This part of the option pane allows the user to specify whether the user wants to include an rt.jar (Java's runtime java archive) in the LazyImporter classpath and, if so, where to find it. There are three options here:
Classpath Refresh Rate
This part of the option pane allows the user to specify how often the classpath (specified above) should be refreshed. This refreshing is important in certain cases, for example, if one of the classpath elements was:
D:\classes
... and, while programming in jEdit, I created a new Java class called Test.java and compiled it to D:\classes. The LazyImporter plugin needs to re-look at the classpath to pick up any new (like Test.class) or removed classes.
This part of the option pane lets the user specify how often the classpath is refreshed in LazyImporter. The value entered is in seconds. The default value is one minute (60 seconds).
A screenshot of the LazyImporter Classpath Options panel is shown below:
This option panel allows the user to configure the ordering of import statements when they are drawn back to the Java source. The following sorting options are available:
LazyImporter supports two types of layout configuration, automatic or manual. You can choose to have either:
Automatic Layout Configuration
This option panel allows the user to configure how the import statements are grouped, and to define the spaces between each group. The grouping mechanism is based upon the level of the import statement. The level is defined by the number of package names in the import statement. The level is used to determine how many packages we should match and group together. Now, this can be a little confusing to understand, so a preview panel is provided to show how changes in the level (and grouping) will affect the drawing of the import statements.
The grouping mechanism will be explained a little further with an example. If the Java source file has the following import statements:
import java.util.ArrayList; import java.util.List; import javax.naming.Context; import javax.rmi.PortableRemoteObject;A grouping level of 0 would not group the import statements at all, so they would look as they do above. However, if a grouping level of 1 was applied, the import statements would look like:
import java.util.ArrayList; import java.util.List;
import javax.naming.Context; import javax.rmi.PortableRemoteObject;You can see that the import statements are now in two groups, based upon the first level of package names. (java, javax). If a grouping level of 2 was applied, the import statements would look like:
import java.util.ArrayList; import java.util.List;
import javax.naming.Context;
import javax.rmi.PortableRemoteObject;Now the import statements are grouped according to the first and second level of package names. (java.util, javax.naming, javax.rmi). This results in two import statements in the first group, and one each in the subsequent two groups.
The preview panel will help show you how the import statements will be grouped at different levels.
This option pane also allows you to define how many newlines are placed in between each import statement group. This would normally be set to only 1 newline, however, for some reason you may want more.
A screenshot of this option pane is shown below:
NOTE: if you choose NO newlines between groups, the grouping level you choose is irrelevant, and vice versa!
Manual Layout Configuration
Manual layout configuration allows the user to specify which groups of imports are together and in what order. Blank lines can also be configured to allow spaces to be inserted between groups of import statements if required. An example of a manual layout configuration is as follows:
org.etheridge <BLANK LINE> * (all remaining imports)
The above configuration would group all imports that start with
org.etheridge
at the top, and all remaining import statements under these, separated
by a single blank line. For example:
import org.etheridge.lazyimporter.LazyImporterPlugin; import org.etheridge.lazyimporter.gui.ImportListWindow; import javax.swing.JWindow; import java.util.Date; import com.sun.java.util.collections.List;
NOTE: leading/trailing blank lines (at the top or bottom of the configuration list) will be ignored!
NOTE: both the automatic and manual grouping (and sorting for that
matter) configuration you set will only take affect when you choose one
of the LazyImporter actions that REDRAW the import statements.
This option pane is for any miscellaneous options that did not belong elsewhere. The options include:
This option pane is used to define which LazyImporter actions should be performed when the QuickImport action is invoked. Any combination of the following actions can be chosen (including all or none):
A screenshot of this option panel is shown below:NOTE: even if none of the above actions are chosen the imports will
still be redrawn according to the grouping and sorting rules defined in
the Ordering and Layout
option panels.
Recent Changes |
To Do |
Things I would like to do if I have the time:
Feedback |
Feel free to email me, Matt Etheridge <matt@etheridge.org> with any bugs or general feedback. Otherwise try these:
Notes |