[ductilej] push by samskivert - A test to illustrate a problem with a primitive field leaking to its p... on 2010-08-09 19:02 GMT

1 view
Skip to first unread message

duct...@googlecode.com

unread,
Aug 9, 2010, 3:02:25 PM8/9/10
to ductil...@googlegroups.com
Revision: 819aa9b72d
Author: Michael Bayne <m...@samskivert.com>
Date: Mon Aug 9 12:02:00 2010
Log: A test to illustrate a problem with a primitive field leaking to its
parent
class before it's initialized (in which case it's null rather than the
default
value for such fields). We can't run code before the super ctor is called,
so
there's no way we can inject the needed default value at that time.
http://code.google.com/p/ductilej/source/detail?r=819aa9b72d

Added:
/src/org/ductilej/tests/InitOrderTest.java

=======================================
--- /dev/null
+++ /src/org/ductilej/tests/InitOrderTest.java Mon Aug 9 12:02:00 2010
@@ -0,0 +1,34 @@
+//
+// $Id$
+
+package org.ductilej.tests;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ * Points out an edge case with finals that we can't help.
+ */
+public class InitOrderTest
+{
+ public static abstract class A {
+ public int fooVal = foo();
+ protected abstract int foo ();
+ }
+
+ public static class B extends A {
+ public final int bar;
+ public B () {
+ bar = 5;
+ }
+ protected int foo () {
+ return bar;
+ }
+ }
+
+ @Test public void testInitOrder() {
+ B b = new B();
+ // assertEquals(0, b.fooVal); // will be 'null' in detyped code
+ assertEquals(5, b.bar);
+ }
+}

Reply all
Reply to author
Forward
0 new messages