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 <mail@eliasnaur.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/bind/genjava.go b/bind/genjava.go
index ff89bea..d4d6eb0 100644
--- a/bind/genjava.go
+++ b/bind/genjava.go
@@ -294,7 +294,7 @@
 	g.genProxyImpl(n)
 	cons := g.constructors[s.obj]
 	for _, f := range cons {
-		if !g.isSigSupported(f.Type()) {
+		if !g.isConsSigSupported(f.Type()) {
 			g.Printf("// skipped constructor %s.%s with unsupported parameter or return types\n\n", n, f.Name())
 			continue
 		}
@@ -354,6 +354,28 @@
 	g.Printf("}\n\n")
 }
 
+// isConsSigSupported reports whether the generators can handle a given
+// constructor signature.
+func (g *JavaGen) isConsSigSupported(t types.Type) bool {
+	if !g.isSigSupported(t) {
+		return false
+	}
+	// Skip constructors taking a single int32 argument
+	// since they clash with the proxy constructors that
+	// take a refnum.
+	params := t.(*types.Signature).Params()
+	if params.Len() != 1 {
+		return true
+	}
+	if t, ok := params.At(0).Type().(*types.Basic); ok {
+		switch t.Kind() {
+		case types.Int32, types.Uint32:
+			return false
+		}
+	}
+	return true
+}
+
 // javaTypeName returns the class name of a given Go type name. If
 // the type name clashes with the package class name, an underscore is
 // appended.
@@ -1112,7 +1134,7 @@
 }
 
 func (g *JavaGen) genJNIConstructor(f *types.Func, sName string) {
-	if !g.isSigSupported(f.Type()) {
+	if !g.isConsSigSupported(f.Type()) {
 		return
 	}
 	sig := f.Type().(*types.Signature)
diff --git a/bind/testdata/testpkg/testpkg.go b/bind/testdata/testpkg/testpkg.go
index 86739fd..cd7a2ea 100644
--- a/bind/testdata/testpkg/testpkg.go
+++ b/bind/testdata/testpkg/testpkg.go
@@ -618,3 +618,11 @@
 func NewMyStruct(ctx context.Context) *MyStruct {
 	return nil
 }
+
+type Int32Constructed struct{}
+
+// Test that constuctors that clash with the internal proxy constructor
+// are skipped.
+func NewInt32Constructed(i int32) *Int32Constructed {
+	return nil
+}