Author Archive

ThreadLocal in One Click

Friday, October 30th, 2009

Most of applications initially are single threaded, and IntelliJ IDEA was no different; though luckily, now it isn’t — but we had to adapt our code to use multiple threads. In this post I’m going to tell you how.

In our example we see SAXBuilder, which is too expensive to be created every time we need it, so it is stored in a static final field.

Because SAXBuilder is not thread safe, multiple calls to loadDocument from different threads cause a lot of interesting exceptions. This is why we need either to make access to this field synchronized, or to make the field ThreadLocal. In our case, we’re choosing the latter. We encapsulate the field, change its type and initializer, then fix a generated getter. Quite a lot of work, right? Luckily in Maia you can do it all in just one click. Just place caret on a field and press Alt+Enter to smoothly migrate it to ThreadLocal.

Create a Class Really Quickly

Tuesday, March 31st, 2009

To quickly create a new class I press Alt+Home and then Alt+Ins. And you?

Quick Create with Alt-Home Alt-Ins

No More Missing TestNG Tests

Wednesday, March 25th, 2009

I often forget to add newly created tests to the testng.xml, which is used in our TeamCity build, and by the time I realize that they don’t run something is surely went wrong. What a bummer! Not any more, fortunately — IntelliJ IDEA makes sure no tests are missing. All it takes is to enable the Undeclared test inspection under TestNG in the Errors section of the Settings dialog.
Undeclared Test Warning

Quick Code Cleanup

Thursday, March 19th, 2009

With IntelliJ IDEA you can quickly clean up your legacy code. If a problem occurs multiple times in a file, you do not need to manually apply a quick-fix every time — use the Fix all intention to repair them all at once.