Pages

Monday, November 19, 2012

Contributing Java Editor templates via a plugin

JDT provides several editor templates. You can also add your own via the Templates View. However, if you want to share the templates between your several workspaces or with others you can also create a plugin.

As a first step, define the following extension for the extension point org.eclipse.ui.editors.templates.
   <extension
         point="org.eclipse.ui.editors.templates">
      <include
      file="templates/default-templates.xml"
      translations="$nl$/templates/default-templates.properties">
      </include>
   </extension>
The xml file just contains the template descriptors. For example, here is a template for printing the enclosing type and the enclosing method.
<template name="debugout" description="%Templates.debugout" id="com.eclipse.jdt.ui.templates.debugout" context="java-statements" enabled="true" autoinsert="true">
System.out.println("${enclosing_type}#${enclosing_method}(..)");${cursor}
</template>
You can read about the available template variables in Eclipse help.

I have also shared a plugin with the above code on GitHub.

Tuesday, November 6, 2012

Debugging with sysouts made awesome

At times I like to debug by writing to console. However, in a long debugging session I often end up with several sysout statements in several files/methods, which makes it hard to track where a particular line of console output came from.

In the past I tried to use code templates to also print the 'enclosing type' and 'enclosing method', but I often forgot to use the template. In any case, via templates you cannot 'link' back to the source code.

Jeeeyul presents a neat solution - just replace PrintStream by a DebugStream so that you can also print 'file : line number : method name' information. I took this code, added it to a plugin and then added this plugin to my launch config. Now everytime I launch an Eclipse Application 'DebugStream' gets activated. 

Essentially, the plugin converts console messages from
Hello World.
to include a link to the source code
(HelloWorld.java:10) main(..) : Hello World.