Wednesday, July 27, 2011

Using JRebel with MyEclipse Blue and WebSphere

Just recently ZeroTurnaround and Genuitec announce JRebel for MyEclipse. Here's what it looks like in combination with WebSphere application server. It is quite easy to install and configure.

JRebel 4.0.x

JRebel 4.0.3 was released just recently. The debugger support is maturing - some features are still missing, like expression evaluation support for the reloaded classes, but stepping functionality and breakpoints installation are quite well polished now.

See the press release.

Monday, July 25, 2011

JVM Language Summit 2011

Back from Santa Clara, CA where I've attended an awesome event - JVM Language Summit. Before the event I thought I know some Java, but it seems I was too self-confident :) The 3 days of technical sessions revealed to me a lot of new things in Java world, including the new features coming in the next versions of Java as well as some JVM-based languages that I didn't pay attention to previously.

Day 1

The event started with a short intro by Brian Goetz followed by a keynote speech by Cameron Purdy. Cameron presented an awesome wishlist for the upcoming versions of Java/JVM. Some of the features I liked the most on that list:

Immutability. The main concern was pretty much about how could Java get so far without immutability natively supported by the language?

Obvious Intrinsic Types are just missing from Java. And this is the major pain for the developers dealing with financial calculations whereas the rule of thumb is - never use double for operations on monetary values. Indeed, every Java programmer should see this presentation - How Java's Floating-Point Hurts Everyone Everywhere. The good news is, IEEE 754-2008 a.k.a. IEEE 754r – defines decimal types, the bad news, it is not yet implemented.

Alternative class format is something that tool implementers would definitely love - why inner classes aren't inner classes? why anonymous classes are not properties of a method where they are defined?. In fact, I was carrying the idea around with myself on why not to try to implement the class nesting into the VM? I was actually thinking it is a crazy idea of mine but it turns out other people do have same crazy ideas! :)

Tail Recursion / Tail Call Optimization is something that every developer, who tried some FP language, misses from Java. I've tried Erlang a while ago, and after a few weeks I really could live without this feature.

Cameron had plenty of other stuff on the list but I'd rather skip mentioning all the points. The presentation slides are available here.

Async .NET

It was quite an interesting presentation by Mads Torgersen. It is nice to know, what are the people working on the other major managed runtime come up with in a while. The main feature that Mads was talking about was the asynchronous task execution coming in C# 5. Basically it is similar to what Java has with Futures. The presentation slides are available here, and actually I've found a video on the same subject at Channel 9 - recommended!.

Indy FTW!

Now it was all about invokedynamic, method handles, call sites, etc. First, John Rose talked about Method Handles. And after that, Dan Heidinga talked about the method handles implementation in IBM J9.

These followed by the language implementers - demonstrating how invokedynamic and method handles can be used: Charles Nutter on JRuby, and Remi Forax on JSR-292 Cookbook. JRuby folks take a great advantage of the invokedynamic which allows a lot of optimizations hence improving the performance. Remi Forax presented a number of Method Handle usage tips. The collection can be found at One really cool thing I've got to know is the ClassValue which can serve as a metadata carrier that might be required for dynamic method invocation.

Day 2

The first half of day 2 was mostly related to all the same JSR-292 features: porting Smalltalk to JVM, new implementation of JavaScript on JVM, Jython&Indy.

Dynalink, presented by Attila Szegedi, is an interesting project for dynamic linking in Java - as far as I understand Dynalink could be used to implement invokedynamic behavior on JVM.

After all the dynamic fluff-and-stuff the statically typed languages came on stage.

Gosu. Honestly, I underestimated the language when I learned about it the first time. This time I was actually surprised by the features presented in Gosu, accompanied with compiler support and the open type system. A couple of years ago I used Drools and Groovy to implement the rules-based system. If I knew about Gosu at that time it would probably be my language of choice. Especially neat is that one can basically connect some XML or properties file to the type system and tell that this is a new type in Gosu, and get all the type-checking backed by the compiler. Just awesome!
Gosu doesn't seem to tackle any of the general-purpose language but it definitely has its niche - a statically-typed language for writing business rules.

Project Kotlin, presented by JetBrains developers, Andrey Breslav and Dmitry Jemerov, has definitely created a lot of buzz by now. According to the creators, Kotlin is designed to be nicier than Java, and simpler than Scala. Here're the slides from the presentation & the workshop. At first sight, the language is definitely nice to read and the syntax seems to be more friendly than in Scala, but going deeper into more advanced features, the claim tends to diminish. I noticed a lot of similarities with Scala, Groovy and C#, whereas C# features (i.e. non-virtual methods) do not seem as coherent with the other two languages. I do not have any strong opinion on the project yet - need to study it a bit and compare to other languages (read Scala) but one thing I quite sure about is that the IDE support should be top class. There are a lot of opinions currently, whether JetBrains should rather have contributed to Scala tooling, or Ceylon project.. don't know what is best - time will show. I do understand the scepsis of Scala fanboys regarding Kotlin, but it is quite stupid to underestimate this effort. IMHO, nothing replaces Java any time soon - it rather benefits of all the language experiments.

Day 3

On the day 3 there have been a few talks related to Java language itself.

Interface injection by Tobias Ivarsson, Extension Methods & Lambda Bytecode by Brian Goetz - awesome content and ideas presented. IMO, properly combined autoboxing, interface injection and extesion methods with lambdas could enable ruby-like syntax in Java, e.g. 5.times({ int x -> println(x); }. Neat, huh?

A very interesting project Graal, presented by Thomas Wuerthinger (one of the core developers on DCEVM. Graal includes a mechanism to execute the compiler outside of the HotSpot JVM which leads to "distributed compilation".

At the end Prashant Deva talked about the Chronon debugger. In fact, it turns out that Chronon isn't really a debugger but you can rather record the dataflow of your program execution and do "post-debugging" using the data recorded at runtime. It really looks as if you're debugging in the IDE, stepping through the lines of code and observing the values of variables. But technically, the plugin doesn't even connect via JVMTI - it rather emulates the program under inspection - executing queries to the recorded data set and jumping to the correct line of code in the IDE.


I think this was the most advanced and interesting Java-related event I've attended so far. I really feel myself being so dumb now - very motivating to finally take a look at some textbook on the compiler construction :)

Sunday, July 10, 2011

What’s Cool In IntelliJIDEA. Part II: Live Templates

In IntelliJ, I use Live Templates quite heavily and still think that these are underused. It is not only the standard number of templates that can make your life easier while typing yet another piece of code, but it is also the ability to define new templates with all the bells and whistles that are available in IntelliJ standard template list.

In the previous post I've covered the basics of Live Templates but I think that it would be cool to cover these in more details and to point out some less-known aspects of this functionality.

First of all, Ctrl+J is the shortcut to use in order to get a list of the available by default. But if you had time to practice, most likely you can remember the abbreviations by heart, like psvm, iter, psfs, soutv, etc.

One thing I do for sure before using Live Templates is that I change the default expansion key, which is Tab by default, to a Space key. It seems to me that it is more natural to use Space for this purpose as when you type you actually hit the Space key almost automatically.

Another cool feature of Live Templates is to surround a capable statement with a block of code - also covered in the previous post.

How do I define my own template?

IntelliJ provides a number of templates in its default distribution but obviously it doesn't cover all cases you might need. So here's the main purpose of this post - how to define a custom live template in IntelliJ. First, let's make up a use case - a situation where we'd like to have some means for typing faster but which isn't defined in the IDE by default.

For some reason, IntelliJ doesn't provide try-catch template by default. It only suggests this option if you'd like to surround an existing statement with the try-catch block, but not when you just want to create an empty one. Here's what we can do: open Settings window (Ctrl+Alt+S) and start typing 'Live Templates' - this will lead you to the templates' settings - and hit the "Add.." button on the right.

It is quite easy to create a simple template which just has to generate a defined text for the given keyword. For the empty try-catch block we just need to fill in the abbreviation and the code itself, and not to forget to bind it to Java context using the corresponding checkbox.

This is quite a dumb template, nothing intelligent here. But what if we'd like to suggest the exception type in the catch block? What if we'd like to position the cursor in some particular place after the template is applied? Let's make our brand new template a little smarter.

To ask IntelliJ for a type of the throwable in the catch block we can add a variable ($EXCEPTION$) and define its value using a special function provided by IntelliJ. Say, I'd like the type to be a subtype of java.lang.Exception class, hence I'm using the subtype(<type>) function:

You've probably noticed that I've used another variable, $END$, but there's no value defined in the dialog window. This is a predefined variable, meaning where should the cursor be positioned after the last choice made within the templates.

So now once I type 'try' and hit the space key, the template is expanded to the following:

First the cursor will be positioned inside the catch block in order to choose the type of the exception to be handled. Right after I make my mind about the type and hit the Enter key, the cursor will be positioned back into the try block.

Disqus for Code Impossible