Archive for the ‘Java’ Category

Get Better At: IntelliJ IDEA

Monday, December 13th, 2010

IntelliJ IDEA is a wonderful piece of software.

And that’s all I’m going to say; I won’t run comparisons against other popular IDEs, because that’s a subject for another day. Today, I want to talk about sharpening your skills with IDEA.

Shortcuts

We all know that shortcuts improve our productivity. Here are the ones I find most valuable:

  • CTRL-W and CTRL-SHIFT-W: Select progressively greater / lesser levels of code
  • CTRL-ALT-V: Introduce Variable. See also Introduce Field / Constant
  • type “iter” TAB: Java 5 style for loop Live Template. See sections below for more info on this
  • ALT-LEFT and ALT-RIGHT: View previous / next tab.
  • CTRL-ALT-LEFT and CTRL-ALT-RIGHT: Back / Forward Tab (in history).
  • CTRL-N and CTRL-SHIFT-N: Find Class and Find File.
  • ALT-F12: Show class outline (press again to also show Inherited members).
  • CTRL-D: Duplicate Line.
  • F2 and SHIFT-F2: Go to next / previous error (or warning).
  • ALT-INSERT: Create new file popup.
  • CTRL-SHIFT-F: Find in path.
  • CTRL-ALT-T: Show list of Live Templates.
  • ALT-ENTER: Show Intentions (see below for more info)

Live Templates

Live Templates are snippets of code you can insert in and around pieces of code. The most common one would probably be “Surround with If”, which takes the highlighted code and surrounds it with an If statement. What’s more, it formats the code correctly so you needn’t worry about tab spacing.

Another good Live Template is the for loop generator. Type “iter” and then hit TAB. IDEA intelligently selects a list of collections you might wish to iterate over:

To create you’re own templates, I would recommend you take a look at this Jetbrains blog post describing how to create a null-check template.

Quick Lists
You can create a shortcut to open a popup of commonly used items of your choosing. Within Preferences, go to Quick Lists to define a new list, and then Key Intentions to create a shortcut for that list. As an example:

Intentions
Intentions can be used to perform common actions, or solve problems with your code. Hitting ALT-ENTER over a valid piece of code will give you common actions. Doing the same over an error will give some suggestions such as:

  • Migrate Type
  • Cast expression
  • Import unknown symbol

Below is an example of both forms of intention:

JPA, Glassfish and Eclipse

Tuesday, July 14th, 2009

Today I’ve been wrestling with Glassfish and Eclipse, in particular the part where I try and persist Entity objects to the database. I admit that I started almost from scratch here with very little previous experience, but I still expected it to be easier.

Here is the issue: The DB that Glassfish uses for its JPA is NOT NECESSARILY the same DB you setup in Eclipse, when you choose a Data Source under the project’s “Java Persistance” property pane. I automatically assumed that Glassfish would play nicely with Eclipse and use the same settings, but it is not true.

… which is why when I tried to persist Entities to the Glassfish database, it couldn’t find the table I had set up inside my MySQL datasource. Instead, it was looking inside the default Glassfish datasource, which is located here:

jdbc:derby://localhost:1527/sun-appserv-samples

In the end, I wanted to get into writing beans fast, so I set Eclipse to talk to this DB instead. It is extremely important that you get the correct Schema also, which is accomplished by logging in as “APP”:


Username: APP
Password: APP

For more information, have a look at this page on the Glassfish Wiki.