Problem shadowing non-static inner classes

210 views
Skip to first unread message

Michael Portuesi

unread,
Jan 12, 2011, 8:32:21 PM1/12/11
to robol...@googlegroups.com
I'm trying to shadow the Camera.Size object.  This is a non-static inner class on Camera.

It contains only public fields and a constructor, so I attempted to shadow the constructor like so:

@Implements(Camera.Size.class)
public class ShadowCameraSize {
 @RealObject private Camera.Size realCameraSize;
 
 public void __constructor__(int width, int height) {
 realCameraSize.width = width;
 realCameraSize.height = height;
 }
}


And here's a bit of code I used to test the constructor:

@Before
public void setUp() throws Exception {
        Robolectric.bindDefaultShadowClasses();
cameraSize = Robolectric.newInstanceOf(Camera.class).new Size(480, 320);
}
@Test
public void testConstructor() throws Exception {
assertThat(cameraSize.width, equalTo(480));
assertThat(cameraSize.height, equalTo(320));
}

I'm seeing two problems when I run the tests:

1) The constructor is never being invoked, so width/height are both zero and the test fails.

2) The Robolectric wiring test fails, because it cannot match the __constructor__ up with the real class constructor:

testAllImplementationMethodsHaveCorrectSignature(com.xtremelabs.robolectric.bytecode.RobolectricWiringTest)  Time elapsed: 0.26 sec  <<< FAILURE!
java.lang.AssertionError: @Implementation method mismatch: public void com.xtremelabs.robolectric.shadows.ShadowCameraSize.__constructor__(int,int) doesn't match a real method expected:<0> but was:<1>
at org.junit.Assert.fail(Assert.java:91)
at org.junit.Assert.failNotEquals(Assert.java:645)
at org.junit.Assert.assertEquals(Assert.java:126)
at org.junit.Assert.assertEquals(Assert.java:470)
at com.xtremelabs.robolectric.bytecode.RobolectricWiringTest.testAllImplementationMethodsHaveCorrectSignature(RobolectricWiringTest.java:30)


Can someone help me understand what I'm doing wrong here?
I'd like to send a pull request, and this is the biggest thing standing in the way.

Thanks,
M.

Christian Williams

unread,
Jan 12, 2011, 9:00:57 PM1/12/11
to robol...@googlegroups.com
I bet it's because Camera.Size is a non-static inner class.

Try prepending a parameter of the containing class type to the __constructor__ method:

  public void __constructor__(Camera camera, int width, int height) {
  this.camera = camera;
  realCameraSize.width = width;
  realCameraSize.height = height;
  }

The signature for non-static inner class constructors look like that at the bytecode level. Tricky.

--X [typos courtesy of my iPhone]

Michael Portuesi

unread,
Jan 12, 2011, 9:44:19 PM1/12/11
to Robolectric
That did the trick!!! thanks!

M.

On Jan 12, 6:00 pm, Christian Williams <antixian...@gmail.com> wrote:
> I bet it's because Camera.Size is a non-static inner class.
>
> Try prepending a parameter of the containing class type to the
> __constructor__ method:
>
>    public void __constructor__(Camera camera, int width, int height) {
>    this.camera = camera;
>   realCameraSize.width = width;
>   realCameraSize.height = height;
>    }
>
> The signature for non-static inner class constructors look like that at the
> bytecode level. Tricky.
>
> --X [typos courtesy of my iPhone]
>
> > com.xtremelabs.robolectric.bytecode.RobolectricWiringTest.testAllImplementa tionMethodsHaveCorrectSignature(RobolectricWiringTest.java:30)
Reply all
Reply to author
Forward
0 new messages