Adding JSP 2.0 support to Intellij using AspectJ


Adding JSP 2.0 support to Intellij using AspectJ

I started working on a project that takes advantage of the new features in JSP 2.0. IntelliJ has pretty good JSP support but they won’t have full JSP 2.0 support for a while.

I haven’t gone and added all the JSP 2.0 features, but I did work on the one I needed most, .tag file support. One of the annoying things you find when using IntelliJ’s builtin JSP editing mode for these .tag files is that you don’t get auto imports into the tag declaration, you don’t get implicit variables from attribute and variable directives, and you don’t get attribute completion when writing new directives. Unfortunately, the plugin that does JSP support is built into IntelliJ and is not released separately in either source or binary form. To make it even a little more difficult, all the IntelliJ class files have been obfuscated.

Fortunately, IntelliJ has a lot of great tools for navigating its own compiled code. The key to finding the right pointcuts are the strings. They are like signposts in the code. For instance, searching for “page” in the code finds the places where the page directive is handled by the plugin. These are the interesting places for adding support for more directives. In the plugin, there are a couple of interesting places that this matters. First the place where the directives and their attributes are added to the system. Here we add the “tag”, “attribute”, and “variable” directives with all their options to the big list for completion. The next two places are where the directives are collected and implicit variables are created. This was pretty straight-forward since the methods are in an interface and thus are not obfuscated. The final adjustment that I make and the biggest hack is the part where imports are written. This was a tough one, mostly because AspectJ is not really a meta-programming API, but an AOP system. To implement this I had to add a bug or two to the code, but they shouldn’t show up in practice and I think it is limited to thinking that you can use “tag” instead of “page” in a jsp:include.

My next project will be to add support for preludes and codas, but they appear to be much more difficult because of the limitations in the WebModule API. I’ve made a stab at it a couple of different ways, but I think it might involve a ton of introductions and more widespread changes to the application.

Moral of the story — AspectJ is too powerful to be a pure AOP solution, IntelliJ needs to ship JSP 2.0 support ASAP, and I have too much time on my hands while home with my new baby, Anna.

Here is the AspectJ code:JSP20.aj

(if you can’t figure out how to apply it to IntelliJ, perhaps that is best.)