Completion: factory methods vs. new

Something that occurs quite often to me is the following.

I need to create a new object of some type. But I don't know whether it is a case class/has a factory method or I have to instantiate it using 'new'. Actually I don't do this reasoning. What I do is trying to type the class name (using camel-case completion). Then I begin hitting ctrl+space and getting upset. The problem is that, the class name won't show up until I prepend a 'new' if the class has no companion apply method.

Example:

frame.getContentPane.add(jb <- doesn't work
frame.getContentPane.add(new jb <- works and proposes JButton

Hit ctrl+space to get the completion for JButton. Everyone knows that JButton doens't have a nice factory method, but you know what I mean?

Maybe it's possible to search also the "constructors" that need a new, even if none is given? Of course you automatically need to insert the 'new' if the completion is chosen.

But I think you should be carefull with making the completion more general, since we had the problem some weeks ago, that there were to many completion proposals (ones that you didn't want). Maybe put it into an option for testing and then use it if it works out well.

0
2 comments

That is big question in Java editor too. I'm sure it will be implemented for smart completion (can't promise that it will be soon), but now we can't imagine now what behaviour exactly should be (similar behaviour should be (?) in case of new to complete Factory method). Should it appropriate lookup added if we found some factory method (but not what you want), what about expected class inheritors?

Best regards,
Alexander Podkhalyuzin

0

Maybe the problem is a bit more difficult in Java. In Scala one might call it an convention to create builders as the apply method of the companion, as case classes are doing this and they are part of the language. And they can be "requested" by simply typing the class name (or camel-case shortcut) and appending the parameters. The difference to instantiation using "new" is just the presence or absence of "new".

In Java, factory methods usually are named methods, and to specify what you want you'd need to give a class name and the name of the builder method.

0

Please sign in to leave a comment.