if99.net

IF99 ITB

Archive for the ‘formatter’ Category

Maven Plugin: Java Code Formatter

without comments

Enforcing code format in an organization needs discipline. And the best way to do that is by doing some automation. A plugin for Maven will be a easy decision if your organization has used Maven to manage the build.

This plugin is one plugin that exactly does that. The backbone on the plugin comes from Eclipse Java Formatter, which I still endorse as the best in the industry.

Compared to Jalopy, which is now not free, I find Eclipse formatter has the flexibility we often needed. Although it is not bug-free, for most cases it is more than enough.

However, I concerned by the fact that official maven doesn’t have the latest jar of Eclipse JDT. It still only has version 3.3 of it. Hopefully they will have it soon, especially after Helios is released, which bring a lot of improvement to the Java formatter.

Related posts:

  1. Java Formatter Improvement on Eclipse Helios
  2. My Eclipse Code Formatter
  3. Eclipse plugin: Introduce Static Imports

Maven Plugin: Java Code Formatter originally appeared on satukubik on June 10, 2010.

Written by Nanda Firdausi

June 10th, 2010 at 3:30 pm

Java Formatter Improvement on Eclipse Helios

without comments

A new milestone of Eclipse 3.6 just released with a long new and noteworthy list.

Instead of discussing what’s new from this release, I want to discuss a set of improvement from its Java Formatter. I still love Eclipse’s Java Formatter and with these new improvements, I even love it more. This improvements unfortunately didn’t make their way to list of new and noteworthy, hence the reason why I wrote this blog post.

First, we can now turn off the formatter for some part of your code that you don’t want to format automatically. This is achieved by defining special tags.

This is very useful to format a part of your code that will if formatted will be uglier. For example:

		// [format off]
		foo(           "|------------|\n"            +
		    "-----------|------------|-----------\n" +
	         "----------|   FORMAT   |----------\n"  +
	          "--------- |          | ---------\n"  +
	            "-------  |        |  -------\n"  +
	                      "--------");

No tools can keep this formatting no matter what are you trying, right? ;)

The next improvement is very beautiful. This is result of bug entry 59891 which is posted five years ago (! just to make sure that you understand its complexity).

Basically the problem is if you have nested method calls, Eclipse can’t group the arguments based on their call level. So if you have something like:

public class Main {
    public static void main(
            String[] args) {
        foo(bar(1, 2, 3), baz(4, 5, 6, 7));
    }
}

In case the editor set to wrap on 40th characters, you will get:

public class Main {
    public static void main(
            String[] args) {
        foo(bar(1, 2, 3), baz(4, 5, 6,
                7));
    }
}

With the improvement, you will get:

public class Main {
    public static void main(
            String[] args) {
        foo(bar(1, 2, 3),
                baz(4, 5, 6, 7));
    }
}

Very nice!

A longer example (based on post on StackOverflow.com).

With old formatter, the best you can get is:

		Options options = new Options().addOption(OPTION_HELP_SHORT,
				OPTION_HELP, false, "print usage information").addOption(
				OPTION_VERSION_SHORT, OPTION_VERSION, false,
				"print version and exit").addOption(
				OptionBuilder.withLongOpt(OPTION_PROPERTIES)
						.hasArg()
						.withArgName("FILE")
						.withType(File.class)
						.withDescription("specify a user properties file")
						.create());

Or anything explained on the original post. With the new improvement, you can set the formatter to get something like this:

		Options options = new Options()
				.addOption(OPTION_HELP_SHORT, OPTION_HELP, false, "print usage information")
				.addOption(OPTION_VERSION_SHORT, OPTION_VERSION, false, "print version")
				.addOption(	OptionBuilder
									.withLongOpt(OPTION_PROPERTIES)
									.hasArg()
									.withArgName("FILE")
									.withType(File.class)
									.withDescription("specify a user properties file")
									.create());

Beautiful, isn’t it? Just one bug maybe since there is a white space before OptionBuilder.

Beside those two, Helios also bring further supports for formatting annotations and various bug fixes. You can check the whole list by clicking this link.

Related posts:

  1. My Eclipse Code Formatter
  2. Performance benchmark for logging
  3. Be extremely careful when you initialize a non-final field in the declaration!

Java Formatter Improvement on Eclipse Helios originally appeared on satukubik on May 3, 2010.

Written by Nanda Firdausi

May 3rd, 2010 at 10:05 pm