5

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
  • Why use wizard when you can type with code assist? Why use refactor for newly crated method? I don't see the point. Commented Dec 4, 2009 at 21:44
  • 1
    Indeed, it's got to be more work to fill in a complex dialog box than just to type the thing in. Commented Dec 4, 2009 at 21:58

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Ctrl+1 doesn't give any suggestions (including method stub). How can I get it?
Ctrl + espace instead of Ctrl+1 worked for me
3

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. :)

4 Comments

"Even though you should work TDD these days" - controversial! :-p
Well, I was desperately looking for a reason to get him out of the outline view to modify his classes :)
That's very similar to how I work, actually [only unfortunately not from a testsuite... :( ], but not what my colleague is looking for.
Even if one is doing TDD, it would be useful to have something create the @Test public void newTestCase { fail("not yet implemented"); } for you automatically.
2

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.