April 24th, 2008 at 3:56 am

Useful Eclipse Java Editor Templates: Tracing

In this blog I’ll collect some nice Eclipse Java code templates which I use.

I did not find any site collecting those, so there is no place I know of, where one can commit his favourite templates. So here we go:

Adding a trace boolean in order to switch on or off tracing of a plugin:

This inserts a static boolean which can be used to turn tracing on or off using the Tracing tab in the launch configuration dialog. In order to use this, you also have to create a file called .options in your project root. In this file add the line <project name>/<type name>/debug=false.

And here comes the template. Use it at class level:

	private static boolean trace = false;

	static {
		String value = Platform
				.getDebugOption("${enclosing_project}/${enclosing_type}/debug");
		if (value != null && value.equalsIgnoreCase("true")) {
			trace = true;
		}
	}

Quickly insert a method trace:

If the trace infrastructure is set up, a method call can be logged very quickly using

if(trace) {
	System.out.println("${enclosing_type}.${enclosing_method}()");
}

at the beginning of the method.

Generic trace output:

In the middle of a method, use this:

if(trace) {
    System.out.println("  ${cursor}");
}

In order to import these templates, go to the Eclipse preferences dialog, navigate to Java > Editor > Templates and import this XML.

Leave a Comment

You must be logged in to post a comment.