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!