mobile/bind: handle null references from Java in Go

The Seq Java class has a special case for null references. Expand
the special case to Go so that null references from Java are properly
translated to nil.

Fixes golang/go#14228

Change-Id: I915d1f843c9db299d6910480f6d10dae0121a3b4
Reviewed-on: https://go-review.googlesource.com/19460
Reviewed-by: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
Elias Naur
2016-02-12 14:54:21 +00:00
committed by David Crawshaw
parent 46f9e01d1e
commit d1c2f486a3
12 changed files with 35 additions and 14 deletions
+4 -1
View File
@@ -77,6 +77,8 @@ public class Seq {
public native void writeByteArray(byte[] v);
public void writeRef(Ref ref) {
if (ref == null)
ref = RefTracker.nullRef;
tracker.inc(ref);
writeInt32(ref.refnum);
}
@@ -172,9 +174,10 @@ public class Seq {
static final class RefTracker {
private static final int REF_OFFSET = 42;
private static final int NULL_REFNUM = 41; // also known to bind/seq/ref.go
// use single Ref for null Seq.Object
private static final Ref nullRef = new Ref(REF_OFFSET - 1, null);
private static final Ref nullRef = new Ref(NULL_REFNUM, null);
// Next Java object reference number.
//
+8
View File
@@ -381,4 +381,12 @@ public class SeqTest extends AndroidTestCase {
String got = n.getErr();
assertEquals("want back the error message we set", want, got);
}
public void testNullReferences() {
assertTrue(Testpkg.CallWithNull(null, new Testpkg.NullTest.Stub() {
public Testpkg.NullTest Null() {
return null;
}
}));
}
}
+8
View File
@@ -204,3 +204,11 @@ func ReadAsset() string {
}
return string(b)
}
type NullTest interface {
Null() NullTest
}
func CallWithNull(_null NullTest, nuller NullTest) bool {
return _null == nil && nuller.Null() == nil
}