From 61011ba5840df8962f09ebc1766c868a950aea37 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Wed, 14 Sep 2016 18:04:50 +0200 Subject: [PATCH] bind: fix error type Errors was recently converted to use objects as representation instead of strings. Issue golang/go#17073 exposed a few places that wasn't properly updated. Fix them and add the test case from the the issue. Fixes golang/go#17073 Change-Id: I0191993a8427d930540716407fc09032f282fc66 Reviewed-on: https://go-review.googlesource.com/29176 Reviewed-by: David Crawshaw --- bind/genjava.go | 5 ++++- bind/genobjc.go | 2 +- bind/java/SeqTest.java | 6 +++--- bind/objc/SeqTest.m | 9 +++++++++ bind/testdata/issue12328.java.golden | 8 ++++---- bind/testdata/issue12328.objc.h.golden | 4 ++-- bind/testpkg/testpkg.go | 4 ++++ 7 files changed, 27 insertions(+), 11 deletions(-) diff --git a/bind/genjava.go b/bind/genjava.go index a36333d..9653563 100644 --- a/bind/genjava.go +++ b/bind/genjava.go @@ -308,7 +308,7 @@ func (g *JavaGen) javaType(T types.Type) string { // The error type is usually translated into an exception in // Java, however the type can be exposed in other ways, such // as an exported field. - return "String" + return "java.lang.Exception" } switch T := T.(type) { case *types.Basic: @@ -906,6 +906,9 @@ func (g *JavaGen) jniClassSigPrefix(pkg *types.Package) string { } func (g *JavaGen) jniSigType(T types.Type) string { + if isErrorType(T) { + return "Ljava/lang/Exception;" + } switch T := T.(type) { case *types.Basic: switch T.Kind() { diff --git a/bind/genobjc.go b/bind/genobjc.go index 4a936bf..74045ff 100644 --- a/bind/genobjc.go +++ b/bind/genobjc.go @@ -964,7 +964,7 @@ func (g *objcGen) refTypeBase(typ types.Type) string { func (g *objcGen) objcFieldType(t types.Type) string { if isErrorType(t) { - return "NSString*" + return "NSError*" } return g.objcType(t) } diff --git a/bind/java/SeqTest.java b/bind/java/SeqTest.java index c81b682..6348e01 100644 --- a/bind/java/SeqTest.java +++ b/bind/java/SeqTest.java @@ -418,9 +418,9 @@ public class SeqTest extends InstrumentationTestCase { public void testErrorField() { final String want = "an error message"; Node n = Testpkg.newNode("ErrTest"); - n.setErr(want); - String got = n.getErr(); - assertEquals("want back the error message we set", want, got); + n.setErr(new Exception(want)); + Exception got = n.getErr(); + assertEquals("want back the error message we set", want, got.getMessage()); } //test if we have JNI local reference table overflow error diff --git a/bind/objc/SeqTest.m b/bind/objc/SeqTest.m index 58519bf..7709131 100644 --- a/bind/objc/SeqTest.m +++ b/bind/objc/SeqTest.m @@ -294,6 +294,15 @@ static int numI = 0; XCTAssertTrue(GoTestpkgCallIError(num, NO, &error2), @"GoTestpkgCallIError(Number, NO) failed(%@); want success", error2); } +- (void)testErrorField { + NSString *want = @"an error message"; + GoTestpkgNode *n = GoTestpkgNewNode(@"ErrTest"); + n.err = [NSError errorWithDomain:@"SeqTest" code:1 userInfo:@{NSLocalizedDescriptionKey: want}]; + NSError *got = n.err; + NSString *gotMsg = [got.userInfo valueForKey:NSLocalizedDescriptionKey]; + XCTAssertEqualObjects(gotMsg, want, @"err = %@, want %@", gotMsg, want); +} + - (void)testVar { NSString *s = GoTestpkg.stringVar; XCTAssertEqualObjects(s, @"a string var", @"GoTestpkg.StringVar = %@, want 'a string var'", s); diff --git a/bind/testdata/issue12328.java.golden b/bind/testdata/issue12328.java.golden index 52f0d70..fb92fc0 100644 --- a/bind/testdata/issue12328.java.golden +++ b/bind/testdata/issue12328.java.golden @@ -9,16 +9,16 @@ import go.Seq; public final class T extends Seq.Proxy { private T(go.Seq.Ref ref) { super(ref); } - public final native String getErr(); - public final native void setErr(String v); + public final native java.lang.Exception getErr(); + public final native void setErr(java.lang.Exception v); @Override public boolean equals(Object o) { if (o == null || !(o instanceof T)) { return false; } T that = (T)o; - String thisErr = getErr(); - String thatErr = that.getErr(); + java.lang.Exception thisErr = getErr(); + java.lang.Exception thatErr = that.getErr(); if (thisErr == null) { if (thatErr != null) { return false; diff --git a/bind/testdata/issue12328.objc.h.golden b/bind/testdata/issue12328.objc.h.golden index 6ab1b99..5e36fc8 100644 --- a/bind/testdata/issue12328.objc.h.golden +++ b/bind/testdata/issue12328.objc.h.golden @@ -16,8 +16,8 @@ @property(strong, readonly) id _ref; - (id)initWithRef:(id)ref; -- (NSString*)err; -- (void)setErr:(NSString*)v; +- (NSError*)err; +- (void)setErr:(NSError*)v; @end #endif diff --git a/bind/testpkg/testpkg.go b/bind/testpkg/testpkg.go index 0df80e4..a812e9e 100644 --- a/bind/testpkg/testpkg.go +++ b/bind/testpkg/testpkg.go @@ -533,3 +533,7 @@ func NewInitCaller() *InitCaller { } func (ic *InitCaller) Init() {} + +type Issue17073 interface { + OnError(err error) +}