Esempio n. 1
0
func (g *ClassGen) genInterface(cls *java.Class) {
	g.Printf("type %s interface {\n", goClsName(cls.Name))
	g.Indent()
	// Methods
	for _, f := range cls.AllMethods {
		if !g.isFuncSupported(f) {
			continue
		}
		g.Printf(f.GoName)
		g.genFuncDecl(true, f)
		g.Printf("\n")
	}
	if cls.HasSuper() {
		g.Printf("Super() %s\n", goClsName(cls.Name))
	}
	if cls.Throwable {
		g.Printf("Error() string\n")
	}
	g.Outdent()
	g.Printf("}\n\n")
}
Esempio n. 2
0
func (g *ClassGen) genGo(cls *java.Class) {
	g.Printf("var class_%s C.jclass\n\n", cls.JNIName)
	g.Printf("func init_%s() {\n", cls.JNIName)
	g.Indent()
	g.Printf("cls := C.CString(%q)\n", strings.Replace(cls.FindName, ".", "/", -1))
	g.Printf("clazz := C.go_seq_find_class(cls)\n")
	g.Printf("C.free(unsafe.Pointer(cls))\n")
	g.Printf("if clazz == nil {\n")
	g.Printf("	return\n")
	g.Printf("}\n")
	g.Printf("class_%s = clazz\n", cls.JNIName)
	for _, f := range cls.Funcs {
		if !f.Public || !g.isFuncSupported(f) {
			continue
		}
		g.Printf("{\n")
		g.Indent()
		name := f.Name
		if f.Constructor {
			name = "<init>"
		}
		g.Printf("fn := C.CString(%q)\n", name)
		g.Printf("fd := C.CString(%q)\n", f.Desc)
		if f.Constructor {
			g.Printf("m := C.go_seq_get_method_id(clazz, fn, fd)\n")
		} else {
			g.Printf("m := C.go_seq_get_static_method_id(clazz, fn, fd)\n")
		}
		g.Printf("C.free(unsafe.Pointer(fn))\n")
		g.Printf("C.free(unsafe.Pointer(fd))\n")
		g.Printf("if m != nil {\n")
		g.Indent()
		g.Printf("%s.%s = func", cls.PkgName, f.GoName)
		g.genFuncDecl(false, f)
		g.Printf(" {\n")
		g.Indent()
		for i, a := range f.Params {
			g.genWrite(fmt.Sprintf("a%d", i), a, modeTransient)
		}
		g.Printf("res := C.cproxy_s_%s_%s(clazz, m", cls.JNIName, f.JNIName)
		for i := range f.Params {
			g.Printf(", _a%d", i)
		}
		g.Printf(")\n")
		g.genFuncRet(f)
		g.Outdent()
		g.Printf("}\n")
		g.Outdent()
		g.Printf("}\n")
		g.Outdent()
		g.Printf("}\n")
	}
	g.Printf("%s.Cast = func(v interface{}) Java.%s {\n", cls.PkgName, goClsName(cls.Name))
	g.Indent()
	g.Printf("t := reflect.TypeOf((*proxy_class_%s)(nil))\n", cls.JNIName)
	g.Printf("cv := reflect.ValueOf(v).Convert(t).Interface().(*proxy_class_%s)\n", cls.JNIName)
	g.Printf("ref := C.jint(_seq.ToRefNum(cv))\n")
	g.Printf("if C.go_seq_isinstanceof(ref, class_%s) != 1 {\n", cls.JNIName)
	g.Printf("	panic(fmt.Errorf(\"%%T is not an instance of %%s\", v, %q))\n", cls.Name)
	g.Printf("}\n")
	g.Printf("return cv\n")
	g.Outdent()
	g.Printf("}\n")
	g.Outdent()
	g.Printf("}\n\n")
	g.Printf("type proxy_class_%s _seq.Ref\n\n", cls.JNIName)
	g.Printf("func (p *proxy_class_%s) Bind_proxy_refnum__() int32 { return (*_seq.Ref)(p).Bind_IncNum() }\n\n", cls.JNIName)
	for _, f := range cls.AllMethods {
		if !g.isFuncSupported(f) {
			continue
		}
		g.Printf("func (p *proxy_class_%s) %s", cls.JNIName, f.GoName)
		g.genFuncDecl(false, f)
		g.genFuncBody(cls, f, "cproxy")
	}
	if cls.Throwable {
		g.Printf("func (p *proxy_class_%s) Error() string {\n", cls.JNIName)
		g.Printf("	return p.ToString()\n")
		g.Printf("}\n")
	}
	if cls.HasSuper() {
		g.Printf("func (p *proxy_class_%s) Super() Java.%s {\n", cls.JNIName, goClsName(cls.Name))
		g.Printf("	return &super_%s{p}\n", cls.JNIName)
		g.Printf("}\n\n")
		g.Printf("type super_%s struct {*proxy_class_%[1]s}\n\n", cls.JNIName)
		for _, f := range cls.AllMethods {
			if !g.isFuncSupported(f) {
				continue
			}
			g.Printf("func (p *super_%s) %s", cls.JNIName, f.GoName)
			g.genFuncDecl(false, f)
			g.genFuncBody(cls, f, "csuper")
		}
	}
}