tags:

views:

105

answers:

3

A coworker of mine new to Eclipse (from JBuilder) wants to know how to just click on a class and add a new method. I right-clicked on the class in the Outline view, and sure enough, there's no "new method" command. I can generate getters and setters, and delegate methods, and extract methods, and pull methods up or push them down, all of which are very useful, but I don't see how to create a new method.

I can type private (or public), with no space, and access code-assist with control-space, and create a new method, and then I can right-click on that method and change the method signature from the refactor menu (alt-shift-C), but it seems like there should be a nice way to click on a class somehow and bring up that method signature wizard right away for creating a brand new method.

Is there a way to do this?

+2  A: 

JDT being one of the main plugins bundled with eclipse (which is an application platform on top of the OSGi-based equinox), it is mainly a Java editor.
Meaning: to create a new method... you just type it in a Java editor.

The only other eclipse environment where you see that kind of feature (click on a class and type new methods) is a UML-oriented editor, like the Omondo product.
(In the UML2 project, Papyrus or MDT-UML2Tools might provide that sort of editing capabilities eventually)
But aside that kind of plugin, I do not think you can achieve natively this way of creating new methods.

VonC
A: 

The closest you can get to what you describe is
1. type the method name (no modifier or something else)
2. hit Ctrl-1 -> create method stub
3. hit Alt-Shift-C to change the signature

Turismo
+1  A: 

In addition to Turismo:

Even though you should work TDD these days, your starting point might be the Testcase anyway. So while you at it to define the behaviour of your instances, you do as he suggested.

void someTest() {
 MyClass myInstance = new MyClass();
 myInstance.theNewMethodGoesHere();
}

And because you haven't added "theNewMethodGoesHere" the compiler will mark that thing as undefined. Then you'll use the ctrl+1 key and voila your method gets added. Even without using a bloated dialog. Makes sense in a perfect world, where we work testdriven. :)

pimpf0r
"Even though you should work TDD these days" - controversial! :-p
Grundlefleck
Well, I was desperately looking for a reason to get him out of the outline view to modify his classes :)
pimpf0r
That's very similar to how I work, actually [only unfortunately not from a testsuite... :( ], but not what my colleague is looking for.
skiphoppy