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();
}
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With