Donnerstag, 5. März 2015

My IntelliJ IDEA 'wow' Moments

I've been using Eclipse for years. It's been my IDE #1. But lately it got slow. Very slow. Especially when switching branches and Eclipse tried to catch up with the changes. At some point it annoyed me so much, that I decided to give IntelliJ IDEA a try. I had several team mates using IDEA and being totally excited about it
Well, I switched, and I didn't regret this. While there are a lot of good features (like type suggestions which very often suggest exactly what you need, or search by typing everywhere in the UI), here are some features which really made me 'wow'.


Intentions is a way to quickly modify code. ALT-ENTER is you shortcut to access intentions. Depending on the context IntelliJ will suggest you to do different things. F.e.: invert a if-clause condition, create JavaDoc, initialize a field by adding a constructor parameter. You can quickly create a lot of boilerplate code by pressing ALT-ENTER and ALT-INSERT (create code) in turn.

Structural Search

Ever wanted to find a interface which complies to a specific structure? Well, I did. Back in my Eclipse times I wondered if there is a public interface which has exactly one void method expecting exactly one generic parameter. The idea was to implement a call back which will receive one value. Sure, I could have written the interface myself, but this requirement appeared so common to me, that I suspected, that there is some public interface already doing exactly this. Well, with features provided by Eclipse I was stuck.

Until I switched to IntelliJ and discovered Structural Search (CTRL-SHIFT-S). It allows you to search for, well...., structures. You specify something like a multi line template, and let IntelliJ search for everything which matches this template. For my specific needs, the template looked like:

interface $Class$ {   void $MethodName$($ParameterType$ $Parameter$); }

Each parameter is configurable by constraints like pattern, min count, max count, if it's read from or written to, etc.... For my purpose I set $MethodName$ name and $Parameter$ to be exactly one. This led to too many interfaces having this exact structure I was looking for, but they didn't match quite well. Eventually I discovered one interface which looked exactly like what I was looking for. The method was named 'execute'. So I modified it in the search and I also changed the type to a common generic type name to see how many of this kind are there:

interface $Class$ {   void execute(T $Parameter$); }

This yielded exactly one match. Sadly a inner class, but luckily a public static one. So I just used it.

public class ActionCell<C> extends AbstractCell<C> {  public static interface Delegate<T> {  void execute(T object);  }    ... }


Inspections is the IntelliJ way of static code analysis. The fact that there is static code analysis itself is probably not so 'wow', after all there are a lot of tools out there providing exactly this. But since it's integrated into the IDE in the tightest way I experienced until now, it gives you an awesome coding experience. What it makes even more 'wow' is that nearly each inspection has an associated quick fix. Press ALT-Enter and let IntelliJ convert the code for you. Additionally IntelliJ features inspection rules I haven't seen in other products yet. For example it will offer you to generalize List parameters to Collection or even Iterable if you don't use features of the sub interfaces. IntelliJ features 632 inspections for several languages: Java, HTML, CSS, JS, XML.

Mighty Break Points

I love this! I know conditional break points already from Eclipse. But check this out:
  • Chose whether you want to suspend all threads if a break point is hit, or only the thread which hit the break point.
  • Log a message to a console when a break point is hit
  • Log a evaluated expression to console when a break point is hit
  • Limit break point triggering to only one single instance of a class (you can chose the instance at run time)
  • Limit break point triggering to only one class implementation of the base class.
  • Automatically disable the break point once it was hit.
  • Automatically re-enable the break point after another break point was hit.
  • Create 'method break' points. This allows you to create break points based on class name and method name patterns. They will be triggered on entry and on exit of the method. This way you can super easy track (log lines) enter and exit of methods.
  • Create breakpoints which will trigger if a field is accessed or modified (for fairness: Eclipse also supports field break points).

Live Templates

Live Templates are a way to generate code in IntelliJ. The trick is: it allows variables, which will be filled depending on the context. Live Template suggestions will appear whenever you type the configured abbreviation, or when you press CTRL-J. Type 'psf' in class context, and the 'public static final' template will be suggested. Live Templates are context aware. You won't get this suggestion if you're in a method body. Type "iter" and hit enter. IntelliJ will create a for-each block and automatically pick an iterable variable from the current context and prefill the for declaration. Additionally it will correctly choose the item type and create a meaningful item name. The cursor will be placed on the iteratable variable. If you don't like the automatic selection, type what you want to have iterated. IntelliJ will automatically update the item type and the item name. Fancy, eh? Next example: I had problems when writing JavaDoc because in IntelliJ you have to type {@link until the IDE starts suggesting classes (in Eclipse you directly start typing the class name). So I fixed this myself using Live Templates:
{@link $LINK$}
The variable $LINK is configured to be the result of the complete() function, which will open the type suggestion box. Whenever I type @link and hit enter, IntelliJ will complete the expression and offer a type selection for me. Next: Creating a Logger declaration:
private static final java.util.logging.Logger LOG = java.util.logging.Logger.getLogger($CURRENT_CLASS$.class.getName());
$CURRENT_CLASS$ is configured to be className(), which returns the current class name. Packages will be automatically converted to imports if possible, which creates this nice code (for class MyClass):
private static final Logger LOG = Logger.getLogger(MyClass.class.getName());

Analyze Stacktrace

You received a stack trace and want to examine the code places in the project. Stop! Don't look up the classes manually. Hit CTRL-SHIFT-A ('Enter action') and type "stacktrace". This will suggest you: "Analyze Stacktrace...". Paste the stack trace there. A tool window opens containing the stack trace with links which directly jump into code. Great time saver!

Keine Kommentare:

Kommentar veröffentlichen