This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.
The hint to create a field will always create a private field, but when used inside a constructor it would be incredibly useful if there was an additional hint (preferably at the top of the list!) to create a private final field. Immutability is a good thing, and should be encouraged as much as possible. I find myself having to manually add final to all my fields and it's a real pain. Also, the hint in its current form seems to make the screen jump to the definition of the new field... that's totally useless as the job has been done, the focus should remain where the user is tying.
Yes -- that's definitely a good idea. Thanks for your input.
I had a look at the NetBeans source code and it seems that the relevant code would be in the file "FieldForUnusedParam.java" which has "Jan Lahoda" listed as the author. The TreeRule/TreePath stuff makes this code require intimate knowledge of the NetBeans Java API to make any edits... but I would strongly recommend that fields be make into final fields as default. (Eclipse has an option to turn this off). Ironically, the 'cancel' field in the FieldForUnusedParam.java file should probably be final ;-)
Created attachment 79936 [details] patch against main-golden, all field creation hints from constructors have final modifier
Created attachment 79937 [details] avoid potential NullPointer... broken logic in previous patch
I note there is potential for code cleanup between FieldForUnusedParam and CreateFieldFix... the former should probably use the latter instead of doing a lot of duplicating the work.
I'd like to NetFIX [1] this bug. Is it possible? [1] http://wiki.netbeans.org/NetFIX
Yes. Please introduce a checkbox inside Options Dialog | Hint | Java. It can be true by default.
Created attachment 80730 [details] option to disable feature
Latest patch includes ability to disable this feature. To do this, I needed to upgrade the FieldForUnusedParam hint to an AbstractHint and move the menu entry from "suggestions" (which I assume are not shown by default) to "general". I then added a Customizer form with a single checkbox. This has the side effect of allowing users to completely disable/promote the field hint (but not the error version). A static accessor for the 'final' preference has been provided to allow the error version of the hint to use the preference. As a refresher, the "error" version is if the user types foo = bar; where bar is defined but foo isn't. If this happens in a constructor and this feature is enabled, the resulting field will be 'final'.
Seems to me a little bit confusing that settings for FieldForUnusedParam influence CreateFieldFix. I've created a separate customizer for it and integrated. Thanks for contribution. PS: Some minor formal rants: - Please add test for everything, see ie. added CreateFieldTest.java:testFinalFromCtor(), its very simple. - Please run them and correct them if necessary -- after last patch being applied, FieldForUnusedParamTest had 2 testcases broken. - Please fill Accessible fields for all visible form components. --- http://hg.netbeans.org/jet-main/rev/cc0fa1dd08b6
@msauer thanks! Re: tests, my biggest problem is learning how to write the tests as there are so many different variations in the codebase. I'll study what you've done here and see how much I can extend that pattern for future contributions. It would greatly increase my productivity to learn how to write test cases for the hinter as loading up a NetBeans instance each time is very time consuming. Reloading java.{editor, hints} is probably theoretically possible but in practice it's not something I've had much success with :-) Will look at the failing test later today. Re: Accessible fields, will do! Didn't realise this.
> Will look at the failing test later today. Not necessary, I've already corrected them as part of the commit. Have a nice day Sam.
Integrated into 'main-golden', will be available in build *200904281401* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress) Changeset: http://hg.netbeans.org/main-golden/rev/cc0fa1dd08b6 User: Max Sauer <msauer@netbeans.org> Log: #146163: create final field hint