From b2862722fc3d51ca194b4b563759c767913c7a7f Mon Sep 17 00:00:00 2001 From: Eric Lagergren Date: Fri, 21 Aug 2020 15:52:42 -0700 Subject: [PATCH 1/2] bind: add Go constructor for bound classes Signed-off-by: Eric Lagergren --- FORK | 6 ++++++ bind/genclasses.go | 2 +- internal/importers/java/java.go | 27 +++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 FORK diff --git a/FORK b/FORK new file mode 100644 index 000000000..7339770f4 --- /dev/null +++ b/FORK @@ -0,0 +1,6 @@ +# Changes +# +# 1. Panic when a Java class cannot be found. +# +# 2. Add Go constructor for generated Java wrappers of Go code to facilitate +# Java calling back into Go. diff --git a/bind/genclasses.go b/bind/genclasses.go index 8746ab27e..0d4287b73 100644 --- a/bind/genclasses.go +++ b/bind/genclasses.go @@ -541,7 +541,7 @@ func (g *ClassGen) genGo(cls *java.Class) { g.Printf("C.free(unsafe.Pointer(cls))\n") // Before Go 1.11 clazz was a pointer value, an uintptr after. g.Printf("if uintptr(clazz) == 0 {\n") - g.Printf(" return\n") + g.Printf("\tpanic(`unable to find class %q`)\n", strings.Replace(cls.FindName, ".", "/", -1)) g.Printf("}\n") g.Printf("class_%s = clazz\n", cls.JNIName) for _, fs := range cls.Funcs { diff --git a/internal/importers/java/java.go b/internal/importers/java/java.go index 3a26dfb8b..5bac3d057 100644 --- a/internal/importers/java/java.go +++ b/internal/importers/java/java.go @@ -242,6 +242,33 @@ func (j *Importer) Import(refs *importers.References) ([]*Class, error) { JNIName: JNIMangle(n), PkgName: emb.Name, HasNoArgCon: true, + Funcs: []*FuncSet{ + { + Name: n, + GoName: "New", + Funcs: []*Func{{ + FuncSig: FuncSig{ + Name: "new", // or emb.Name + Desc: "()V", + }, + JNIName: JNIMangle(emb.Name), + Public: true, + Final: true, + Constructor: true, + Ret: &Type{ + Kind: Object, + Class: n, + }, + }}, + CommonSig: CommonSig{ + HasRet: true, + Ret: &Type{ + Kind: Object, + Class: n, + }, + }, + }, + }, } for _, ref := range emb.Refs { jpkg := strings.Replace(ref.Pkg, "/", ".", -1) From e3b8af96d25be850ef310ad324658956f9983491 Mon Sep 17 00:00:00 2001 From: David Stainton Date: Fri, 16 Oct 2020 18:28:56 -0400 Subject: [PATCH 2/2] Add buildmode argument, duh! --- cmd/gomobile/build.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cmd/gomobile/build.go b/cmd/gomobile/build.go index 4811ab787..f7bc3fe3e 100644 --- a/cmd/gomobile/build.go +++ b/cmd/gomobile/build.go @@ -221,6 +221,7 @@ var ( buildO string // -o buildGcflags string // -gcflags buildLdflags string // -ldflags + buildBuildmode string // -buildmode buildTarget string // -target buildTrimpath bool // -trimpath buildWork bool // -work @@ -234,6 +235,7 @@ func addBuildFlags(cmd *command) { cmd.flag.StringVar(&buildO, "o", "", "") cmd.flag.StringVar(&buildGcflags, "gcflags", "", "") cmd.flag.StringVar(&buildLdflags, "ldflags", "", "") + cmd.flag.StringVar(&buildBuildmode, "buildmode", "", "") cmd.flag.StringVar(&buildTarget, "target", "android", "") cmd.flag.StringVar(&buildBundleID, "bundleid", "", "") cmd.flag.StringVar(&buildIOSVersion, "iosversion", "7.0", "") @@ -316,6 +318,9 @@ func goCmdAt(at string, subcmd string, srcs []string, env []string, args ...stri if buildLdflags != "" { cmd.Args = append(cmd.Args, "-ldflags", buildLdflags) } + if buildBuildmode != "" { + cmd.Args = append(cmd.Args, "-buildmode", buildBuildmode) + } if buildTrimpath { cmd.Args = append(cmd.Args, "-trimpath") }