I can't delete a method using eclipse refactoring? - eclipse

I have a method which I want to delete. This method is being called from n number of classes. I want to delete this method using refactoring and also make eclipse delete all calls to this method rather than go and clean up in each file. I could not find a straight way to do this from refactor (I am using ganymede)

Delete the method body
Then select the method
Refactor -> Inline

I don't think it can be done through refactoring. You can do a Search and replace using regex though.
CTRL-H to bring up the search replace dialog

Related

Auto fix common typo in eclipse

Lets say for example I write many times priavte instead private.
Is there a way to let Eclipse automatically fix my common typo?
Something like construct a map of my common typo to its desire fix,
and then just let Eclipse fix it without asking me about that.
Are there any other IDE\editors that have such support?
There is no builtin support for automatically changing strings. The closest to your request are the templates of the Java editor, but even those must explicitly be activated using CtrlSpace.
To get around your problem, I suggest simply not to write that much yourself. If you want to declare a private field, type just "pr" and hit CtrlSpace to invoke code completion. Eclipse can do code completion quite well, often even without any trigger characters (try it with an empty class file).

NetBeans replace all occurrences of a method with another?

I'm looking for a way to refactor all usages of a method with another. I'm de-singleton-izing a class, and I'm trying to replace all of the getInstance() methods with another method.
In my case, I'd like to change all usages of OldClass.getInstance() to be NewClass.getInstance().getOldClass(0). Is this possible with NetBeans' refactoring tools?
In Netbeans you can refractor the name of the method, but you can't change C.m() to C.m().m2() with refractoring tools.
So the best solutions is to go with Edit->Replace or Replace in project.
Then replace all .getInstance() by .getInstance().getOldClass(0). (Take care of the scope). Don't worry it doesn't replace all occurence directly, it finds match then it displays a tree with checkboxes, you can click on Replace to replace the selected occurence.

Eclipse caret jumps to constructor while typing

While typing in Eclipse (Java) I often have the problem that when I begin to type accessors, the caret jumps down to the beginning of the constructor definition. So in a document like this:
private int mSomeInt;
public
in|public MyClass(){
}
I would like to manually type out the accessor (getter/setter) for mSomeInt, but when I press space after 'public' above, the caret jumps to the beginning of 'public MyClass'.
I often type complete lines to look up and find my methods jumbled with the constructor (like above).
Any help would be appreciated.
Note - this isn't only with accessors but rather any access modifiers that I define before the constructor or another method.
Edit
After unsuccessfully trying Deco's solution below, I've managed to narrow it down a little further.
The problem only happens if I have all the blocks in the file in a collapsed state (ctrl+shift+numPadDivide). I can see the problem is now that the new access modifier I type is then (quickly) collapsed into the below method. i.e. Eclipse is actually taking the first accessor modifier and collapsing everything from there, even though my intention is actually to write a new method.
The only solution I've been able to find is to only edit the source with all the 'fold' elements unfolded.
Under Window -> Preferences -> <Language> (e.g. Java) -> Editor there is a Content Assist menu item where you can configure auto completion and caret placement as well as auto-activation of it and the delay it uses.
Edit:
After your update to the original question I was able to successfully replicate this in Eclipse Indigo. When you have all of the code blocks collapsed it looks like Eclipse assumes that the code you are writing needs to be in that block (rather than as a variable declaration). I'm not sure if this is expected behaviour or not - but the only way around it I've found is to edit the code with the main block open, and then close it after the fact - or turn folding off altogether.
From what I can tell there are various folding plugins/addons that you can get for Eclipse which override the default behaviour and might function better? A quick Google search will be able to get you a list of them quickly.
I'd probably also suggest posting this as an issue on the Eclipse support site for their official answer.
Unfortunately this issue still exists for me in the latest Elcipse version (Kepler).
As the issue only occurs when the document is 'folded', the work around this is to either disable folding in the editor - or disable folding on 'Members' from the :
Preferences -> Java -> Editor -> Folding

Quick way to change method signature for all methods by adding a parameter in eclipse

Is there any quick way to add a parameter in all methods of a package in eclipse?
If you mean to add a few parameters for one method and after that it'll be updated automatically in all packages , there is. Right click on the method and there is option Refactor -> Change Method signature, here you can add/remove parameters.
See http://www.raymondcamden.com/index.cfm/2009/3/16/Multifile-search-and-replace-in-Eclipse. You will have to modify the instructions there just a tad for your needs:
Instead of telling Eclipse to search for a literal string you will be searching for a regular expression pattern that describes a method call/definition. I'll leave that as an exercise for you ;-)

Generate all setXXX calls of a POJO in Eclipse?

Im currently doing a lot of testing with JPA entities, where i have to keep calling the setter methods on the entity that looks something like this :
myEntity.setXXX(value);
myEntity.setYYY(value);
myEntity.setZZZ(value);
Is there any magic shortcut or menu in eclipse IDE to generate all the setter-method-calls that starts with "set", like those displayed in the ctrl-space (auto completion) popup (i think the inherited methods from Object are not being shown at popup) ?
So im imagining something like :
i type myEntity.set
and myEntity.set* are generated right away
Im a lazy programmer and currently using Eclipse Helios IDE.
Thank you !
Edit
Im not looking for source -> generate getter and setter, because that would helps me in generating the methods itself. Generating the method calls is what i want to achieve.
I have found the answer (I was always searching for this thing)...
The easiest way is to expand the class members in the "Package Explorer", sort them by name, multi-select all the setters, and then you have in the clipboard all the method names...
;-)
I like #Oscar's answer. It does lead to some cleanup though.
When I paste from the clipboard, I get something that looks like this:
setOne(int)
setTwo(String)
In order to clean this up, I first add semicolons with this search/replace regexp:
search = (.)$
replace = \1;
Then I add the getter calls (assuming incoming data object is named "data"):
search = s(et.*)\(.*
replace = s\1(data.g\1());
This doesn't handle multiple arguments in a method call...
you can use the outline at right side. There you can sort alphabetically or by declaration order using the toolbar button of the view.
and then you can filter out non required this.
From here also you can copy..all setter functions or getters functions names...
There is eclipse plugin to do that. The name of the plugin is **
FastCode
**. There are so many templates. Among those there is template to generate code for create object of the class and all setters method.
Source --> Generate Getters and Setters...
You can also get at it via the Quick Fix command (Ctrl+1) when the cursor is on a property.
EDIT
If you are simply looking for a faster way to copy properties from one object to another I suggest that you look at using reflection. I think this path would be much easier long term then generating the same-looking code over-and-over.
Commons BeanUtils can take away some of the pain in writing pure reflection code. For example, copyProperties takes a destination bean and either another bean or a Map as the source.