JDK-6449497 : Clarify IAE thrown by Container.add(Component, int)
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.4.1,6
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,solaris_7
  • CPU: generic,sparc
  • Submitted: 2006-07-17
  • Updated: 2011-01-19
  • Resolved: 2006-08-04
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b95Fixed
Related Reports
Duplicate :  
Relates :  
Description
If components are added to Container using Container.add(Component comp, int
index), and index is not consecutive, IllegalArgumentException will be thrown.

Here is an example:

import java.awt.*;

public class ContainerAdd {
    public static void main(String[] args) throws Exception {
        Container c = new Container();
        int[]  values = {0, 1, 2, 3, 4, 8}; 
        LWComponent comp = new LWComponent();  
        for(int i=0;i<values.length;i++) {
            try {
                c.add(new LWComponent(), values[i]);
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("Unexpected ArrayIndexOutOfBoundsException, index=" + values[i]);
            } catch (IllegalArgumentException e) {
                System.out.println("IllegalArgumentExeption is thrown for index " + values[i]);
                e.printStackTrace();
            }
        }
    }
}

class LWComponent extends Component {
    public LWComponent() {
    }
}

Output in build 1.4.1-beta-b11 is:

IllegalArgumentExeption is thrown for index 8
java.lang.IllegalArgumentException: illegal component position
        at java.awt.Container.addImpl(Container.java:605)
        at java.awt.Container.add(Container.java:346)
        at ContainerAdd.main(ContainerAdd.java:10)

"IllegalArgumentException - if index is invalid" this is unclear. User needs to know what indices are invalid. 
This behavior needs to be documented.
.

Comments
EVALUATION Fixed as part of 6449894, thus closing as duplicate
27-07-2006

EVALUATION it looks likeclosing this bug as fixed is more usable for submitter to verify fix.
27-07-2006

EVALUATION we need to add the same note for all add() methods which have index param, and to adImpl(). Do not forget about special case whan component is already in this container. We will remove it from this container and then check validity of index, so for this case index should be in range [-1, getComponentCount() - 2]
18-07-2006

SUGGESTED FIX *** /tmp/geta6009.Rb6012 2006-07-17 17:52:31.748894305 +0400 --- Container.java 2006-07-17 17:51:44.257183000 +0400 *************** *** 974,980 **** * @param index the position in the container's list at which to * insert the component, where <code>-1</code> * means append to the end ! * @exception IllegalArgumentException if <code>index</code> is invalid * @exception IllegalArgumentException if adding the container's parent * to itself * @exception IllegalArgumentException if adding a window to a container --- 974,982 ---- * @param index the position in the container's list at which to * insert the component, where <code>-1</code> * means append to the end ! * @exception IllegalArgumentException if {@code index} is less then ! zero but not equals {@code -1}; ! if {@code index} is not consecutive with previous values * @exception IllegalArgumentException if adding the container's parent * to itself * @exception IllegalArgumentException if adding a window to a container *** (#1 of 1): [ UNSAVED ] ###@###.###
17-07-2006

EVALUATION We have to describe what "incorrect index" means there. if (index > ncomponents || (index < 0 && index != -1)) { throw new IllegalArgumentException( "illegal component position"); It should be clarified in terms of ncomponents and zero/negative values.
17-07-2006