Please use the Apache issue tracking system for new NetBeans issues ( !!
Bug 146163 - create final field hint
create final field hint
Product: java
Classification: Unclassified
Component: Hints
All All
: P3 (vote)
: 6.x
Assigned To: Max Sauer
Depends on:
  Show dependency treegraph
Reported: 2008-09-04 12:10 UTC by fommil
Modified: 2009-04-28 18:57 UTC (History)
1 user (show)

See Also:

patch against main-golden, all field creation hints from constructors have final modifier (2.52 KB, patch)
2009-04-12 01:46 UTC, fommil
Details | Diff
avoid potential NullPointer... broken logic in previous patch (2.47 KB, text/plain)
2009-04-12 01:52 UTC, fommil
option to disable feature (14.05 KB, patch)
2009-04-22 22:51 UTC, fommil
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description fommil 2008-09-04 12:10:57 UTC
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.
Comment 1 Max Sauer 2008-09-04 12:43:17 UTC
Yes -- that's definitely a good idea. Thanks for your input. 
Comment 2 fommil 2009-03-08 22:42:51 UTC
I had a look at the NetBeans source code and it seems that the relevant code would be in the file "" 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 file should probably be final ;-)
Comment 3 fommil 2009-04-12 01:46:27 UTC
Created attachment 79936 [details]
patch against main-golden, all field creation hints from constructors have final modifier
Comment 4 fommil 2009-04-12 01:52:51 UTC
Created attachment 79937 [details]
avoid potential NullPointer... broken logic in previous patch
Comment 5 fommil 2009-04-12 01:54:34 UTC
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.
Comment 6 fommil 2009-04-22 13:59:00 UTC
I'd like to NetFIX [1] this bug. Is it possible? [1]
Comment 7 Max Sauer 2009-04-22 15:01:29 UTC
Yes. Please introduce a checkbox inside Options Dialog | Hint | Java. It can be true by default.
Comment 8 fommil 2009-04-22 22:51:15 UTC
Created attachment 80730 [details]
option to disable feature
Comment 9 fommil 2009-04-22 22:57:58 UTC
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'.
Comment 10 Max Sauer 2009-04-27 14:36:40 UTC
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, 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.

Comment 11 fommil 2009-04-27 15:07:43 UTC
@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.
Comment 12 Max Sauer 2009-04-27 15:21:00 UTC
> 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.
Comment 13 Quality Engineering 2009-04-28 18:57:33 UTC
Integrated into 'main-golden', will be available in build *200904281401* on (upload may still be in progress)
User: Max Sauer <>
Log: #146163: create final field hint

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo