As most of other modern IDEs, NetBeans supports a rename refactoring. Focus the variable, methods, class etc. you want to rename and press Ctrl+R. Alternatively you may open a context menu and choose “Refactor, Rename”. The rename not only affects your Java code. If you use JSF as Java EE frontend, then the rename affects the JSF page too. But, there are some restrictions and useful features to know.Let’s assume you have a property, which is accessed from a page.
Java:
private String _name; public String getName() { return _name; } public void setName(String name) { _name = name; }
Page:
<h:inputText value="#{cat.name}"/>
If you now focus “getName” and invoke the rename refactoring to name it “getNameX”, then NetBeans would rename this on the page as expected: “#{cat.nameX}”
Due to a small bug, a rename back to “getName” only affects all occurrence within your Java classes, but the pages. If used on a couple a pages, it might be a mess to find all usages and to manually correct them. In case of this problem, there is a workaround: Rename “setName” to “setNameX” also. Then, a rename to “getName” and “setName” would work fine. This bug is reported to NetBeans, and I’m sure it’s gonna being fixed soon.
The names of a property’s getter and setter methods always have to (re-)named synchronous. Usually you want to keep it synchronized with the field’s name, too. And here NetBeans offers a nice feature. Simply rename the field!
When NetBeans opens the rename dialog, tick “Rename Getters and Setters”.
NetBeans would rename the field, the getter and setter and all occurrence, including those on pages.
—
If you are editing a page and want to rename a property, action method or other, then NetBeans sadly only displays “The Rename refactoring cannot be applied to this context”. Hopefully it will support renaming some day…
But don’t give up. Simply jump directly to the Java code (Ctrl-B, read full description [1]) and rename within the Java code.
[1] NetBeans and Java EE: Code navigation
Do you want to support this blog? You may help to finance the needed hardware.
Want to read more about software development? Purchase one of my books:
Start application development with Java. Learn development foundation, quality control and more.
A Journey through Java EE Technologies whilst developing Web Applications with JavaServer Faces.
Java Lambdas and Parallel Streams
The compact starter: Foundation, supporting structures, parallel processing
No software development, just nature:
A photographic image book. Get it for free. Or pay whatever you like.
No Comments Yet