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.

Saturday, July 28, 2012

Going back to school

After working at IBM for 5 years, I am going back to school. August 10th is my last day as a 'full-time' JDT committer, after which I head to University of British Columbia for Masters in Computer Science. I am looking forward to spending two fun-filled years in Vancouver. :-)

It has been fun contributing to Eclipse so far, and in my free time I will likely continue to contribute. So, I will not actually say 'Good bye'.

Sunday, July 1, 2012

Eclipse Tip: Stepping into selection and hyperlink debugging

The Java debugger allows you to step into a single method within a series of chained or nested method calls. Simply select the method you wish to step into and select Step into Selection from the Java editor context menu. I prefer to use the shortcut Ctrl+F5.


You can also step into a method by using hyperlink navigation. Simply place the cursor over the method you wish to step into and use Ctrl+Alt+Click to step into the method (rather than Ctrl+Click which will navigate to the source code).

Saturday, June 30, 2012

Improving Java code quality with Eclipse Juno

The Eclipse Java compiler performs more checks and analyses than are mandated by the Java Language Specification. See Java > Compiler > Errors/Warnings preference page for available options.

Now several of these options should be obvious, however a few maybe not :-) In Juno we (I mean Stephan) added a few pages to give some background on certain analyses and hints on how to make the best use of them.




Please let us know if these pages could be improved, or if you want to know more details on some other compiler options as well.

Thursday, June 28, 2012

Eclipse Demo Camp 2012 - Bangalore

The Demo Camp happened last Friday, and was great fun! A couple of things stood out for me.

First, I was pleasantly surprised to see people from Bosch already exploring Orion and evaluating how they can use the Orion platform and the Orion editors at their work.

Second, I came to know about Eclipse Spykit which looks an interesting project. Based on Runtime Spy Eclipse Spykit creates more charts and does a bit more analysis on what happens during Eclipse startup. Annamali started this project and mentioned that he is looking for contributors.

Oh yeah, the Fajitas were good and the wine was excellent!

Tuesday, June 19, 2012

Eclipse Demo Camp 2012 - Bangalore

It's demo camp time again! I will be speaking this Friday at the Eclipse Demo Camp Bangalore where I will demo some of the interesting new features in JDT for Juno and also show off a few tips and tricks. 


It should be fun discussing Eclipse over some Tapas and wine :-) Thanks Annamalai for organizing this!

Thursday, June 14, 2012

JDT Tip: Toggle between inserting and replacing content assist


When content assist is invoked on an existing identifier, it can either replace the identifier with the chosen completion or do an insert.

The default behavior is to insert. You can toggle this behavior while inside the content assist selection dialog by pressing and holding the Ctrl key while selecting the completion. The highlighted text is overwritten.

If you wish the completion to overwrite without pressing the Ctrl key then you can select 'Preferences > Java > Editor > Content Assist > Completion overwrites'.

Thursday, May 3, 2012

Subwords completion

Not so long ago I was fairly happy with JDT's content assist, and then I started using Subwords completion from Code Recommenders project and now it is fair to say that I cannot write code without it! :-)

The idea is simple enough - you should not have to type a name from the beginning to find a match in the content assist popup. It really helps when I do not know if I have to 'find' an element or 'get' an element.

The feature is sophisticated enough to understand a rough shorthand e.g. dclr for declaration.


It is also sophisticated enough to understand that a method or type name in Java is composed of several words e.g. 'ty + dclr' finds all 'type' + 'declarations'.

Wednesday, May 2, 2012

JDT 3.8/4.2 M7 - New and Noteworthy

As usual we devoted most of our time in M7 towards polish and performance, nevertheless we managed a few new and noteworthy items.

Firstly a word about bracket matching. So far, a matching bracket was found when the caret was placed immediately 'after' a bracket. This caused confusion because a user can also think in terms of placing the caret 'inside' or 'outside' a bracket pair, see bug 9503. We tweaked this during M6 so that the start bracket was highlighted when the caret is before (not after) the end bracket, i.e. bracket matching now worked when the caret was 'inside' a bracket pair. We realized that this was still not ideal, and also that the old behavior was broken which some of users were already accustomed to.

All this has been improved and now a matching bracket is found both when the caret is placed either immediately before or immediately after a bracket i.e. now bracket matching just works in all cases a user might expect it to work.
You can hover over a closing curly brace and see the source near the matching opening brace in a hover. This is helpful when you want to see the code near the start of a long code block. The hover also tells the number of lines in the code block.


The hover is also useful to temporarily 'highlight' a code block.

In M6 we had tweaked the warning on enum based switch statements, unfortunately the tweaks were not ideal and resulted in a bit of activity on bugzilla. This has been improved and new compiler options have been added to take care of all cases.

Those who are using null annotations will also discover that there are a few new quick fixes to help you a little bit.  One set of quick fixes help to quickly setup a project to use null annotations, i.e. appropriately add the annotations jar to the build path. The second set of quick fixes help you fix problems with respect to incorrect usage of null annotations, missing annotations etc. This second set is not yet perfect, see bug 337977

As always, feedback is greatly appreciated especially in the form of bug reports!

In other news, during this milestone I also earned commit rights on Platform/Text project :-)

Friday, April 20, 2012

Recovering from a hard disk failure


A few weeks back the hard disk of my laptop computer crashed, and as a hard disk had never crashed in my vicinity earlier I was a bit unsure about what to do. I could have used one of those data recovery services, however I decided to tackle the problem on my own because - no one cares for my data more than me and you cannot really trust a stranger with your data. I ended up spending a fair bit of time trying to recover my data. Here is a summary of what I did

1. Essentially my computer had stopped booting up. On powering it on, the Loading Windows screen would come and then a blue-screen-of-death. Now this could be because some OS files are corrupted, or there is a problem with master-boot-record, mother board issue or a gazillion other things. Hence, the first step is to verify that it is an hard disk issue. To do so get a bootable CD/DVD or a USB and boot using that, if the system boots then something is probably wrong with the hard disk. If you do not have a bootable CD/DVD/USB ready at hand, ask around, a bootable Linux disc is not too hard to find.

2. I could boot my system using a bootable USB drive. Once booted I tried to access the hard disk in question, however that did not work. The hard disk would not mount. I even tried using a bootable BartPE disk, and even that did not help. So something was seriously wrong with my hard disk.

3. Now I was certain that there is a serious failure and I would need some special tools to recover my data. At this point you have 2 choices in front of you
a) Boot your system using a bootable disk and use a specialized software installed on that bootable disk to recover your data
b) Take out your hard disk and connect it to another computer as a secondary drive.

Let me explain both choices and reasons behind why I went with option (b)
a) For this option, you can use BartPE to boot your computer. BartPE has a plugin for GetDataBack, however I found this option a bit cumbersome to use. There were a few steps to setup everything nicely so as to copy data on to an external drive. Hence, I thought of exploring option (b)
b) In this option, the major hurdle is to be able to connect your hard disk to another computer because you need some hardware which not everyone may not have lying around. Luckily for me a friend had replaced his CD/DVD drive with a secondary hard disk and he was kind enough to lend me the hard disk bay for a few days :-) I also had another laptop readily available, something that may not be true for everyone. Once I was able to connect my faulty hard drive to another computer as a secondary drive, I could use any number of data recovery tools like GetDataBack, Partition Table Doctor, or freeware such as PC Inspector File recovery. Some more tools can be found at alternativeto.net.

4. Now reading data from my faulty hard drive was a simple task, it was slow but simple enough. However, some sectors on the disk were clearly bad and there were quite a few I/O errors while recovering the data. Hence, I was a bit unsure about the quality of data I was getting back - maybe some files were forever lost, or maybe some files were corrupted - essentially I did not know if I could trust the data. Now I also had a few months old data backup on an external drive, hence I thought to start with that and compare with the recovered data and sort of merge the two :-) Again there are a few tools which can help with this, Microsoft SyncToy is fairly neat and is also free, Beyond Comapre is a bit better but is not free.

Now I could have avoided all this if I frequently did a data backup. In any case it was kind of interesting to do all this and I did learn a few things along the way. The most important lesson obviously being - take a data backup every week! :-)

Note: I should also mention that somewhere in between I also tried the freezing-the-hard-disk trick. I know it sounds a bit crazy, and it did not work for me, but for some folks it seems to help a bit.

Sunday, April 15, 2012

YourKit profiler APIs

YourKit provides APIs to start and stop measuring from within your application, e.g.
com.yourkit.api.Controller controller = new com.yourkit.api.Controller();
controller.startCPUProfiling (com.yourkit.api.ProfilingModes.CPU_TRACING , null);
controller.stopCPUProfiling();
controller.captureSnapshot (com.yourkit.api.ProfilingModes.SNAPSHOT_WITHOUT_HEAP);
To use these APIs include <Profiler Installation Directory>/lib/yjp-controller-api-redist.jar in the classpath. Read more about YourKit APIs here.

Wednesday, April 11, 2012

Permanently enable 'Open command window here' in Windows 7

By default you need to right-click a folder, while pressing the SHIFT key, in order to access 'Open command window here' context menu option. However, it can be enabled permanently with the help of following steps
  • Click Start, Run and type Regedit.exe
  • Navigate to the following location:
    • HKEY_CLASSES_ROOT\Directory\shell\cmd
  • In the right-pane, delete the REG_SZ value named Extended

Wednesday, March 21, 2012

Contributing a quick fix and a quick assist for Java code

JDT offers a large number of quick fixes and quick assists for Java code. However, you may want to implement your own as well, and it is actually quite easy to do so with the help of org.eclipse.jdt.ui.quickFixProcessors and org.eclipse.jdt.ui.quickAssistProcessors extension points.

Using the extension points
To create a new extension for the extension points you need to first provide the required extensions in the plugin.xml. For example, JDT defines the following processors

 <extension
       point="org.eclipse.jdt.ui.quickFixProcessors">
    <quickFixProcessor
          name="%defaultQuickFixProcessor"
          class="org.eclipse.jdt.internal.ui.text.correction.QuickFixProcessor"
          id="org.eclipse.jdt.ui.text.correction.QuickFixProcessor">
    </quickFixProcessor>
 </extension>
   
 <extension
       point="org.eclipse.jdt.ui.quickAssistProcessors">
    <quickAssistProcessor
          name="%defaultQuickAssistProcessor"
          class="org.eclipse.jdt.internal.ui.text.correction.QuickAssistProcessor"
          id="org.eclipse.jdt.ui.text.correction.QuickAssistProcessor">
    </quickAssistProcessor>
 </extension>

For a description of the individual attributes, please refer to the extension point documentation.

Contributing a quick fix and a quick assist
To contribute a quick fix, you need to create the class that implements the org.eclipse.jdt.ui.text.java.IQuickFixProcessor interface. This is the same class that you specified in the extension declaration. Each Java problem has a unique id which is defined in org.eclipse.jdt.core.compiler.IProblem interface. For a particular Java problem you may offer one or more correction proposals.

To contribute a quick assist, you need to create the class that implements the org.eclipse.jdt.ui.text.java.IQuickAssistProcessor interface. Again, this is the same class that you specified in the extension declaration.

Supplying the right IJavaCompletionProposal
JDT provides the following default implementations for correction proposals that can be used to contribute quick fixes and quick assists.
  • org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal
  • org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal
  • org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal
Typically you will use an org.eclipse.jdt.core.dom.rewrite.ASTRewrite, in that case you should create an ASTRewriteCorrectionProposal. However, if as a result of a quick assist you want to start an action e.g. open a wizard, you should create a ChangeCorrectionProposal and override its apply(IDocument) method.

Note: These default implementations became API only in 3.8/4.2 (Juno) M6.

Manipulating Java code via ASTRewrite
Using ASTRewrite is simple enough, you can read more about AST and ASTRewrite in this Eclipse Corner Article and in this EclipseCon tutorial (slides 44-46). However, sometimes you may not know which AST nodes need to be modified and to what. ASTView plugin helps you visualize the AST of a Java source file, and is really helpful in identifying what modifications need to be done in the AST.

Friday, March 16, 2012

JDT 3.8/4.2 M6 - New and Noteworthy

M6 was a long milestone - there was one extra week of development - and hence we could do a little bit more.  There are quite a few new features and a few API additions as well.

JDT always provided extension points to contribute Quick Fixes/Assists. However, it was hard for contributors to supply the right IJavaCompletionProposal required by IQuickFixProcessor and IQuickAssistProcessor interfaces. This has been improved as JDT now provides the following default implementations for correction proposals

  • org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal
  • org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal
  • org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal

This makes it easier to implement quick fixes/assists that operate on .java files and use an ASTRewrite. Of course you could always use the internal implementations, but then your code would be littered with forbidden access warnings and the implementations could change any time :)

The bracket matching support in Java Editor has been improved, and among other things it now supports highlighting of enclosing brackets.
This can be configured on the Java > Editor preference page.


While the feature is visible in Java editor, the infrastructure is in Platform/Text. Hence, if anyone wishes to add the same functionality to other editors they can make use of the following types
  • org.eclipse.jface.text.source.DefaultCharacterPairMatcher
  • org.eclipse.jface.text.source.MatchingCharacterPainter

Marcel Bruch, of Code Recommenders fame, added the concept of sorting to the content assist framework by adding several new APIs on the content assistant and the processor - see org.eclipse.jface.text.contentassist.ICompletionProposalSorter. Thanks Marcel, and hope to see more contributions from you in future! :-)

There are a few more interesting items, like Selectively ignore errors/warnings from source folders and
Null analysis treats org.eclipse.core.runtime.Assert like Java assert. You can read the complete list here.

As always, feedback is greatly appreciated especially in the form of bug reports!

Thursday, January 26, 2012

Weird code pattern in Eclipse SDK

In recent Juno milestones JDT has added several new static analysis capabilities - resource leak detection (Juno M3), annotation based null analysis (Juno M4), null analysis for fields (Juno M5). This week I have been testing these new features on the entire Eclipse SDK source code. 

While there are quite a few 'gems', the following code pattern occurs quite frequently. If the object can be null at the first if condition, how is it magically guaranteed to be non-null at the second if statement?  :-)


As Stephan mentioned in his blog today - Help the JDT Compiler helping you. Please start using the new static analysis options and report any issues you find. In the process you will certainly make improvements to your code as well.