JDK-4965810 : No reference to containing object when initializing inner class during superinit
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2003-12-09
  • Updated: 2003-12-12
  • Resolved: 2003-12-12
Related Reports
Duplicate :  
Description
^Xo^Xo

Name: gm110360			Date: 12/09/2003


FULL PRODUCT VERSION :
java version "1.4.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [Version 5.00.2195]
Service Pack 3


A DESCRIPTION OF THE PROBLEM :
Given a class extends another class which constructor calls
a method that creates an instance of an inner class of that
class, then another class that extends said class and
overrides the creator method to return inner descendants of
the super class' inner class there is a null reference to
the containing class. What's more, even if the containing
class is null it is possible to create non-static instances
of inner classes and they will never have a non-null
reference to the containing class.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Make a class with an inner class and a creator method
that creates an instance of the inner class which is invoked
from the constructor.
2. Create an extending class which overrides the creator
method to return an inner class that extends the super
class' inner class
3. When instantiated this way, the instance of the inner
class will have a null reference to the containing object.
It may, however, instantiate non-static inner classes of the
extending object if not specifying the outer object
explicitly but they will also have a null reference to the
containing object (see source code)

EXPECTED VERSUS ACTUAL BEHAVIOR :
NullPointerExceptions when trying to reference the outer
object explicitly or accessing it's members. No
NullPointerException when instantiating inner classes when
specifying the outer object explicitly.

Preferrably no NullPointerException, but since it is during
super initialization it might be dangerous. If there is to
be one, it shouldn't be possible to instantiate non-static
inner classes when the outer object is null without getting
a NullPointerException

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
The class BugDescendant is a java application. Decomment either row 12 or 13 to
compile and see the different results.

  Bug.java:

public abstract class Bug {
    
    public Bug() {
        this.createInner();
    }
    
    protected abstract Inner createInner();
    
    protected abstract class Inner {
        
        protected Inner(boolean createInner) {
            if(createInner) {
                this.createInner();
            }
        }
        
        protected abstract Inner createInner();
    }
}

  BugDescendant.java:
public class BugDescendant extends Bug {
    protected Bug.Inner createInner() {
        return this.new InnerDescendant(true);
    }

    protected class InnerDescendant extends Bug.Inner {
        protected InnerDescendant(boolean createInner) {
            super(createInner);
        }
        
        public Bug.Inner createInner() {
            return new InnerDescendant(false); // This doesn't, but creates a
non-static inner class that will never get an outer reference
            return BugDescendant.this.new InnerDescendant(false); // This
generates a NullPointerException
        }
    }
    
    public static void main(String[] args) {
        new BugDescendant();
    }
    
}
---------- END SOURCE ----------
(Incident Review ID: 181261) 
======================================================================

Comments
EVALUATION Use -target 1.4 to get the correct behavior. ###@###.### 2003-12-12
12-12-2003

WORK AROUND Use -target 1.4. ###@###.### 2003-12-12
12-12-2003