United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 6684401 JTree isExpanded should not call itself recursively
6684401 : JTree isExpanded should not call itself recursively

Details
Type:
Bug
Submit Date:
2008-04-04
Status:
Resolved
Updated Date:
2011-02-16
Project Name:
JDK
Resolved Date:
2008-07-28
Component:
client-libs
OS:
windows_xp
Sub-Component:
javax.swing
CPU:
x86
Priority:
P2
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:
6u7-rev

Related Reports
Backport:
Backport:

Sub Tasks

Description
FULL PRODUCT VERSION :
java version "1.6.0_02"
Java(TM) SE Runtime Environment (build 1.6.0_02-b05)
Java HotSpot(TM) Client VM (build 1.6.0_02-b05, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
JTree isExpanded(TreePath) is unnecessarily recursive. With the default stack size (or any reasonable size) a TreePath to a few thousand entries will lead to StackOverflowException. Trees are used to represent graphs, e.g., reference chains that can easily exceed this depth. isExpanded(TreePath) can be trivially rewritten as a loop.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
In a loop, lengthen a path by adding children to a branch of a tree, one per level, and keep calling isVisible() on the TreePath that represents the deepest child until you get StackOverflowException.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Some degradation in speed, but no exceptions.
ACTUAL -
StackOverflowException when isVisible(TreePath) calls isExpanded(TreePath).

REPRODUCIBILITY :
This bug can be reproduced always.

                                    

Comments
SUGGESTED FIX

public boolean isExpanded(TreePath path) {

  Object value;

  if(path == null)
    return false;
        
  do{
    value = expandedState.get(path);
    if(value == null || !((Boolean)value).booleanValue()) 
      return false;
  }while( (path=path.getParentPath())!=null );
  
  return true;
}
                                     
2008-06-27
EVALUATION

Assigned to JTree owner
                                     
2008-04-10



Hardware and Software, Engineered to Work Together