Skip to content

Accessing inner classes

stephanenicolas edited this page Sep 26, 2013 · 7 revisions

BoundBox provides access to all non-static inner classes defined in a given class.


Let's say we have a class Outer like :

@SuppressWarnings("unused")
public class Outer {
    private class Inner {
    }
}

Note that the inner class is private. Thus, the class Outer.Inner can't even be used as type to declare a variable outside of Outer. Nevertheless, with BoundBox, you can write a test that accesses the inner class of Outer. You can also create a BoundBoxOfInner :

@BoundBox( boundClass = Outer.class )
public class OuterTest {
 
    @Test
    public void test_access_inner_class() {
        //GIVEN
        Outer outer = new Outer();
        //WHEN
        Object inner = new BoundBoxOfOuter(outer).boundBox_new_Inner();
       //THEN
        assertTrue(Outer.class.getDeclaredClasses()[0].isAssignableFrom(inner.getClass()));
    }

    @Test
    public void access_bound_box_of_inner_class() {
        //GIVEN
        Outer outer = new Outer();
        //WHEN
        Object inner = new BoundBoxOfOuter(outer).boundBox_new_Inner();
        //THEN
        assertNotNull( new BoundBoxOfOuter(outer).new BoundBoxOfInner(inner) );
     }
}

For all inner classes in a class Outer, BoundBoxOfOuter will contain :

  • an accessor to each of the constructors of the inner class : boundBox_new_Inner(...) ;
  • a non-static inner class to create a BoundBox of the inner classes : BoundBoxOfInner.

The BoundBoxOfInner can then be used to access all fields and methods of Inner.