| 1 |
// Copyright 2016 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package bind |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"path" |
| 10 |
"reflect" |
| 11 |
"strings" |
| 12 |
"unicode" |
| 13 |
"unicode/utf8" |
| 14 |
|
| 15 |
"golang.org/x/mobile/internal/importers" |
| 16 |
"golang.org/x/mobile/internal/importers/java" |
| 17 |
) |
| 18 |
|
| 19 |
type ( |
| 20 |
// ClassGen generates Go and C stubs for Java classes so import statements |
| 21 |
// on the form |
| 22 |
// |
| 23 |
// import "Java/classpath/to/Class" |
| 24 |
// |
| 25 |
// will work. |
| 26 |
ClassGen struct { |
| 27 |
*Printer |
| 28 |
// JavaPkg is the Java package prefix for the generated classes. The prefix is prepended to the Go |
| 29 |
// package name to create the full Java package name. |
| 30 |
JavaPkg string |
| 31 |
imported map[string]struct{} |
| 32 |
// The list of imported Java classes |
| 33 |
classes []*java.Class |
| 34 |
// The list of Go package paths with Java interfaces inside |
| 35 |
jpkgs []string |
| 36 |
// For each Go package path, the list of Java classes. |
| 37 |
typePkgs map[string][]*java.Class |
| 38 |
// For each Go package path, the Java class with static functions |
| 39 |
// or constants. |
| 40 |
clsPkgs map[string]*java.Class |
| 41 |
// goClsMap is the map of Java class names to Go type names, qualified with package name. Go types |
| 42 |
// that implement Java classes need Super methods and Unwrap methods. |
| 43 |
goClsMap map[string]string |
| 44 |
// goClsImports is the list of imports of user packages that contains the Go types implementing Java |
| 45 |
// classes. |
| 46 |
goClsImports []string |
| 47 |
} |
| 48 |
) |
| 49 |
|
| 50 |
func (g *ClassGen) isSupported(t *java.Type) bool { |
| 51 |
switch t.Kind { |
| 52 |
case java.Array: |
| 53 |
// TODO: Support all array types |
| 54 |
return t.Elem.Kind == java.Byte |
| 55 |
default: |
| 56 |
return true |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
func (g *ClassGen) isFuncSetSupported(fs *java.FuncSet) bool { |
| 61 |
for _, f := range fs.Funcs { |
| 62 |
if g.isFuncSupported(f) { |
| 63 |
return true |
| 64 |
} |
| 65 |
} |
| 66 |
return false |
| 67 |
} |
| 68 |
|
| 69 |
func (g *ClassGen) isFuncSupported(f *java.Func) bool { |
| 70 |
for _, a := range f.Params { |
| 71 |
if !g.isSupported(a) { |
| 72 |
return false |
| 73 |
} |
| 74 |
} |
| 75 |
if f.Ret != nil { |
| 76 |
return g.isSupported(f.Ret) |
| 77 |
} |
| 78 |
return true |
| 79 |
} |
| 80 |
|
| 81 |
func (g *ClassGen) goType(t *java.Type, local bool) string { |
| 82 |
if t == nil { |
| 83 |
// interface{} is used for parameters types for overloaded methods |
| 84 |
// where no common ancestor type exists. |
| 85 |
return "interface{}" |
| 86 |
} |
| 87 |
switch t.Kind { |
| 88 |
case java.Int: |
| 89 |
return "int32" |
| 90 |
case java.Boolean: |
| 91 |
return "bool" |
| 92 |
case java.Short: |
| 93 |
return "int16" |
| 94 |
case java.Char: |
| 95 |
return "uint16" |
| 96 |
case java.Byte: |
| 97 |
return "byte" |
| 98 |
case java.Long: |
| 99 |
return "int64" |
| 100 |
case java.Float: |
| 101 |
return "float32" |
| 102 |
case java.Double: |
| 103 |
return "float64" |
| 104 |
case java.String: |
| 105 |
return "string" |
| 106 |
case java.Array: |
| 107 |
return "[]" + g.goType(t.Elem, local) |
| 108 |
case java.Object: |
| 109 |
name := goClsName(t.Class) |
| 110 |
if !local { |
| 111 |
name = "Java." + name |
| 112 |
} |
| 113 |
return name |
| 114 |
default: |
| 115 |
panic("invalid kind") |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// Init initializes the class wrapper generator. Classes is the |
| 120 |
// list of classes to wrap, goClasses is the list of Java classes |
| 121 |
// implemented in Go. |
| 122 |
func (g *ClassGen) Init(classes []*java.Class, goClasses []importers.Struct) { |
| 123 |
g.goClsMap = make(map[string]string) |
| 124 |
impMap := make(map[string]struct{}) |
| 125 |
for _, s := range goClasses { |
| 126 |
n := s.Pkg + "." + s.Name |
| 127 |
jn := n |
| 128 |
if g.JavaPkg != "" { |
| 129 |
jn = g.JavaPkg + "." + jn |
| 130 |
} |
| 131 |
g.goClsMap[jn] = n |
| 132 |
if _, exists := impMap[s.PkgPath]; !exists { |
| 133 |
impMap[s.PkgPath] = struct{}{} |
| 134 |
g.goClsImports = append(g.goClsImports, s.PkgPath) |
| 135 |
} |
| 136 |
} |
| 137 |
g.classes = classes |
| 138 |
g.imported = make(map[string]struct{}) |
| 139 |
g.typePkgs = make(map[string][]*java.Class) |
| 140 |
g.clsPkgs = make(map[string]*java.Class) |
| 141 |
pkgSet := make(map[string]struct{}) |
| 142 |
for _, cls := range classes { |
| 143 |
g.imported[cls.Name] = struct{}{} |
| 144 |
clsPkg := strings.Replace(cls.Name, ".", "/", -1) |
| 145 |
g.clsPkgs[clsPkg] = cls |
| 146 |
typePkg := path.Dir(clsPkg) |
| 147 |
g.typePkgs[typePkg] = append(g.typePkgs[typePkg], cls) |
| 148 |
if _, exists := pkgSet[clsPkg]; !exists { |
| 149 |
pkgSet[clsPkg] = struct{}{} |
| 150 |
g.jpkgs = append(g.jpkgs, clsPkg) |
| 151 |
} |
| 152 |
if _, exists := pkgSet[typePkg]; !exists { |
| 153 |
pkgSet[typePkg] = struct{}{} |
| 154 |
g.jpkgs = append(g.jpkgs, typePkg) |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
// Packages return the list of Go packages to be generated. |
| 160 |
func (g *ClassGen) Packages() []string { |
| 161 |
return g.jpkgs |
| 162 |
} |
| 163 |
|
| 164 |
func (g *ClassGen) GenPackage(idx int) { |
| 165 |
jpkg := g.jpkgs[idx] |
| 166 |
g.Printf(gobindPreamble) |
| 167 |
g.Printf("package %s\n\n", path.Base(jpkg)) |
| 168 |
g.Printf("import \"Java\"\n\n") |
| 169 |
g.Printf("const _ = Java.Dummy\n\n") |
| 170 |
for _, cls := range g.typePkgs[jpkg] { |
| 171 |
g.Printf("type %s Java.%s\n", cls.PkgName, goClsName(cls.Name)) |
| 172 |
} |
| 173 |
if cls, ok := g.clsPkgs[jpkg]; ok { |
| 174 |
g.Printf("const (\n") |
| 175 |
g.Indent() |
| 176 |
// Constants |
| 177 |
for _, v := range cls.Vars { |
| 178 |
if g.isSupported(v.Type) && v.Constant() { |
| 179 |
g.Printf("%s = %s\n", initialUpper(v.Name), v.Val) |
| 180 |
} |
| 181 |
} |
| 182 |
g.Outdent() |
| 183 |
g.Printf(")\n\n") |
| 184 |
|
| 185 |
g.Printf("var (\n") |
| 186 |
g.Indent() |
| 187 |
// Functions |
| 188 |
loop: |
| 189 |
for _, fs := range cls.Funcs { |
| 190 |
for _, f := range fs.Funcs { |
| 191 |
if f.Public && g.isFuncSupported(f) { |
| 192 |
g.Printf("%s func", fs.GoName) |
| 193 |
g.genFuncDecl(false, fs) |
| 194 |
g.Printf("\n") |
| 195 |
continue loop |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
g.Printf("// Cast takes a proxy for a Java object and converts it to a %s proxy.\n", cls.Name) |
| 200 |
g.Printf("// Cast panics if the argument is not a proxy or if the underlying object does\n") |
| 201 |
g.Printf("// not extend or implement %s.\n", cls.Name) |
| 202 |
g.Printf("Cast func(v interface{}) Java.%s\n", goClsName(cls.Name)) |
| 203 |
g.Outdent() |
| 204 |
g.Printf(")\n\n") |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
func (g *ClassGen) GenGo() { |
| 209 |
g.Printf(classesGoHeader) |
| 210 |
for _, cls := range g.classes { |
| 211 |
pkgName := strings.Replace(cls.Name, ".", "/", -1) |
| 212 |
g.Printf("import %q\n", "Java/"+pkgName) |
| 213 |
} |
| 214 |
for _, imp := range g.goClsImports { |
| 215 |
g.Printf("import %q\n", imp) |
| 216 |
} |
| 217 |
if len(g.classes) > 0 { |
| 218 |
g.Printf("import \"unsafe\"\n\n") |
| 219 |
g.Printf("import \"reflect\"\n\n") |
| 220 |
g.Printf("import \"fmt\"\n\n") |
| 221 |
} |
| 222 |
g.Printf("type proxy interface { Bind_proxy_refnum__() int32 }\n\n") |
| 223 |
g.Printf("// Suppress unused package error\n\n") |
| 224 |
g.Printf("var _ = _seq.FromRefNum\n") |
| 225 |
g.Printf("const _ = Java.Dummy\n\n") |
| 226 |
g.Printf("//export initClasses\n") |
| 227 |
g.Printf("func initClasses() {\n") |
| 228 |
g.Indent() |
| 229 |
g.Printf("C.init_proxies()\n") |
| 230 |
for _, cls := range g.classes { |
| 231 |
g.Printf("init_%s()\n", cls.JNIName) |
| 232 |
} |
| 233 |
g.Outdent() |
| 234 |
g.Printf("}\n\n") |
| 235 |
for _, cls := range g.classes { |
| 236 |
g.genGo(cls) |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
func (g *ClassGen) GenH() { |
| 241 |
g.Printf(classesHHeader) |
| 242 |
for _, tn := range []string{"jint", "jboolean", "jshort", "jchar", "jbyte", "jlong", "jfloat", "jdouble", "nstring", "nbyteslice"} { |
| 243 |
g.Printf("typedef struct ret_%s {\n", tn) |
| 244 |
g.Printf(" %s res;\n", tn) |
| 245 |
g.Printf(" jint exc;\n") |
| 246 |
g.Printf("} ret_%s;\n", tn) |
| 247 |
} |
| 248 |
g.Printf("\n") |
| 249 |
for _, cls := range g.classes { |
| 250 |
for _, fs := range cls.AllMethods { |
| 251 |
for _, f := range fs.Funcs { |
| 252 |
if !g.isFuncSupported(f) { |
| 253 |
continue |
| 254 |
} |
| 255 |
g.Printf("extern ") |
| 256 |
g.genCMethodDecl("cproxy", cls.JNIName, f) |
| 257 |
g.Printf(";\n") |
| 258 |
if _, ok := g.goClsMap[cls.Name]; ok { |
| 259 |
g.Printf("extern ") |
| 260 |
g.genCMethodDecl("csuper", cls.JNIName, f) |
| 261 |
g.Printf(";\n") |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
for _, cls := range g.classes { |
| 267 |
g.genH(cls) |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
func (g *ClassGen) GenC() { |
| 272 |
g.Printf(classesCHeader) |
| 273 |
for _, cls := range g.classes { |
| 274 |
g.Printf("static jclass class_%s;\n", cls.JNIName) |
| 275 |
if _, ok := g.goClsMap[cls.Name]; ok { |
| 276 |
g.Printf("static jclass sclass_%s;\n", cls.JNIName) |
| 277 |
} |
| 278 |
for _, fs := range cls.Funcs { |
| 279 |
for _, f := range fs.Funcs { |
| 280 |
if !f.Public || !g.isFuncSupported(f) { |
| 281 |
continue |
| 282 |
} |
| 283 |
g.Printf("static jmethodID m_s_%s_%s;\n", cls.JNIName, f.JNIName) |
| 284 |
} |
| 285 |
} |
| 286 |
for _, fs := range cls.AllMethods { |
| 287 |
for _, f := range fs.Funcs { |
| 288 |
if g.isFuncSupported(f) { |
| 289 |
g.Printf("static jmethodID m_%s_%s;\n", cls.JNIName, f.JNIName) |
| 290 |
if _, ok := g.goClsMap[cls.Name]; ok { |
| 291 |
g.Printf("static jmethodID sm_%s_%s;\n", cls.JNIName, f.JNIName) |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
g.genC(cls) |
| 297 |
} |
| 298 |
g.Printf("\n") |
| 299 |
g.Printf("void init_proxies() {\n") |
| 300 |
g.Indent() |
| 301 |
g.Printf("JNIEnv *env = go_seq_push_local_frame(%d);\n", len(g.classes)) |
| 302 |
g.Printf("jclass clazz;\n") |
| 303 |
for _, cls := range g.classes { |
| 304 |
g.Printf("clazz = go_seq_find_class(%q);\n", strings.Replace(cls.FindName, ".", "/", -1)) |
| 305 |
g.Printf("if (clazz != NULL) {\n") |
| 306 |
g.Indent() |
| 307 |
g.Printf("class_%s = (*env)->NewGlobalRef(env, clazz);\n", cls.JNIName) |
| 308 |
if _, ok := g.goClsMap[cls.Name]; ok { |
| 309 |
g.Printf("sclass_%s = (*env)->GetSuperclass(env, clazz);\n", cls.JNIName) |
| 310 |
g.Printf("sclass_%s = (*env)->NewGlobalRef(env, sclass_%s);\n", cls.JNIName, cls.JNIName) |
| 311 |
} |
| 312 |
for _, fs := range cls.Funcs { |
| 313 |
for _, f := range fs.Funcs { |
| 314 |
if !f.Public || !g.isFuncSupported(f) { |
| 315 |
continue |
| 316 |
} |
| 317 |
g.Printf("m_s_%s_%s = ", cls.JNIName, f.JNIName) |
| 318 |
if f.Constructor { |
| 319 |
g.Printf("go_seq_get_method_id(clazz, \"<init>\", %q);\n", f.Desc) |
| 320 |
} else { |
| 321 |
g.Printf("go_seq_get_static_method_id(clazz, %q, %q);\n", f.Name, f.Desc) |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
for _, fs := range cls.AllMethods { |
| 326 |
for _, f := range fs.Funcs { |
| 327 |
if g.isFuncSupported(f) { |
| 328 |
g.Printf("m_%s_%s = go_seq_get_method_id(clazz, %q, %q);\n", cls.JNIName, f.JNIName, f.Name, f.Desc) |
| 329 |
if _, ok := g.goClsMap[cls.Name]; ok { |
| 330 |
g.Printf("sm_%s_%s = go_seq_get_method_id(sclass_%s, %q, %q);\n", cls.JNIName, f.JNIName, cls.JNIName, f.Name, f.Desc) |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
g.Outdent() |
| 336 |
g.Printf("}\n") |
| 337 |
} |
| 338 |
g.Printf("go_seq_pop_local_frame(env);\n") |
| 339 |
g.Outdent() |
| 340 |
g.Printf("}\n\n") |
| 341 |
for _, cls := range g.classes { |
| 342 |
for _, fs := range cls.AllMethods { |
| 343 |
for _, f := range fs.Funcs { |
| 344 |
if !g.isFuncSupported(f) { |
| 345 |
continue |
| 346 |
} |
| 347 |
g.genCMethodDecl("cproxy", cls.JNIName, f) |
| 348 |
g.genCMethodBody(cls, f, false) |
| 349 |
if _, ok := g.goClsMap[cls.Name]; ok { |
| 350 |
g.genCMethodDecl("csuper", cls.JNIName, f) |
| 351 |
g.genCMethodBody(cls, f, true) |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
func (g *ClassGen) GenInterfaces() { |
| 359 |
g.Printf(classesPkgHeader) |
| 360 |
for _, cls := range g.classes { |
| 361 |
g.genInterface(cls) |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
func (g *ClassGen) genCMethodBody(cls *java.Class, f *java.Func, virtual bool) { |
| 366 |
g.Printf(" {\n") |
| 367 |
g.Indent() |
| 368 |
// Add 1 for the 'this' argument |
| 369 |
g.Printf("JNIEnv *env = go_seq_push_local_frame(%d);\n", len(f.Params)+1) |
| 370 |
g.Printf("// Must be a Java object\n") |
| 371 |
g.Printf("jobject _this = go_seq_from_refnum(env, this, NULL, NULL);\n") |
| 372 |
for i, a := range f.Params { |
| 373 |
g.genCToJava(fmt.Sprintf("a%d", i), a) |
| 374 |
} |
| 375 |
if f.Ret != nil { |
| 376 |
g.Printf("%s res = ", f.Ret.JNIType()) |
| 377 |
} |
| 378 |
g.Printf("(*env)->Call") |
| 379 |
if virtual { |
| 380 |
g.Printf("Nonvirtual") |
| 381 |
} |
| 382 |
if f.Ret != nil { |
| 383 |
g.Printf("%s", f.Ret.JNICallType()) |
| 384 |
} else { |
| 385 |
g.Printf("Void") |
| 386 |
} |
| 387 |
g.Printf("Method(env, _this, ") |
| 388 |
if virtual { |
| 389 |
g.Printf("sclass_%s, sm_%s_%s", cls.JNIName, cls.JNIName, f.JNIName) |
| 390 |
} else { |
| 391 |
g.Printf("m_%s_%s", cls.JNIName, f.JNIName) |
| 392 |
} |
| 393 |
for i := range f.Params { |
| 394 |
g.Printf(", _a%d", i) |
| 395 |
} |
| 396 |
g.Printf(");\n") |
| 397 |
g.Printf("jobject _exc = go_seq_get_exception(env);\n") |
| 398 |
g.Printf("int32_t _exc_ref = go_seq_to_refnum(env, _exc);\n") |
| 399 |
if f.Ret != nil { |
| 400 |
g.genCRetClear("res", f.Ret, "_exc") |
| 401 |
g.genJavaToC("res", f.Ret) |
| 402 |
} |
| 403 |
g.Printf("go_seq_pop_local_frame(env);\n") |
| 404 |
if f.Ret != nil { |
| 405 |
g.Printf("ret_%s __res = {_res, _exc_ref};\n", f.Ret.CType()) |
| 406 |
g.Printf("return __res;\n") |
| 407 |
} else { |
| 408 |
g.Printf("return _exc_ref;\n") |
| 409 |
} |
| 410 |
g.Outdent() |
| 411 |
g.Printf("}\n\n") |
| 412 |
} |
| 413 |
|
| 414 |
func initialUpper(s string) string { |
| 415 |
if s == "" { |
| 416 |
return "" |
| 417 |
} |
| 418 |
r, n := utf8.DecodeRuneInString(s) |
| 419 |
return string(unicode.ToUpper(r)) + s[n:] |
| 420 |
} |
| 421 |
|
| 422 |
func (g *ClassGen) genFuncDecl(local bool, fs *java.FuncSet) { |
| 423 |
g.Printf("(") |
| 424 |
for i, a := range fs.Params { |
| 425 |
if i > 0 { |
| 426 |
g.Printf(", ") |
| 427 |
} |
| 428 |
g.Printf("a%d ", i) |
| 429 |
if i == len(fs.Params)-1 && fs.Variadic { |
| 430 |
g.Printf("...") |
| 431 |
} |
| 432 |
g.Printf("%s", g.goType(a, local)) |
| 433 |
} |
| 434 |
g.Printf(")") |
| 435 |
if fs.Throws { |
| 436 |
if fs.HasRet { |
| 437 |
g.Printf(" (%s, error)", g.goType(fs.Ret, local)) |
| 438 |
} else { |
| 439 |
g.Printf(" error") |
| 440 |
} |
| 441 |
} else if fs.HasRet { |
| 442 |
g.Printf(" %s", g.goType(fs.Ret, local)) |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
func (g *ClassGen) genC(cls *java.Class) { |
| 447 |
for _, fs := range cls.Funcs { |
| 448 |
for _, f := range fs.Funcs { |
| 449 |
if !f.Public || !g.isFuncSupported(f) { |
| 450 |
continue |
| 451 |
} |
| 452 |
g.genCFuncDecl(cls.JNIName, f) |
| 453 |
g.Printf(" {\n") |
| 454 |
g.Indent() |
| 455 |
g.Printf("JNIEnv *env = go_seq_push_local_frame(%d);\n", len(f.Params)) |
| 456 |
for i, a := range f.Params { |
| 457 |
g.genCToJava(fmt.Sprintf("a%d", i), a) |
| 458 |
} |
| 459 |
if f.Constructor { |
| 460 |
g.Printf("jobject res = (*env)->NewObject(env") |
| 461 |
} else if f.Ret != nil { |
| 462 |
g.Printf("%s res = (*env)->CallStatic%sMethod(env", f.Ret.JNIType(), f.Ret.JNICallType()) |
| 463 |
} else { |
| 464 |
g.Printf("(*env)->CallStaticVoidMethod(env") |
| 465 |
} |
| 466 |
g.Printf(", class_%s, m_s_%s_%s", cls.JNIName, cls.JNIName, f.JNIName) |
| 467 |
for i := range f.Params { |
| 468 |
g.Printf(", _a%d", i) |
| 469 |
} |
| 470 |
g.Printf(");\n") |
| 471 |
g.Printf("jobject _exc = go_seq_get_exception(env);\n") |
| 472 |
g.Printf("int32_t _exc_ref = go_seq_to_refnum(env, _exc);\n") |
| 473 |
if f.Ret != nil { |
| 474 |
g.genCRetClear("res", f.Ret, "_exc") |
| 475 |
g.genJavaToC("res", f.Ret) |
| 476 |
} |
| 477 |
g.Printf("go_seq_pop_local_frame(env);\n") |
| 478 |
if f.Ret != nil { |
| 479 |
g.Printf("ret_%s __res = {_res, _exc_ref};\n", f.Ret.CType()) |
| 480 |
g.Printf("return __res;\n") |
| 481 |
} else { |
| 482 |
g.Printf("return _exc_ref;\n") |
| 483 |
} |
| 484 |
g.Outdent() |
| 485 |
g.Printf("}\n\n") |
| 486 |
} |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
func (g *ClassGen) genH(cls *java.Class) { |
| 491 |
for _, fs := range cls.Funcs { |
| 492 |
for _, f := range fs.Funcs { |
| 493 |
if !f.Public || !g.isFuncSupported(f) { |
| 494 |
continue |
| 495 |
} |
| 496 |
g.Printf("extern ") |
| 497 |
g.genCFuncDecl(cls.JNIName, f) |
| 498 |
g.Printf(";\n") |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
func (g *ClassGen) genCMethodDecl(prefix, jniName string, f *java.Func) { |
| 504 |
if f.Ret != nil { |
| 505 |
g.Printf("ret_%s", f.Ret.CType()) |
| 506 |
} else { |
| 507 |
// Return only the exception, if any |
| 508 |
g.Printf("jint") |
| 509 |
} |
| 510 |
g.Printf(" %s_%s_%s(jint this", prefix, jniName, f.JNIName) |
| 511 |
for i, a := range f.Params { |
| 512 |
g.Printf(", %s a%d", a.CType(), i) |
| 513 |
} |
| 514 |
g.Printf(")") |
| 515 |
} |
| 516 |
|
| 517 |
func (g *ClassGen) genCFuncDecl(jniName string, f *java.Func) { |
| 518 |
if f.Ret != nil { |
| 519 |
g.Printf("ret_%s", f.Ret.CType()) |
| 520 |
} else { |
| 521 |
// Return only the exception, if any |
| 522 |
g.Printf("jint") |
| 523 |
} |
| 524 |
g.Printf(" cproxy_s_%s_%s(", jniName, f.JNIName) |
| 525 |
for i, a := range f.Params { |
| 526 |
if i > 0 { |
| 527 |
g.Printf(", ") |
| 528 |
} |
| 529 |
g.Printf("%s a%d", a.CType(), i) |
| 530 |
} |
| 531 |
g.Printf(")") |
| 532 |
} |
| 533 |
|
| 534 |
func (g *ClassGen) genGo(cls *java.Class) { |
| 535 |
g.Printf("var class_%s C.jclass\n\n", cls.JNIName) |
| 536 |
g.Printf("func init_%s() {\n", cls.JNIName) |
| 537 |
g.Indent() |
| 538 |
g.Printf("cls := C.CString(%q)\n", strings.Replace(cls.FindName, ".", "/", -1)) |
| 539 |
g.Printf("clazz := C.go_seq_find_class(cls)\n") |
| 540 |
g.Printf("C.free(unsafe.Pointer(cls))\n") |
| 541 |
// Before Go 1.11 clazz was a pointer value, an uintptr after. |
| 542 |
g.Printf("if uintptr(clazz) == 0 {\n") |
| 543 |
g.Printf(" return\n") |
| 544 |
g.Printf("}\n") |
| 545 |
g.Printf("class_%s = clazz\n", cls.JNIName) |
| 546 |
for _, fs := range cls.Funcs { |
| 547 |
var supported bool |
| 548 |
for _, f := range fs.Funcs { |
| 549 |
if f.Public && g.isFuncSupported(f) { |
| 550 |
supported = true |
| 551 |
break |
| 552 |
} |
| 553 |
} |
| 554 |
if !supported { |
| 555 |
continue |
| 556 |
} |
| 557 |
g.Printf("%s.%s = func", cls.PkgName, fs.GoName) |
| 558 |
g.genFuncDecl(false, fs) |
| 559 |
g.genFuncBody(cls, fs, "cproxy_s", true) |
| 560 |
} |
| 561 |
g.Printf("%s.Cast = func(v interface{}) Java.%s {\n", cls.PkgName, goClsName(cls.Name)) |
| 562 |
g.Indent() |
| 563 |
g.Printf("t := reflect.TypeOf((*proxy_class_%s)(nil))\n", cls.JNIName) |
| 564 |
g.Printf("cv := reflect.ValueOf(v).Convert(t).Interface().(*proxy_class_%s)\n", cls.JNIName) |
| 565 |
g.Printf("ref := C.jint(_seq.ToRefNum(cv))\n") |
| 566 |
g.Printf("if C.go_seq_isinstanceof(ref, class_%s) != 1 {\n", cls.JNIName) |
| 567 |
g.Printf(" panic(fmt.Errorf(\"%%T is not an instance of %%s\", v, %q))\n", cls.Name) |
| 568 |
g.Printf("}\n") |
| 569 |
g.Printf("return cv\n") |
| 570 |
g.Outdent() |
| 571 |
g.Printf("}\n") |
| 572 |
g.Outdent() |
| 573 |
g.Printf("}\n\n") |
| 574 |
g.Printf("type proxy_class_%s _seq.Ref\n\n", cls.JNIName) |
| 575 |
g.Printf("func (p *proxy_class_%s) Bind_proxy_refnum__() int32 {\n", cls.JNIName) |
| 576 |
g.Indent() |
| 577 |
g.Printf("return (*_seq.Ref)(p).Bind_IncNum()\n") |
| 578 |
g.Outdent() |
| 579 |
g.Printf("}\n\n") |
| 580 |
for _, fs := range cls.AllMethods { |
| 581 |
if !g.isFuncSetSupported(fs) { |
| 582 |
continue |
| 583 |
} |
| 584 |
g.Printf("func (p *proxy_class_%s) %s", cls.JNIName, fs.GoName) |
| 585 |
g.genFuncDecl(false, fs) |
| 586 |
g.genFuncBody(cls, fs, "cproxy", false) |
| 587 |
} |
| 588 |
if cls.Throwable { |
| 589 |
g.Printf("func (p *proxy_class_%s) Error() string {\n", cls.JNIName) |
| 590 |
g.Printf(" return p.ToString()\n") |
| 591 |
g.Printf("}\n") |
| 592 |
} |
| 593 |
if goName, ok := g.goClsMap[cls.Name]; ok { |
| 594 |
g.Printf("func (p *proxy_class_%s) Super() Java.%s {\n", cls.JNIName, goClsName(cls.Name)) |
| 595 |
g.Printf(" return &super_%s{p}\n", cls.JNIName) |
| 596 |
g.Printf("}\n\n") |
| 597 |
g.Printf("type super_%s struct {*proxy_class_%[1]s}\n\n", cls.JNIName) |
| 598 |
g.Printf("func (p *proxy_class_%s) Unwrap() interface{} {\n", cls.JNIName) |
| 599 |
g.Indent() |
| 600 |
g.Printf("goRefnum := C.go_seq_unwrap(C.jint(p.Bind_proxy_refnum__()))\n") |
| 601 |
g.Printf("return _seq.FromRefNum(int32(goRefnum)).Get().(*%s)\n", goName) |
| 602 |
g.Outdent() |
| 603 |
g.Printf("}\n\n") |
| 604 |
for _, fs := range cls.AllMethods { |
| 605 |
if !g.isFuncSetSupported(fs) { |
| 606 |
continue |
| 607 |
} |
| 608 |
g.Printf("func (p *super_%s) %s", cls.JNIName, fs.GoName) |
| 609 |
g.genFuncDecl(false, fs) |
| 610 |
g.genFuncBody(cls, fs, "csuper", false) |
| 611 |
} |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
// genFuncBody generated a Go function body for a FuncSet. It resolves overloading dynamically, |
| 616 |
// by inspecting the number of arguments (if the FuncSet contains varying parameter counts), |
| 617 |
// and their types. |
| 618 |
func (g *ClassGen) genFuncBody(cls *java.Class, fs *java.FuncSet, prefix string, static bool) { |
| 619 |
maxp := len(fs.Funcs[0].Params) |
| 620 |
minp := maxp |
| 621 |
// sort the function variants into argument sizes. |
| 622 |
buckets := make(map[int][]*java.Func) |
| 623 |
numF := 0 |
| 624 |
for _, f := range fs.Funcs { |
| 625 |
if !g.isFuncSupported(f) { |
| 626 |
continue |
| 627 |
} |
| 628 |
numF++ |
| 629 |
n := len(f.Params) |
| 630 |
if n < minp { |
| 631 |
minp = n |
| 632 |
} else if n > maxp { |
| 633 |
maxp = n |
| 634 |
} |
| 635 |
buckets[n] = append(buckets[n], f) |
| 636 |
} |
| 637 |
g.Printf(" {\n") |
| 638 |
g.Indent() |
| 639 |
if len(buckets) != 1 { |
| 640 |
// Switch over the number of arguments. |
| 641 |
g.Printf("switch %d + len(a%d) {\n", minp, minp) |
| 642 |
} |
| 643 |
for i := minp; i <= maxp; i++ { |
| 644 |
funcs := buckets[i] |
| 645 |
if len(funcs) == 0 { |
| 646 |
continue |
| 647 |
} |
| 648 |
if len(buckets) != 1 { |
| 649 |
g.Printf("case %d:\n", i) |
| 650 |
g.Indent() |
| 651 |
} |
| 652 |
for _, f := range funcs { |
| 653 |
if len(funcs) > 1 { |
| 654 |
g.Printf("{\n") |
| 655 |
g.Indent() |
| 656 |
} |
| 657 |
var argNames []string |
| 658 |
var preds []string |
| 659 |
for i, a := range f.Params { |
| 660 |
var ct *java.Type |
| 661 |
var argName string |
| 662 |
if i >= minp { |
| 663 |
argName = fmt.Sprintf("a%d[%d]", minp, i-minp) |
| 664 |
ct = fs.Params[minp] |
| 665 |
} else { |
| 666 |
argName = fmt.Sprintf("a%d", i) |
| 667 |
ct = fs.Params[i] |
| 668 |
} |
| 669 |
if !reflect.DeepEqual(ct, a) { |
| 670 |
g.Printf("_a%d, ok%d := %s.(%s)\n", i, i, argName, g.goType(a, false)) |
| 671 |
argName = fmt.Sprintf("_a%d", i) |
| 672 |
preds = append(preds, fmt.Sprintf("ok%d", i)) |
| 673 |
} |
| 674 |
argNames = append(argNames, argName) |
| 675 |
} |
| 676 |
if len(preds) > 0 { |
| 677 |
g.Printf("if %s {\n", strings.Join(preds, " && ")) |
| 678 |
g.Indent() |
| 679 |
} |
| 680 |
for i, a := range f.Params { |
| 681 |
g.genWrite(fmt.Sprintf("__a%d", i), argNames[i], a, modeTransient) |
| 682 |
} |
| 683 |
g.Printf("res := C.%s_%s_%s(", prefix, cls.JNIName, f.JNIName) |
| 684 |
if !static { |
| 685 |
g.Printf("C.jint(p.Bind_proxy_refnum__())") |
| 686 |
} |
| 687 |
for i := range f.Params { |
| 688 |
if !static || i > 0 { |
| 689 |
g.Printf(", ") |
| 690 |
} |
| 691 |
g.Printf("__a%d", i) |
| 692 |
} |
| 693 |
g.Printf(")\n") |
| 694 |
g.genFuncRet(fs, f, numF > 1) |
| 695 |
if len(preds) > 0 { |
| 696 |
g.Outdent() |
| 697 |
g.Printf("}\n") |
| 698 |
} |
| 699 |
if len(funcs) > 1 { |
| 700 |
g.Outdent() |
| 701 |
g.Printf("}\n") |
| 702 |
} |
| 703 |
} |
| 704 |
if len(buckets) != 1 { |
| 705 |
g.Outdent() |
| 706 |
} |
| 707 |
} |
| 708 |
if len(buckets) != 1 { |
| 709 |
g.Printf("}\n") |
| 710 |
} |
| 711 |
if numF > 1 { |
| 712 |
g.Printf("panic(\"no overloaded method found for %s.%s that matched the arguments\")\n", cls.Name, fs.Name) |
| 713 |
} |
| 714 |
g.Outdent() |
| 715 |
g.Printf("}\n\n") |
| 716 |
} |
| 717 |
|
| 718 |
func (g *ClassGen) genFuncRet(fs *java.FuncSet, f *java.Func, mustReturn bool) { |
| 719 |
if f.Ret != nil { |
| 720 |
g.genRead("_res", "res.res", f.Ret, modeRetained) |
| 721 |
g.genRefRead("_exc", "res.exc", "error", "proxy_error", true) |
| 722 |
} else { |
| 723 |
g.genRefRead("_exc", "res", "error", "proxy_error", true) |
| 724 |
} |
| 725 |
if !fs.Throws { |
| 726 |
g.Printf("if (_exc != nil) { panic(_exc) }\n") |
| 727 |
if fs.HasRet { |
| 728 |
if f.Ret != nil { |
| 729 |
g.Printf("return _res\n") |
| 730 |
} else { |
| 731 |
// The variant doesn't return a value, but the common |
| 732 |
// signature does. Use nil as a placeholder return value. |
| 733 |
g.Printf("return nil\n") |
| 734 |
} |
| 735 |
} else if mustReturn { |
| 736 |
// If there are overloaded variants, return here to avoid the fallback |
| 737 |
// panic generated in genFuncBody. |
| 738 |
g.Printf("return\n") |
| 739 |
} |
| 740 |
} else { |
| 741 |
if fs.HasRet { |
| 742 |
if f.Ret != nil { |
| 743 |
g.Printf("return _res, _exc\n") |
| 744 |
} else { |
| 745 |
// As above, use a nil placeholder return value. |
| 746 |
g.Printf("return nil, _exc\n") |
| 747 |
} |
| 748 |
} else { |
| 749 |
g.Printf("return _exc\n") |
| 750 |
} |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
func (g *ClassGen) genRead(to, from string, t *java.Type, mode varMode) { |
| 755 |
switch t.Kind { |
| 756 |
case java.Int, java.Short, java.Char, java.Byte, java.Long, java.Float, java.Double: |
| 757 |
g.Printf("%s := %s(%s)\n", to, g.goType(t, false), from) |
| 758 |
case java.Boolean: |
| 759 |
g.Printf("%s := %s != C.JNI_FALSE\n", to, from) |
| 760 |
case java.String: |
| 761 |
g.Printf("%s := decodeString(%s)\n", to, from) |
| 762 |
case java.Array: |
| 763 |
if t.Elem.Kind != java.Byte { |
| 764 |
panic("unsupported array type") |
| 765 |
} |
| 766 |
g.Printf("%s := toSlice(%s, %v)\n", to, from, mode == modeRetained) |
| 767 |
case java.Object: |
| 768 |
_, hasProxy := g.imported[t.Class] |
| 769 |
g.genRefRead(to, from, g.goType(t, false), "proxy_class_"+flattenName(t.Class), hasProxy) |
| 770 |
default: |
| 771 |
panic("invalid kind") |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
func (g *ClassGen) genRefRead(to, from string, intfName, proxyName string, hasProxy bool) { |
| 776 |
g.Printf("var %s %s\n", to, intfName) |
| 777 |
g.Printf("%s_ref := _seq.FromRefNum(int32(%s))\n", to, from) |
| 778 |
g.Printf("if %s_ref != nil {\n", to) |
| 779 |
g.Printf(" if %s < 0 { // go object\n", from) |
| 780 |
g.Printf(" %s = %s_ref.Get().(%s)\n", to, to, intfName) |
| 781 |
g.Printf(" } else { // foreign object\n") |
| 782 |
if hasProxy { |
| 783 |
g.Printf(" %s = (*%s)(%s_ref)\n", to, proxyName, to) |
| 784 |
} else { |
| 785 |
g.Printf(" %s = %s_ref\n", to, to) |
| 786 |
} |
| 787 |
g.Printf(" }\n") |
| 788 |
g.Printf("}\n") |
| 789 |
} |
| 790 |
|
| 791 |
func (g *ClassGen) genWrite(dst, v string, t *java.Type, mode varMode) { |
| 792 |
switch t.Kind { |
| 793 |
case java.Int, java.Short, java.Char, java.Byte, java.Long, java.Float, java.Double: |
| 794 |
g.Printf("%s := C.%s(%s)\n", dst, t.CType(), v) |
| 795 |
case java.Boolean: |
| 796 |
g.Printf("%s := C.jboolean(C.JNI_FALSE)\n", dst) |
| 797 |
g.Printf("if %s {\n", v) |
| 798 |
g.Printf(" %s = C.jboolean(C.JNI_TRUE)\n", dst) |
| 799 |
g.Printf("}\n") |
| 800 |
case java.String: |
| 801 |
g.Printf("%s := encodeString(%s)\n", dst, v) |
| 802 |
case java.Array: |
| 803 |
if t.Elem.Kind != java.Byte { |
| 804 |
panic("unsupported array type") |
| 805 |
} |
| 806 |
g.Printf("%s := fromSlice(%s, %v)\n", dst, v, mode == modeRetained) |
| 807 |
case java.Object: |
| 808 |
g.Printf("var %s C.jint = _seq.NullRefNum\n", dst) |
| 809 |
g.Printf("if %s != nil {\n", v) |
| 810 |
g.Printf(" %s = C.jint(_seq.ToRefNum(%s))\n", dst, v) |
| 811 |
g.Printf("}\n") |
| 812 |
default: |
| 813 |
panic("invalid kind") |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
// genCRetClear clears the result value from a JNI call if an exception was |
| 818 |
// raised. |
| 819 |
func (g *ClassGen) genCRetClear(v string, t *java.Type, exc string) { |
| 820 |
g.Printf("if (%s != NULL) {\n", exc) |
| 821 |
g.Indent() |
| 822 |
switch t.Kind { |
| 823 |
case java.Int, java.Short, java.Char, java.Byte, java.Long, java.Float, java.Double, java.Boolean: |
| 824 |
g.Printf("%s = 0;\n", v) |
| 825 |
default: |
| 826 |
// Assume a nullable type. It will break if we missed a type. |
| 827 |
g.Printf("%s = NULL;\n", v) |
| 828 |
} |
| 829 |
g.Outdent() |
| 830 |
g.Printf("}\n") |
| 831 |
} |
| 832 |
|
| 833 |
func (g *ClassGen) genJavaToC(v string, t *java.Type) { |
| 834 |
switch t.Kind { |
| 835 |
case java.Int, java.Short, java.Char, java.Byte, java.Long, java.Float, java.Double, java.Boolean: |
| 836 |
g.Printf("%s _%s = %s;\n", t.JNIType(), v, v) |
| 837 |
case java.String: |
| 838 |
g.Printf("nstring _%s = go_seq_from_java_string(env, %s);\n", v, v) |
| 839 |
case java.Array: |
| 840 |
if t.Elem.Kind != java.Byte { |
| 841 |
panic("unsupported array type") |
| 842 |
} |
| 843 |
g.Printf("nbyteslice _%s = go_seq_from_java_bytearray(env, %s, 1);\n", v, v) |
| 844 |
case java.Object: |
| 845 |
g.Printf("jint _%s = go_seq_to_refnum(env, %s);\n", v, v) |
| 846 |
default: |
| 847 |
panic("invalid kind") |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
func (g *ClassGen) genCToJava(v string, t *java.Type) { |
| 852 |
switch t.Kind { |
| 853 |
case java.Int, java.Short, java.Char, java.Byte, java.Long, java.Float, java.Double, java.Boolean: |
| 854 |
g.Printf("%s _%s = %s;\n", t.JNIType(), v, v) |
| 855 |
case java.String: |
| 856 |
g.Printf("jstring _%s = go_seq_to_java_string(env, %s);\n", v, v) |
| 857 |
case java.Array: |
| 858 |
if t.Elem.Kind != java.Byte { |
| 859 |
panic("unsupported array type") |
| 860 |
} |
| 861 |
g.Printf("jbyteArray _%s = go_seq_to_java_bytearray(env, %s, 0);\n", v, v) |
| 862 |
case java.Object: |
| 863 |
g.Printf("jobject _%s = go_seq_from_refnum(env, %s, NULL, NULL);\n", v, v) |
| 864 |
default: |
| 865 |
panic("invalid kind") |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
func goClsName(n string) string { |
| 870 |
return initialUpper(strings.Replace(n, ".", "_", -1)) |
| 871 |
} |
| 872 |
|
| 873 |
func (g *ClassGen) genInterface(cls *java.Class) { |
| 874 |
g.Printf("type %s interface {\n", goClsName(cls.Name)) |
| 875 |
g.Indent() |
| 876 |
// Methods |
| 877 |
for _, fs := range cls.AllMethods { |
| 878 |
if !g.isFuncSetSupported(fs) { |
| 879 |
continue |
| 880 |
} |
| 881 |
g.Printf("%s", fs.GoName) |
| 882 |
g.genFuncDecl(true, fs) |
| 883 |
g.Printf("\n") |
| 884 |
} |
| 885 |
if goName, ok := g.goClsMap[cls.Name]; ok { |
| 886 |
g.Printf("Super() %s\n", goClsName(cls.Name)) |
| 887 |
g.Printf("// Unwrap returns the Go object this Java instance\n") |
| 888 |
g.Printf("// is wrapping.\n") |
| 889 |
g.Printf("// The return value is a %s, but the delclared type is\n", goName) |
| 890 |
g.Printf("// interface{} to avoid import cycles.\n") |
| 891 |
g.Printf("Unwrap() interface{}\n") |
| 892 |
} |
| 893 |
if cls.Throwable { |
| 894 |
g.Printf("Error() string\n") |
| 895 |
} |
| 896 |
g.Outdent() |
| 897 |
g.Printf("}\n\n") |
| 898 |
} |
| 899 |
|
| 900 |
// Flatten java class names. "java.package.Class$Inner" is converted to |
| 901 |
// "java_package_Class_Inner" |
| 902 |
func flattenName(n string) string { |
| 903 |
return strings.Replace(strings.Replace(n, ".", "_", -1), "$", "_", -1) |
| 904 |
} |
| 905 |
|
| 906 |
const ( |
| 907 |
classesPkgHeader = gobindPreamble + ` |
| 908 |
package Java |
| 909 |
|
| 910 |
// Used to silence this package not used errors |
| 911 |
const Dummy = 0 |
| 912 |
|
| 913 |
` |
| 914 |
classesCHeader = gobindPreamble + ` |
| 915 |
#include <jni.h> |
| 916 |
#include "seq.h" |
| 917 |
#include "classes.h" |
| 918 |
|
| 919 |
` |
| 920 |
classesHHeader = gobindPreamble + ` |
| 921 |
#include <jni.h> |
| 922 |
#include "seq.h" |
| 923 |
|
| 924 |
extern void init_proxies(); |
| 925 |
|
| 926 |
` |
| 927 |
|
| 928 |
javaImplHeader = gobindPreamble |
| 929 |
|
| 930 |
classesGoHeader = gobindPreamble + ` |
| 931 |
package main |
| 932 |
|
| 933 |
/* |
| 934 |
#include <stdlib.h> // for free() |
| 935 |
#include <jni.h> |
| 936 |
#include "seq.h" |
| 937 |
#include "classes.h" |
| 938 |
*/ |
| 939 |
import "C" |
| 940 |
|
| 941 |
import ( |
| 942 |
"Java" |
| 943 |
_seq "golang.org/x/mobile/bind/seq" |
| 944 |
) |
| 945 |
|
| 946 |
` |
| 947 |
) |