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>
95 lines
1.9 KiB
Go
95 lines
1.9 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 bind implements a code generator for gobind.
|
|
//
|
|
// See the documentation on the gobind command for usage details
|
|
// and the list of currently supported types.
|
|
// (http://godoc.org/golang.org/x/mobile/cmd/gobind)
|
|
package bind // import "golang.org/x/mobile/bind"
|
|
|
|
// TODO(crawshaw): slice support
|
|
// TODO(crawshaw): channel support
|
|
|
|
import (
|
|
"bytes"
|
|
"go/format"
|
|
"go/token"
|
|
"go/types"
|
|
"io"
|
|
)
|
|
|
|
type (
|
|
GeneratorConfig struct {
|
|
Writer io.Writer
|
|
Fset *token.FileSet
|
|
Pkg *types.Package
|
|
AllPkg []*types.Package
|
|
}
|
|
|
|
fileType int
|
|
)
|
|
|
|
const (
|
|
ObjcM = iota
|
|
ObjcH
|
|
ObjcGoH
|
|
)
|
|
|
|
// GenGo generates a Go stub to support foreign language APIs.
|
|
func GenGo(conf *GeneratorConfig) error {
|
|
buf := new(bytes.Buffer)
|
|
g := &goGen{
|
|
Generator: &Generator{
|
|
Printer: &Printer{Buf: buf, IndentEach: []byte("\t")},
|
|
Fset: conf.Fset,
|
|
AllPkg: conf.AllPkg,
|
|
Pkg: conf.Pkg,
|
|
},
|
|
}
|
|
g.Init()
|
|
if err := g.gen(); err != nil {
|
|
return err
|
|
}
|
|
src := buf.Bytes()
|
|
srcf, err := format.Source(src)
|
|
if err != nil {
|
|
conf.Writer.Write(src) // for debugging
|
|
return err
|
|
}
|
|
_, err = conf.Writer.Write(srcf)
|
|
return err
|
|
}
|
|
|
|
// GenObjc generates the Objective-C API from a Go package.
|
|
func GenObjc(conf *GeneratorConfig, prefix string, ft fileType) error {
|
|
buf := new(bytes.Buffer)
|
|
g := &objcGen{
|
|
Generator: &Generator{
|
|
Printer: &Printer{Buf: buf, IndentEach: []byte("\t")},
|
|
Fset: conf.Fset,
|
|
AllPkg: conf.AllPkg,
|
|
Pkg: conf.Pkg,
|
|
},
|
|
prefix: prefix,
|
|
}
|
|
g.init()
|
|
var err error
|
|
switch ft {
|
|
case ObjcH:
|
|
err = g.genH()
|
|
case ObjcM:
|
|
err = g.genM()
|
|
case ObjcGoH:
|
|
err = g.genGoH()
|
|
default:
|
|
panic("invalid fileType")
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = io.Copy(conf.Writer, buf)
|
|
return err
|
|
}
|