Projects

Clean Qualified Types Plugin (CQTP)

The JDT Plugin CQTP replaces fully qualified type names with simple type names and adds the missing import statements. For example „new java.util.ArrayList()“ will be changed to „... import java.util.ArrayList; ... new ArrayList()„. The plugin manipulates the abstract syntrax tree which is produced by the JDT integrated parser. There is no undo functionality except the modified file is currently opened within the Eclipse Editor (to undo use control-Z).

In the example section you can see two screenshots. The first screenshot is the input, the second one is the output. The changes are marked in the screenshot.

Features
  • The plugin adds a new menu button to the Source context menu of the JDT. The button is displayed in this screenshot. The entry can be used on Java projects, source folders, packages and for file selections. The first three options manipulates all classes of the selected directory and recursivly all their sub directories (packages). You can see the integration in the following screencast (Flash)
  • It replaces fully qualified type names with simple type names with respect to import conflicts. They are handled by the plugin. See line 18 and 20 in the before screenshot. The usage of java.util.List and a own class named List is handled.
  • Fully qualified types from the package java.lang are resolved but not imported (see line 18 in the before screenshot). The same will be done for types from the same package. Imports from the root package are ignored.
  • Since version 0.0.2 there exists a Xpand integration as an additional but optional plugin. For more information see the Xpand Integration section.
Download / Installation / Sources

Eclipse update site: http://cqtp.sourceforge.net/eclipse/

Installation

Add the update site to your Eclipse installation and install the plugins you wish.

License

The plugins are published under the Eclipse Public License – v 1.0.

Sources

SVN: Hosted on Sourceforge

Example
Before the action is executed

Before

After the execution was executed

After

Note: I removed the line break in line 18 manually.

Eclipse Integration
JDT Integration

JDT Integration

Screencast of the JDT integration.

Xpand Integration

To use the integration you must install the Xpand integration feature. You must also add the following dependencies to your MANIFEST.MF file of your generator project:

  • org.eclipse.core.resources
  • de.dainel.cleanqualifiedtypes.xpand

The integration class CleanFullyQualifiedTypes implements a postprocessor for a generator configuration. See the usage in the following example:

<component class="org.eclipse.xpand2.Generator">
	<metaModel idRef="mm_emf"/>
	<expand
		value="template::Template::main FOR model" />
	<outlet path="${src-gen}" >
		<postprocessor
 class="de.dainel.cleanqualifiedtypes.xpand.CleanFullyQualifiedTypes" />
		<postprocessor
			class="org.eclipse.xpand2.output.JavaBeautifier" />
	</outlet>
</component>


Since release 0.0.3, a group based sorting for import statements is also possible. The postprocessor provides the property organizeImports. The value can be either „true“ or „false“. „false“ is the default value. If the value is „true“, you will be able to configure a list of import groups by using the tag importGroup. If you have a protected region within your imports, the feature must be disabled. See the following example postprocessor configuration with enabled sorting and some import groups:

<postprocessor
 class="de.dainel.cleanqualifiedtypes.xpand.CleanFullyQualifiedTypes"
 organizeImports="true">
	<importGroup value="java" />
	<importGroup value="javax" />
	<importGroup value="org" />
	<importGroup value="com" />
</postprocessor>


Import groups are separated by a line break by default. The amount of line breaks is configurable by setting the property amountOfGroupLineBreaks for import groups. The amount of line breaks after the last import statement can be configured by setting the property amountOfLineBreaksAfterLastImport The default value of both properties is 1. Normally you don’t have to change this, because the java beautifier is responsible for this. But the beautifer does not work, if there is not at least one separator line break. If you do not wish to create line breaks you can override this behavior by setting the property addLineBreaks to false. See the following generator result for import groups:

Grouped Imports

Grouped Imports

Environment

I tested the plugin with Xpand 1.0, Eclipse 3.6 and Java 1.5 Source Code.

Screencast

Screnncast of the Xpand integration: Xpand Integration In Action (10 MB) (Note: in the screencast the integration class will be used from the package mwe but the package was renamed to xpand before the first release was created.)

Why not using the new ONFILECLOSE keyword instead?

With high intrest I read the documentation of this new feature and than I decided it is code smell for handling Java imports. This is because it should be possible for each DEFINE statement to add imports to the list, so the import list variable must be given to each DEFINE declaration as a parameter. Otherwise you have no chance to enrich a generator for example with a generator aspect, which requires some new types which must be added to the import list. In a fine-grained generator with lots of DEFINE statements, this does not make it easier to read and develop generator code if you always have to pass this variable to each DEFINE statement. Beyond that it is not possible to handle import conflicts as seen in the example section. This should be killer arguments for not using it for Java imports and instead using a postprocessor and generating against fully qualified types. You also won’t be responsible for code formatting and using the configurable build in formatter of Xpand, the same arguments for using a formatter are also valid for handling imports.

Leave a Reply

Your email is never published nor shared.

You may use these HTML tags and attributes:<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


*