Skip to content

Commit

Permalink
bind: adjust invalid java package names
Browse files Browse the repository at this point in the history
Updates golang/go#12273

Change-Id: I8eac3e84d5a473e9ffe45705cea88537573aef61
Reviewed-on: https://go-review.googlesource.com/13873
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
  • Loading branch information
crawshaw committed Aug 24, 2015
1 parent 8b58227 commit bbebc93
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 8 deletions.
1 change: 1 addition & 0 deletions bind/bind_test.go
Expand Up @@ -28,6 +28,7 @@ var tests = []string{
"testdata/structs.go",
"testdata/interfaces.go",
"testdata/issue10788.go",
"testdata/try.go",
}

var fset = token.NewFileSet()
Expand Down
43 changes: 35 additions & 8 deletions bind/genjava.go
Expand Up @@ -11,8 +11,7 @@ import (
"go/types"
"io"
"regexp"
"unicode"
"unicode/utf8"
"strings"
)

// TODO(crawshaw): disallow basic android java type names in exported symbols.
Expand Down Expand Up @@ -546,15 +545,43 @@ import go.Seq;
`

func (g *javaGen) gen() error {
g.Printf(javaPreamble, g.pkg.Name(), g.pkg.Path(), g.pkg.Name())
var javaNameReplacer = strings.NewReplacer(
"-", "_",
".", "_",
)

func (g *javaGen) javaPkgName() string {
s := javaNameReplacer.Replace(g.pkg.Name())
// Look for Java keywords that are not Go keywords, and avoid using
// them as a package name.
//
// This is not a problem for normal Go identifiers as we only expose
// exported symbols. The upper case first letter saves everything
// from accidentally matching except for the package name.
//
// Note that basic type names (like int) are not keywords in Go.
switch s {
case "abstract", "assert", "boolean", "byte", "catch", "char", "class",
"do", "double", "enum", "extends", "final", "finally", "float",
"implements", "instanceof", "int", "long", "native", "private",
"protected", "public", "short", "static", "strictfp", "super",
"synchronized", "this", "throw", "throws", "transient", "try",
"void", "volatile", "while":
s += "_"
}
return s
}

firstRune, size := utf8.DecodeRuneInString(g.pkg.Name())
className := string(unicode.ToUpper(firstRune)) + g.pkg.Name()[size:]
func (g *javaGen) className() string {
return strings.Title(javaNameReplacer.Replace(g.pkg.Name()))
}

func (g *javaGen) gen() error {
g.Printf(javaPreamble, g.javaPkgName(), g.pkg.Path(), g.javaPkgName())

g.Printf("public abstract class %s {\n", className)
g.Printf("public abstract class %s {\n", g.className())
g.Indent()
g.Printf("private %s() {} // uninstantiable\n\n", className)
g.Printf("private %s() {} // uninstantiable\n\n", g.className())
scope := g.pkg.Scope()
names := scope.Names()
var funcs []string
Expand Down
9 changes: 9 additions & 0 deletions bind/testdata/try.go
@@ -0,0 +1,9 @@
// Copyright 2015 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 try has a name that is a Java keyword.
// Gobind has to translate it usefully. See Issue #12273.
package try

func This() string { return "This" }
19 changes: 19 additions & 0 deletions bind/testdata/try.go.golden
@@ -0,0 +1,19 @@
// Package go_try is an autogenerated binder stub for package try.
// gobind -lang=go try
//
// File is generated by gobind. Do not edit.
package go_try

import (
"golang.org/x/mobile/bind/seq"
"try"
)

func proxy_This(out, in *seq.Buffer) {
res := try.This()
out.WriteString(res)
}

func init() {
seq.Register("try", 1, proxy_This)
}
23 changes: 23 additions & 0 deletions bind/testdata/try.java.golden
@@ -0,0 +1,23 @@
// Java Package try_ is a proxy for talking to a Go program.
// gobind -lang=java try
//
// File is generated by gobind. Do not edit.
package go.try_;

import go.Seq;

public abstract class Try {
private Try() {} // uninstantiable

public static String This() {
go.Seq _in = new go.Seq();
go.Seq _out = new go.Seq();
String _result;
Seq.send(DESCRIPTOR, CALL_This, _in, _out);
_result = _out.readString();
return _result;
}

private static final int CALL_This = 1;
private static final String DESCRIPTOR = "try";
}
13 changes: 13 additions & 0 deletions bind/testdata/try.objc.h.golden
@@ -0,0 +1,13 @@
// Objective-C API for talking to try Go package.
// gobind -lang=objc try
//
// File is generated by gobind. Do not edit.

#ifndef __GoTry_H__
#define __GoTry_H__

#include <Foundation/Foundation.h>

FOUNDATION_EXPORT NSString* GoTryThis();

#endif
29 changes: 29 additions & 0 deletions bind/testdata/try.objc.m.golden
@@ -0,0 +1,29 @@
// Objective-C API for talking to try Go package.
// gobind -lang=objc try
//
// File is generated by gobind. Do not edit.

#include "GoTry.h"
#include <Foundation/Foundation.h>
#include "seq.h"

static NSString* errDomain = @"go.try";

@protocol goSeqRefInterface
-(GoSeqRef*) ref;
@end

#define _DESCRIPTOR_ "try"

#define _CALL_This_ 1

NSString* GoTryThis() {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_send(_DESCRIPTOR_, _CALL_This_, &in_, &out_);
NSString* ret0_ = go_seq_readUTF8(&out_);
go_seq_free(&in_);
go_seq_free(&out_);
return ret0_;
}

0 comments on commit bbebc93

Please sign in to comment.