Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse auto completion with generic classes and static methods

For generic classes, I usually prefer static methods instead of constructors to avoid tedious redundant type parameters (example given below for clarification).

But when using auto completion, Eclipse always suggests the type parameter, like:

example (ctrl+space) Example<T>

I then have to remove the <T> manually and instead add my static method, like Example.new(). Is there a possiblity to change this behaviour or a completely different convenient shortcut for a better workflow? (I am still working with Eclipse 3, has this been changed in 4?)

static final class Example<T> {

    /** Private, use factory method instead. */
    private Example() {
    }

    public static <T> Example<T> create() {
        return new Example<T>();
    }
}

public static void main(String[] args) {
    Example<Integer> example = Example.create();
}
like image 926
qqilihq Avatar asked Jun 11 '13 11:06

qqilihq


3 Answers

I think it was a bug in eclipse as seen here Auto complete inserts type parameter placeholders too aggressively

And you can have a look at this it may help you Eclipse and Generic Types

like image 122
Mohamed E. ManSour Avatar answered Oct 15 '22 02:10

Mohamed E. ManSour


Having been bothered by this forever I've just noticed that if you complete by pressing '.' rather than 'return' it completes to the type name. So

  • Exam<ctrl+space>. completes to Example. with the completion dialog open suggesting fields and methods of the class.
  • Exam<ctrl+space><return> completes to Example<T> and closes the completion dialog.

I don't see any evidence of the backspace thing working.

like image 35
Mumrah Avatar answered Oct 15 '22 00:10

Mumrah


If immediately after hitting (ctrl-space), you hit the backspace key twice, Eclipse will remove the entire type argument clause. This was implemented to address https://bugs.eclipse.org/bugs/show_bug.cgi?id=301990.

It's not perfect - I would rather eclipse only add the type argument clause when the type name is preceded by new, but it at least makes it manageable.

like image 1
Ian Robertson Avatar answered Oct 15 '22 00:10

Ian Robertson