• Home
  • Events
  • About
  • IntelliJ IDEA Static Analysis: Custom Rules with Structural Search & Replace

    March 29th, 2011

    Well, well, I made another screencast. This time I’m taking on IntelliJ IDEA code inspections, and writing your own static code analysis rule (and quick fix!) using Structural Search & Replace. Not bad for under 5 minutes.

    If you have any trouble viewing the video then perhaps you should watch it directly on the JetBrains site. And if you’re feeling generous, then clicky clicky to upvote at DZone.

    Check out these pages for more information on IDEA inspections: 

    Read these pages to learn more about Structural Search and Replace: 

    I’ve made a lot of screencasts and blog posts over the years. If you like this, then there are many ways to see the other stuff I’ve done: 

    Phew, that’s a lot of self-promotion 🙂

    Thanks for watching, and leave a comment! 

    CDI Event Bus Basics Screencast

    March 2nd, 2011

    Another day another screencast. This one’s about the Event Bus that is part of CDI. Contexts and Dependency Injection is part of Java EE but available in SE using the Weld framework. This screencast shows you how to use CDI events and the event system, which is just one part of the larger CDI feature set. The screencast briefly explains the benefits of an event system and then shows how easy it is to use it from CDI.

    If it doesn’t play correctly, you may want to launch it from the JetBrains.tv site.

    You could do worse with 5 minutes of your time.

    If you like this, you can check out my other screencasts on YouTube (http://www.youtube.com/user/HamletDRC) or JetBrains.tv (http://tv.jetbrains.net/tags/hamlet)

    IDEA series (ix) selective actions

    January 12th, 2011
    Working with selections

    Please note that this is for Groovy and Grails development on Mac OS X with Swiss-german keyboard layout and default keymap, so the keybindings may differ on your machine. (For Windows, you most often can replace Cmd with Ctrl)

    We have seen the editing support for lines, combined with selections. This part is more specifically about working with selections.
    know your keyboard navigation
    first of all, it is very helpful to familiarize yourself with your OS-specific keyboard commands for moving the cursor around and with the Shift key pressed, doing a selection. On Mac that is Cmd left/right for begin/end of line and Alt left/right for jumping over words. Of course you can use the mouse for selections but once you master the keystrokes, you will be faster and more precise.
    Cmd W (Shift for reverse)
    is widening the current selection. That is, if you already have a selection, it expands it to the next enclosing syntactical scope (expression, statement, block). Without a selection, it starts a selection at the caret.
    Mac users may expect Cmd W to close the current editor (internal window) as typical for mac applications, but that functionality is mapped to Cmd F4 in the standard keymap (somehow resembles the Windows keystroke).
    I use widening most of the time to select a small expression precisely – often for later use in a refactoring. I start in the middle of the code and hit Cmd W repeatedly.
    When selecting a larger block, let’s say a method or a whole class body, it’s efficient to place the cursor just after the opening brace and hit Cmd W just once to select the whole block.
    Cmd Alt T
    surrounds the selection (or current line if there is no selection) with a live template. This features spares you the need for fiddling around with cursor positioning to put some code into an “if” block and the likes. I even made my own templates for surrounding a selection with parentheses, brackets (to make a literal Groovy list), braces (to make a closure or block), single quotes, and double quotes.
    Especially putting quotes around a selection is nice since otherwise IDEA sometimes inserts pairs of quotes when it shouldn’t.
    Context menu -> Compare with Clipboard
    when you have a selection and open the context menu (right mouse click) you’ll see a “Compare with clipboard” entry, which opens the internal diffing view. Nice.
    keep groovin’

    IDEA series (viii) line management

    January 11th, 2011
    Rethinking your line management (pun intended)
    Please note that this is for Groovy and Grails development on Mac OS X with Swiss-german keyboard layout and default keymap, so the keybindings may differ on your machine. (For Windows, you most often can replace Cmd with Ctrl)

    This series has come to the point where we do the actual editing of the code and we start with working on the current line. That is, the actions below work on the current line where the caret is positioned in. When it says “or selection” then the action works on the current selection if there is any and on the current line otherwise.
    Cmd Y
    yanks out the current line. I use this a lot. It is quicker than selecting and deleting even when yanking many lines, since you can simply press the same keystroke again (remember the discussion about interaction design?).
    Cmd D
    duplicate the current line (or selection). Again, a much used shortcut for me for making variants of the current line or selection. Some Eclipse converts take issue with this one since they expect a delete action. (It’s worse when you get from IDEA to Eclipse and mistakingly delete a line that you want to duplicate, but then you deserve the pain)
    Cmd /
    comments the current line or selection with a line comment (//). This has a little gotcha when working with non-english keyboards where you need to press Shift to get to the slash character. With my swiss-german layout the shortcut is actually Cmd . (dot) because the dot is where the slash is on english keyboards (see also http://youtrack.jetbrains.net/issue/IDEA-63779).
    Ctrl Shift J
    join the current line with next line. This shortcut is used by many text-editors. It is worth remembering. Much faster than a manual join.
    Cmd Shift up/down
    moves the current line (or selection) one line up or down. This used to have issues in older versions when it tried to be too clever about which lines to jump over, e.g. when you select a whole method, it jumps not per-line but per-method. This makes sense for methods but other blocks are not as easy to support since you may want to nest them. The “intelligent” jumping sometimes led to unexpected results, especially in Groovy code. I haven’t seen any trouble with version X, yet.
    Cmd Alt V
    the “insert explaining variable” refactoring is something you normally call on a selected expression but when there is no selection, it takes the current line as the expression. Now, this has an interesting effect: in a variable assignment, you never have to write the left-hand side! I learned this from IDEA uber-guru Vaclav Pech himself. It works like this: you don’t type “MyType myRef = expression” but only “expression” then strike Cmd Alt V. Try this and enjoy the effect.
    keep groovin’

    IntelliJ IDEA series (vii)

    January 10th, 2011

    helpful selection modes and type inference

    Please note that this is for Groovy and Grails development on Mac OS X with Swiss-german keyboard layout and default keymap, so the keybindings may differ on your machine. (For Windows, you most often can replace Cmd with Ctrl)

    With only a few keystrokes it becomes much easier to move code around in your editor or gather more information.

    Press the Alt key while selecting with the mouse

    and the selection is in “column” mode. Very useful for many well-formatted source files and often even more in resource files with tabular format.

    Column mode

    can also be toggled with a keystroke. I couldn’t quite remember the default keystroke, so I’ve set it to Cmd Alt Backspace. To find your keystroke look in the Edit menu under Column Mode (or as always use Cmd Shift A and type “col”).

    Hold Cmd while hovering over a reference

    and you will see the inferred type like so:

    Note that this is Groovy code and from the content of the dynamically typed myMap, IDEA infers that this is a Map from Strings to Integers, i.e. not only the raw type is inferred but also the generic type! Is that cool or is that cool?

    We touch a very important topic here.

    Many developers are under the misconception that IDEs can only do what the compiler of a specific language does. The Groovy compiler does no type inference but still, IDEA does and uses this info for code completion, analysis, refactorings, warnings on mistyped method names, and so on.

    This shouldn’t come as a big surprise. Even for Java, IDEs do a lot more analysis than what the Java compiler does.

    Using a dynamic language like Groovy does not mean that you lose all the IDE support (as you can hear occasionally). Also note that in Groovy you can always declare your types, making it an optionally typed language.
    Inside IDEA, you can seamlessly switch between Groovy and Java, using cross-language testing, debugging, analysis tools, and refactoring.

    The next post will be about more editing commands.

    keep groovin’