mobile/bind: let Java toString call String method if available
Bind generates a default toString for Go structs. If the struct has a String() string method, use that instead. Change-Id: If83a6f5c9ad03abbd0b939b9120ff8dd2135f713 Reviewed-on: https://go-review.googlesource.com/20656 Reviewed-by: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
parent
d841a034b5
commit
1a0b242e59
|
@ -48,8 +48,12 @@ func (g *javaGen) genStruct(obj *types.TypeName, T *types.Struct) {
|
|||
g.Printf("public final native void set%s(%s v);\n\n", f.Name(), g.javaType(f.Type()))
|
||||
}
|
||||
|
||||
var isStringer bool
|
||||
for _, m := range methods {
|
||||
g.genFuncSignature(m, false, false)
|
||||
t := m.Type().(*types.Signature)
|
||||
isStringer = isStringer || (m.Name() == "String" && t.Params().Len() == 0 && t.Results().Len() == 1 &&
|
||||
types.Identical(t.Results().At(0).Type(), types.Typ[types.String]))
|
||||
}
|
||||
|
||||
g.Printf("@Override public boolean equals(Object o) {\n")
|
||||
|
@ -85,19 +89,22 @@ func (g *javaGen) genStruct(obj *types.TypeName, T *types.Struct) {
|
|||
g.Printf("});\n")
|
||||
g.Printf("}\n\n")
|
||||
|
||||
// TODO(crawshaw): use String() string if it is defined.
|
||||
g.Printf("@Override public String toString() {\n")
|
||||
g.Indent()
|
||||
g.Printf("StringBuilder b = new StringBuilder();\n")
|
||||
g.Printf(`b.append("%s").append("{");`, obj.Name())
|
||||
g.Printf("\n")
|
||||
for _, f := range fields {
|
||||
n := f.Name()
|
||||
g.Printf(`b.append("%s:").append(get%s()).append(",");`, n, n)
|
||||
if isStringer {
|
||||
g.Printf("return String();\n")
|
||||
} else {
|
||||
g.Printf("StringBuilder b = new StringBuilder();\n")
|
||||
g.Printf(`b.append("%s").append("{");`, obj.Name())
|
||||
g.Printf("\n")
|
||||
for _, f := range fields {
|
||||
n := f.Name()
|
||||
g.Printf(`b.append("%s:").append(get%s()).append(",");`, n, n)
|
||||
g.Printf("\n")
|
||||
}
|
||||
g.Printf(`return b.append("}").toString();`)
|
||||
g.Printf("\n")
|
||||
}
|
||||
g.Printf(`return b.append("}").toString();`)
|
||||
g.Printf("\n")
|
||||
g.Outdent()
|
||||
g.Printf("}\n")
|
||||
|
||||
|
|
|
@ -34,3 +34,7 @@ type (
|
|||
|
||||
func (s *S2) M() {
|
||||
}
|
||||
|
||||
func (_ *S2) String() string {
|
||||
return ""
|
||||
}
|
||||
|
|
|
@ -86,6 +86,15 @@ func proxystructs_S2_M(refnum C.int32_t) {
|
|||
v.M()
|
||||
}
|
||||
|
||||
//export proxystructs_S2_String
|
||||
func proxystructs_S2_String(refnum C.int32_t) C.nstring {
|
||||
ref := _seq.FromRefNum(int32(refnum))
|
||||
v := ref.Get().(*structs.S2)
|
||||
res_0 := v.String()
|
||||
_res_0 := encodeString(res_0)
|
||||
return _res_0
|
||||
}
|
||||
|
||||
//export proxystructs_I_M
|
||||
func proxystructs_I_M(refnum C.int32_t) {
|
||||
ref := _seq.FromRefNum(int32(refnum))
|
||||
|
|
|
@ -106,6 +106,14 @@ Java_go_structs_Structs_00024S2_M(JNIEnv* env, jobject this) {
|
|||
proxystructs_S2_M(o);
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_go_structs_Structs_00024S2_String(JNIEnv* env, jobject this) {
|
||||
int32_t o = go_seq_to_refnum(env, this);
|
||||
nstring r0 = proxystructs_S2_String(o);
|
||||
jstring _r0 = go_seq_to_java_string(env, r0);
|
||||
return _r0;
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_go_structs_Structs_00024I_00024Proxy_M(JNIEnv* env, jobject this) {
|
||||
int32_t o = go_seq_to_refnum(env, this);
|
||||
|
|
|
@ -73,6 +73,7 @@ public abstract class Structs {
|
|||
public final go.Seq.Ref ref() { return ref; }
|
||||
|
||||
public native void M();
|
||||
public native String String();
|
||||
@Override public boolean equals(Object o) {
|
||||
if (o == null || !(o instanceof S2)) {
|
||||
return false;
|
||||
|
@ -86,9 +87,7 @@ public abstract class Structs {
|
|||
}
|
||||
|
||||
@Override public String toString() {
|
||||
StringBuilder b = new StringBuilder();
|
||||
b.append("S2").append("{");
|
||||
return b.append("}").toString();
|
||||
return String();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
|
||||
- (id)initWithRef:(id)ref;
|
||||
- (void)m;
|
||||
- (NSString*)string;
|
||||
@end
|
||||
|
||||
@protocol GoStructsI
|
||||
|
|
|
@ -95,6 +95,13 @@ static NSString* errDomain = @"go.structs";
|
|||
proxystructs_S2_M(refnum);
|
||||
}
|
||||
|
||||
- (NSString*)string {
|
||||
int32_t refnum = go_seq_go_to_refnum(self._ref);
|
||||
nstring r0 = proxystructs_S2_String(refnum);
|
||||
NSString *_ret0_ = go_seq_to_objc_string(r0);
|
||||
return _ret0_;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation GoStructsI {
|
||||
|
|
Loading…
Reference in New Issue