2014-08-01 10:45:18 -04:00
|
|
|
// 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"
|
2016-09-30 12:46:45 +02:00
|
|
|
"go/ast"
|
|
|
|
"go/build"
|
2015-11-13 07:44:59 -05:00
|
|
|
"go/importer"
|
2016-09-30 12:46:45 +02:00
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
2016-03-11 11:52:33 +01:00
|
|
|
"go/types"
|
2016-09-30 12:46:45 +02:00
|
|
|
"io/ioutil"
|
2014-08-01 10:45:18 -04:00
|
|
|
"log"
|
|
|
|
"os"
|
2017-02-21 21:57:37 +01:00
|
|
|
"os/exec"
|
2016-09-30 12:46:45 +02:00
|
|
|
"path/filepath"
|
2017-02-21 21:57:37 +01:00
|
|
|
"strings"
|
2016-09-30 12:46:45 +02:00
|
|
|
|
|
|
|
"golang.org/x/mobile/internal/importers"
|
|
|
|
"golang.org/x/mobile/internal/importers/java"
|
2014-08-01 10:45:18 -04:00
|
|
|
)
|
|
|
|
|
2015-01-06 15:37:05 -05:00
|
|
|
var (
|
2016-09-30 12:46:45 +02:00
|
|
|
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.")
|
2017-01-01 22:43:46 +01:00
|
|
|
javaPkg = flag.String("javapkg", "", "custom Java package path prefix. Valid only with -lang=java.")
|
|
|
|
prefix = flag.String("prefix", "", "custom Objective-C name prefix. Valid only with -lang=objc.")
|
2016-09-30 12:46:45 +02:00
|
|
|
bootclasspath = flag.String("bootclasspath", "", "Java bootstrap classpath.")
|
|
|
|
classpath = flag.String("classpath", "", "Java classpath.")
|
2015-01-06 15:37:05 -05:00
|
|
|
)
|
2014-08-01 10:45:18 -04:00
|
|
|
|
|
|
|
var usage = `The Gobind tool generates Java language bindings for Go.
|
|
|
|
|
|
|
|
For usage details, see doc.go.`
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2015-08-27 12:19:47 -04:00
|
|
|
if *lang != "java" && *javaPkg != "" {
|
|
|
|
log.Fatalf("Invalid option -javapkg for gobind -lang=%s", *lang)
|
2016-12-23 11:18:18 +01:00
|
|
|
} else if *lang != "objc" && *prefix != "" {
|
2015-08-27 12:19:47 -04:00
|
|
|
log.Fatalf("Invalid option -prefix for gobind -lang=%s", *lang)
|
|
|
|
}
|
|
|
|
|
2016-09-30 12:46:45 +02:00
|
|
|
oldCtx := build.Default
|
|
|
|
ctx := &build.Default
|
|
|
|
var allPkg []*build.Package
|
|
|
|
for _, path := range flag.Args() {
|
|
|
|
pkg, err := ctx.Import(path, ".", build.ImportComment)
|
2014-08-01 10:45:18 -04:00
|
|
|
if err != nil {
|
2016-09-30 12:46:45 +02:00
|
|
|
log.Fatalf("package %q: %v", path, err)
|
2014-08-01 10:45:18 -04:00
|
|
|
}
|
2016-03-11 11:52:33 +01:00
|
|
|
allPkg = append(allPkg, pkg)
|
|
|
|
}
|
2016-09-30 12:46:45 +02:00
|
|
|
var classes []*java.Class
|
|
|
|
refs, err := importers.AnalyzePackages(allPkg, "Java/")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(refs.Refs) > 0 {
|
bind,cmd,internal: generate reverse bindings for exported Go structs
Before this CL, the type of the implicit "this" parameter to Java methods
implemented in Go could only be a super class of the generated Java
class. For example, the following GoRunnable type is an implementation of
the Java interface java.lang.Runnable with a toString method:
package somepkg
import "Java/java/lang"
type GoRunnable struct {
lang.Runnable
}
func (r *GoRunnable) ToString(this lang.Runnable) string {
...
}
The "this" parameter is implicit in the sense that the reverse generator
automatically fills it with a reference to the Java instance of
GoRunnable.
Note that "this" has the type Java/java/lang.Runnable, not
Java/go/somepkg.GoRunnable, which renders it impossible to call Java
methods and functions that expect GoRunnable. The most practical example
of this is the Android databinding libraries.
This CL changes the implicit this parameter to always match the exact
type. In the example, the toString implementation becomes:
import gopkg "Java/go/somepkg"
func (r *GoRunnable) ToString(this gopkg.GoRunnable) string {
...
}
One strategy would be to simply treat the generated Java classes
(GoRunnable in our example) as any other Java class and import it
through javap. However, since the Java classes are generated after
importing, this present a chicken-and-egg problem.
Instead, use the newly added support for structs with embedded prefixed types
and synthesize class descriptors for every exported Go struct type.
Change-Id: Ic5ce4a151312bd89f91798ed4088c9959225b448
Reviewed-on: https://go-review.googlesource.com/34776
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-12-31 16:41:36 +01:00
|
|
|
imp := &java.Importer{
|
|
|
|
Bootclasspath: *bootclasspath,
|
|
|
|
Classpath: *classpath,
|
2017-01-01 22:43:46 +01:00
|
|
|
JavaPkg: *javaPkg,
|
bind,cmd,internal: generate reverse bindings for exported Go structs
Before this CL, the type of the implicit "this" parameter to Java methods
implemented in Go could only be a super class of the generated Java
class. For example, the following GoRunnable type is an implementation of
the Java interface java.lang.Runnable with a toString method:
package somepkg
import "Java/java/lang"
type GoRunnable struct {
lang.Runnable
}
func (r *GoRunnable) ToString(this lang.Runnable) string {
...
}
The "this" parameter is implicit in the sense that the reverse generator
automatically fills it with a reference to the Java instance of
GoRunnable.
Note that "this" has the type Java/java/lang.Runnable, not
Java/go/somepkg.GoRunnable, which renders it impossible to call Java
methods and functions that expect GoRunnable. The most practical example
of this is the Android databinding libraries.
This CL changes the implicit this parameter to always match the exact
type. In the example, the toString implementation becomes:
import gopkg "Java/go/somepkg"
func (r *GoRunnable) ToString(this gopkg.GoRunnable) string {
...
}
One strategy would be to simply treat the generated Java classes
(GoRunnable in our example) as any other Java class and import it
through javap. However, since the Java classes are generated after
importing, this present a chicken-and-egg problem.
Instead, use the newly added support for structs with embedded prefixed types
and synthesize class descriptors for every exported Go struct type.
Change-Id: Ic5ce4a151312bd89f91798ed4088c9959225b448
Reviewed-on: https://go-review.googlesource.com/34776
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-12-31 16:41:36 +01:00
|
|
|
}
|
|
|
|
classes, err = imp.Import(refs)
|
2016-09-30 12:46:45 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(classes) > 0 {
|
|
|
|
tmpGopath, err := ioutil.TempDir(os.TempDir(), "gobind-")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpGopath)
|
2017-01-17 02:26:29 +01:00
|
|
|
if err := genJavaPackages(ctx, tmpGopath, classes, refs.Embedders); err != nil {
|
2016-09-30 12:46:45 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
gopath := ctx.GOPATH
|
|
|
|
if gopath != "" {
|
|
|
|
gopath = string(filepath.ListSeparator)
|
|
|
|
}
|
|
|
|
ctx.GOPATH = gopath + tmpGopath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 21:57:37 +01:00
|
|
|
// Make sure the export data for any imported packages are up to date.
|
|
|
|
cmd := exec.Command("go", "install")
|
|
|
|
cmd.Args = append(cmd.Args, flag.Args()...)
|
|
|
|
cmd.Env = append(os.Environ(), "GOPATH="+ctx.GOPATH)
|
2017-06-25 18:52:56 -07:00
|
|
|
cmd.Env = append(cmd.Env, "GOROOT="+ctx.GOROOT)
|
2017-02-21 21:57:37 +01:00
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
// Only report I/O errors. Errors from go install is expected for as-yet
|
|
|
|
// undefined Java wrappers.
|
|
|
|
if _, ok := err.(*exec.ExitError); !ok {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s failed: %v", strings.Join(cmd.Args, " "), err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 12:46:45 +02:00
|
|
|
typePkgs := make([]*types.Package, len(allPkg))
|
|
|
|
fset := token.NewFileSet()
|
|
|
|
conf := &types.Config{
|
|
|
|
Importer: importer.Default(),
|
|
|
|
}
|
|
|
|
conf.Error = func(err error) {
|
|
|
|
// Ignore errors. They're probably caused by as-yet undefined
|
|
|
|
// Java wrappers.
|
|
|
|
}
|
|
|
|
for i, pkg := range allPkg {
|
|
|
|
var files []*ast.File
|
|
|
|
for _, name := range pkg.GoFiles {
|
|
|
|
f, err := parser.ParseFile(fset, filepath.Join(pkg.Dir, name), nil, 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to parse Go file %s: %v", name, err)
|
|
|
|
}
|
|
|
|
files = append(files, f)
|
|
|
|
}
|
2018-01-13 18:29:34 +01:00
|
|
|
tpkg, _ := conf.Check(pkg.ImportPath, fset, files, nil)
|
2016-09-30 12:46:45 +02:00
|
|
|
typePkgs[i] = tpkg
|
|
|
|
}
|
|
|
|
build.Default = oldCtx
|
|
|
|
for _, pkg := range typePkgs {
|
|
|
|
genPkg(pkg, typePkgs, classes)
|
2014-08-01 10:45:18 -04:00
|
|
|
}
|
2016-09-30 12:46:45 +02:00
|
|
|
// Generate the error package and support files
|
|
|
|
genPkg(nil, typePkgs, classes)
|
2014-08-01 10:45:18 -04:00
|
|
|
os.Exit(exitStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
var exitStatus = 0
|
|
|
|
|
|
|
|
func errorf(format string, args ...interface{}) {
|
|
|
|
fmt.Fprintf(os.Stderr, format, args...)
|
2014-08-12 14:29:18 -04:00
|
|
|
fmt.Fprintln(os.Stderr)
|
2014-08-01 10:45:18 -04:00
|
|
|
exitStatus = 1
|
|
|
|
}
|