Before this CL, generated Java classes or interfaces were inner classes to the top package class. That is both unnecessary and creates ugly class names. Instead, move every generated class and interface to its own package level class. NOTE: This is a backwards incompatible change and requires every client of gomobile APIs to be updated to leave out the package class in the type names. For example, the Go type package pkg type S struct { } now generates (with the default java package name go) a Java class named go.pkg.S. The name before this CL was go.pkg.Pkg.S. Also, change the custom java package to specify the package prefix and not the full package as before. This is an unfortunate change needed to avoid name clashes between two bound packages. On the plus side, the change brings the custom package case closer to the default behaviour, which is a commen prefix, "go.", and a distinct java package for every Go package bound. Change-Id: Iadfaad56e101d1caf7e2a05006f4d384859a20fe Reviewed-on: https://go-review.googlesource.com/27436 Reviewed-by: David Crawshaw <crawshaw@golang.org>
73 lines
2.0 KiB
Go
73 lines
2.0 KiB
Go
// Copyright 2014 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 main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"go/importer"
|
|
"go/types"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
lang = flag.String("lang", "java", "target language for bindings, either java, go, or objc (experimental).")
|
|
outdir = flag.String("outdir", "", "result will be written to the directory instead of stdout.")
|
|
javaPkg = flag.String("javapkg", "", "custom Java package path prefix used instead of the default 'go'. Valid only with -lang=java.")
|
|
prefix = flag.String("prefix", "", "custom Objective-C name prefix used instead of the default 'Go'. Valid only with -lang=objc.")
|
|
)
|
|
|
|
var usage = `The Gobind tool generates Java language bindings for Go.
|
|
|
|
For usage details, see doc.go.`
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
|
|
if *lang != "java" && *javaPkg != "" {
|
|
log.Fatalf("Invalid option -javapkg for gobind -lang=%s", *lang)
|
|
} else if *lang != "objc" && *prefix != "" {
|
|
log.Fatalf("Invalid option -prefix for gobind -lang=%s", *lang)
|
|
}
|
|
|
|
// Make sure the export data for the packages being compiled is up to
|
|
// date. Also use the go tool to provide good error messages for any
|
|
// type checking errors in the provided packages.
|
|
cmd := exec.Command("go", "install")
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
cmd.Args = append(cmd.Args, flag.Args()...)
|
|
if err := cmd.Run(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "%s failed: %v", strings.Join(cmd.Args, " "), err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
var allPkg []*types.Package
|
|
imp := importer.Default()
|
|
for _, arg := range flag.Args() {
|
|
pkg, err := imp.Import(arg)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "could not import package %s: %v", arg, err)
|
|
os.Exit(1)
|
|
}
|
|
allPkg = append(allPkg, pkg)
|
|
}
|
|
for _, pkg := range allPkg {
|
|
genPkg(pkg, allPkg)
|
|
}
|
|
os.Exit(exitStatus)
|
|
}
|
|
|
|
var exitStatus = 0
|
|
|
|
func errorf(format string, args ...interface{}) {
|
|
fmt.Fprintf(os.Stderr, format, args...)
|
|
fmt.Fprintln(os.Stderr)
|
|
exitStatus = 1
|
|
}
|