Posts Tagged ‘Code Insight’

Smarter and Faster Introduce Variable in Java

Thursday, November 11th, 2010

Introduce variable is one of those refactorings used many times a day. So, how can we make it work faster?
Get rid of modal dialogs!

So, in order to introduce a variable from an expression press Ctrl+Alt+V.

Remember modal dialog? No more of it! Now you can edit your code right in the editor:

Oh, you prefer List as variable type? No problem, press Shift+Tab, choose List from drop down and IntelliJ IDEA will remember your choice for future.

You already pressed Ctrl+Alt+V but then realized that such variable already exists? Not a problem! Now it is possible to reassign a variable right from introduce template. Press Ctrl+Alt+V one more time and IntelliJ IDEA will collect all available variables of appropriate types and will suggest you to choose one:

You can try it yourself using a latest IntelliJ IDEA X EAP.

And let us know what you think!

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.