Skip to content

Commit 6bd1229

Browse files
committed
bind,bind/testdata: skip conflicting Java constructors
Our Java proxy class defines a constructor which takes a single int that is the reference number of the Go peer. However, in Go, constructors of the form NewT(i int32) *T generate the same signature as the internal constructor. The longer fix is to avoid using a constructor for initializing proxies. This CL simply skips clashing Go constructors. Reported in https://groups.google.com/forum/#!topic/golang-nuts/EKC_gEjaQH4 Change-Id: I1153f71d8b5a757c499b3ce6e18e2ea5d22dc9e5 Reviewed-on: https://go-review.googlesource.com/c/mobile/+/167660 Run-TryBot: Elias Naur <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
1 parent cde3d95 commit 6bd1229

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

bind/genjava.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func (g *JavaGen) genStruct(s structInfo) {
294294
g.genProxyImpl(n)
295295
cons := g.constructors[s.obj]
296296
for _, f := range cons {
297-
if !g.isSigSupported(f.Type()) {
297+
if !g.isConsSigSupported(f.Type()) {
298298
g.Printf("// skipped constructor %s.%s with unsupported parameter or return types\n\n", n, f.Name())
299299
continue
300300
}
@@ -354,6 +354,28 @@ func (g *JavaGen) genStruct(s structInfo) {
354354
g.Printf("}\n\n")
355355
}
356356

357+
// isConsSigSupported reports whether the generators can handle a given
358+
// constructor signature.
359+
func (g *JavaGen) isConsSigSupported(t types.Type) bool {
360+
if !g.isSigSupported(t) {
361+
return false
362+
}
363+
// Skip constructors taking a single int32 argument
364+
// since they clash with the proxy constructors that
365+
// take a refnum.
366+
params := t.(*types.Signature).Params()
367+
if params.Len() != 1 {
368+
return true
369+
}
370+
if t, ok := params.At(0).Type().(*types.Basic); ok {
371+
switch t.Kind() {
372+
case types.Int32, types.Uint32:
373+
return false
374+
}
375+
}
376+
return true
377+
}
378+
357379
// javaTypeName returns the class name of a given Go type name. If
358380
// the type name clashes with the package class name, an underscore is
359381
// appended.
@@ -1112,7 +1134,7 @@ func (g *JavaGen) genJNIVar(o *types.Var) {
11121134
}
11131135

11141136
func (g *JavaGen) genJNIConstructor(f *types.Func, sName string) {
1115-
if !g.isSigSupported(f.Type()) {
1137+
if !g.isConsSigSupported(f.Type()) {
11161138
return
11171139
}
11181140
sig := f.Type().(*types.Signature)

bind/testdata/testpkg/testpkg.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,3 +618,11 @@ type MyStruct struct {
618618
func NewMyStruct(ctx context.Context) *MyStruct {
619619
return nil
620620
}
621+
622+
type Int32Constructed struct{}
623+
624+
// Test that constuctors that clash with the internal proxy constructor
625+
// are skipped.
626+
func NewInt32Constructed(i int32) *Int32Constructed {
627+
return nil
628+
}

0 commit comments

Comments
 (0)