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.

Bug 85950 - For loop completion should be intelligent about uninitialized arrays
Summary: For loop completion should be intelligent about uninitialized arrays
Alias: None
Product: java
Classification: Unclassified
Component: Source (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker (vote)
Assignee: Dusan Balek
Depends on:
Reported: 2006-09-27 18:53 UTC by Petr Hrebejk
Modified: 2007-01-17 10:09 UTC (History)
1 user (show)

See Also:
Exception Reporter:


Note You need to log in before you can comment on or make changes to this bug.
Description Petr Hrebejk 2006-09-27 18:53:07 UTC
I have the following line of code
Node[] addedNodes = new Node[added.size() + 1];

and I use the 'fori' abbreviation to get a for loop. It generates this:

for (int i = 0; i < addedNodes.length; i++) {
Node node = addedNodes[i];

The line assigning the local variable 'node' is useless - I'm going to populate
this array, it has nothing in it yet. It would be nice if the hint could notice
if no element in the array has ever been assigned, and if so generate either
nothing or something like
addedNodes[i] = ;
leaving the caret placed before the ;
Comment 1 Dusan Balek 2006-10-11 17:33:28 UTC
I'm not sure if it is possible to express the described behavior in terms of a
single code template definition. However, if an array initialization is your
frequently used pattern, you can always define a special tempalte for it.

for (int ${IDX newVarName default="idx"} = 0; ${IDX} < ${ARR array
default="arr"}.length; ${IDX}++) {
${ARR}[${IDX}] = ${VALUE default="null"};

Closing as wontfix.
Comment 2 _ tboudreau 2006-10-11 23:22:52 UTC
Reopening as an enhancement - if it cannot be expressed in the template
language, isn't that a weakness of the template language?

It's not so much that I want to generate code that will initialize the array, as
that I don't want 
Node node = addedNodes[i];
to be generated, because it will always be assigning null and the only useful
thing to do is delete it.

I don't think you need to determine if it is *possible* that the array not be
initialized - for a class member that would require flow analysis and it's

But for the case that the array is defined within the scope of the method or
static block, it is certainly possible to determine if there is ever assignment
to any members of that array (handle simple cases only - i.e. don't try to solve
assigning one array variable to another one).

The IDE has enough information to get this right most of the time.  The fact
that the template language doesn't allow for expressing it doesn't mean we
shouldn't consider doing it another way.