Original IntelliJ Cast

Let’s talk about type casts — those things you hardly can avoid in JVM-based code. Being a helpful IDE, IntelliJ IDEA now does all the casting stuff for you when you are using its code completion in Java and Groovy.

To get you started, have a look at this Groovy example. In a dynamic language there’s almost no need in type casting at all — you just invoke any method on any object and hope it won’t fail. IDE can help you here, suggesting a list of acceptable choices based on preceding type checks:

In Java, there’s always a need in explicit casting. It’s easy of course, when you know the type to cast expression to. Smart completion after opening parenthesis has been able to do this for ages:

Imagine you’ve just checked an expression type via instanceof and now you want to invoke a method on the cast expression. Quite a natural wish, isn’t it? A week ago you had to write a cast manually, use a light bulb intention action or inst live template. Now you just invoke code completion after the cast expression and it suggests the members of the cast type. A lot simpler, right? Of course, the cast will be inserted automatically:

This works for both basic (Ctrl+Space) and smart completion (Ctrl+Shift+Space). I’ve been using this feature for 2 days now and I already can’t understand how could I survive without it through all the previous years.

Impressed already? But, there’s more. Say you’ve just checked that a certain expression has certain type. Then, you may want to cast that expression and assign the result to a variable, or pass it to a method. All you need is smart completion:

Moreover, as you know, IntelliJ IDEA has second smart completion feature. It’s now also aware of the run-time type checks that you made. After such a check you can perform cast and method invocation in a single action! Well, actually, you have to press Ctrl+Shift+Space two times, but that still counts:


Comments below can no longer be edited.

4 Responses to Original IntelliJ Cast

  1. Avatar

    Vaclav Slovacek says:

    August 26, 2009

    Nice to see that code completition gets some more attention. I personally think, that it is one of the most important features as it is used very often. I miss functionality of CodeCompletionLive plugin that is no longer working in Idea 8 as I prefer pressing just one shortcut and choose the most logical option according to context. I don’t like thinking about what type of completion is appropriate in which situation.

    Recently I found out that maybe it would be interesting to create different shortcuts for different depth in reference hierarchy and always use smart type. For example:
    shortcut 1: name, getName(), Person
    shortcut 2: person.getName(), Person.NAME, Person.create()
    shortcut 3: person.getFriend().getName(), Person.getName().substring()

    If the shortcuts were one next to each other like F10, F11, F12 it might work as a developer I know for example, I usually know that I have to call person.getName() already when writing person.

    I’m not sure whether I have described the idea in an understandable way. Hopefully.

  2. Avatar

    Peter Gromov says:

    August 27, 2009


    In fact the Class name completion (Ctrl+Alt+Space) is available via second Ctrl+Space in Java. So you may choose between just the two completions: Basic and Smart. It may be easier.

    As for separate shortcuts for completion stages, it’s not scalable. A more appropriate solution to me seems as follows: leave the current behavior with possibility not to autocomplete the only variant from the first invocation. Then there’ll always be a single key sequence to invoke second completion. For example, you’ll just press Ctrl, Shift, Space (lookup won’t close at this stage), Space again, then release Ctrl and Shift and you’ll get the variants from second completion.

  3. Avatar

    Vaclav Slovacek says:

    August 28, 2009

    Thanks Peter for the info. What I wanted to say is that there is still a lot of space for improvement/experiments in code completion. I’m not sure whether approach I mentioned would be better for users as until it gets tested by real users you usually don’t know. I realize there might be problems when there is a method call in the middle that requires parameters etc.

    I believe that code completion should get some attention and maybe thinking over several approaches to choose the most intuitive. Code completion is in IDEs for ages and I have to admit that Idea is pretty good in it.

    What I could imagine in basic code completion for example when pressing Alt+Enter when choosing the proper item from the popup IntelliJ would retype the chosen variable to required type.

    For example I have a method that requires integer but I have chosen long. Then I get something like setInt((int) autocompletedLong); or something smarter like setInt(Integer.parseInt(autocompletedString)); … I mean there is a lot of area for research.

    Sorry for just brainstorming ideas, I just tried to show that code completion might be eventually improved, because it is simply the most often used feature.

  4. Avatar

    Peter Gromov says:

    August 28, 2009

    I agree, that code completion needs attention, and let me assure you, it gets that attention. After all, we use it ourselves, and we surely want it to be powerful enough to develop IntelliJ IDEA with pleasure.
    I’ll consider the non-casting conversion you mentioned, although they may be very application-specific. Maybe there’s a place for some user customization here.

Discover more