From 44ce26ee9405ee85f50a7c0f966a0f8e07f2d5d6 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Thu, 7 Jul 2016 17:27:58 +0200 Subject: [PATCH] mobile/bind: fix gomobile bind with custom Java package The change from using strings to objects for passing errors across the language barrier broke the custom java package mode of gombile bind. Fix it and add a runtime test to make sure it won't happen again. Fixes golang/go#16262 Change-Id: Ia7f8afb79556798056f0755758052190081a2dbb Reviewed-on: https://go-review.googlesource.com/24800 Reviewed-by: Hyang-Ah Hana Kim --- bind/genjava.go | 6 +++--- bind/java/CustomPkgTest.java | 15 +++++++++++++++ bind/java/seq_test.go | 27 ++++++++++++++++++++------- 3 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 bind/java/CustomPkgTest.java diff --git a/bind/genjava.go b/bind/genjava.go index 4099260..8632e66 100644 --- a/bind/genjava.go +++ b/bind/genjava.go @@ -538,12 +538,12 @@ var javaNameReplacer = strings.NewReplacer( ) func (g *javaGen) javaPkgName(pkg *types.Package) string { - if g.javaPkg != "" { - return g.javaPkg - } if pkg == nil { return "go" } + if g.javaPkg != "" { + return g.javaPkg + } s := javaNameReplacer.Replace(pkg.Name()) // Look for Java keywords that are not Go keywords, and avoid using // them as a package name. diff --git a/bind/java/CustomPkgTest.java b/bind/java/CustomPkgTest.java new file mode 100644 index 0000000..723389c --- /dev/null +++ b/bind/java/CustomPkgTest.java @@ -0,0 +1,15 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package go; + +import android.test.InstrumentationTestCase; + +import org.golang.custompkg.Testpkg; + +public class CustomPkgTest extends InstrumentationTestCase { + public void testHi() { + Testpkg.Hi(); + } +} diff --git a/bind/java/seq_test.go b/bind/java/seq_test.go index 5535fcb..254e0bf 100644 --- a/bind/java/seq_test.go +++ b/bind/java/seq_test.go @@ -16,15 +16,18 @@ import ( "time" ) -// TestJavaSeqTest runs java test SeqTest.java. -// This requires the gradle command in PATH and -// the Android SDK whose path is available through ANDROID_HOME environment variable. +func TestCustomPkg(t *testing.T) { + runTest(t, []string{ + "golang.org/x/mobile/bind/testpkg", + }, "org.golang.custompkg", "CustomPkgTest") +} + func TestJavaSeqTest(t *testing.T) { runTest(t, []string{ "golang.org/x/mobile/bind/testpkg", "golang.org/x/mobile/bind/testpkg/secondpkg", "golang.org/x/mobile/bind/testpkg/simplepkg", - }, "SeqTest") + }, "", "SeqTest") } // TestJavaSeqBench runs java test SeqBench.java, with the same @@ -38,10 +41,15 @@ func TestJavaSeqTest(t *testing.T) { // // while running the benchmark to see the results. func TestJavaSeqBench(t *testing.T) { - runTest(t, []string{"golang.org/x/mobile/bind/benchmark"}, "SeqBench") + runTest(t, []string{"golang.org/x/mobile/bind/benchmark"}, "", "SeqBench") } -func runTest(t *testing.T, pkgNames []string, javaCls string) { +// runTest runs the Android java test class specified with javaCls. If javaPkg is +// set, it is passed with the -javapkg flag to gomobile. The pkgNames lists the Go +// packages to bind for the test. +// This requires the gradle command in PATH and +// the Android SDK whose path is available through ANDROID_HOME environment variable. +func runTest(t *testing.T, pkgNames []string, javaPkg, javaCls string) { if _, err := run("which gradle"); err != nil { t.Skip("command gradle not found, skipping") } @@ -84,7 +92,12 @@ func runTest(t *testing.T, pkgNames []string, javaCls string) { } } - buf, err := run("gomobile bind -o pkg.aar " + strings.Join(pkgNames, " ")) + cmd := []string{"gomobile", "bind", "-o", "pkg.aar"} + if javaPkg != "" { + cmd = append(cmd, "-javapkg", javaPkg) + } + cmd = append(cmd, pkgNames...) + buf, err := run(strings.Join(cmd, " ")) if err != nil { t.Logf("%s", buf) t.Fatalf("failed to run gomobile bind: %v", err)