func genPkg(pkg *build.Package) { files := parseFiles(pkg.Dir, pkg.GoFiles) if len(files) == 0 { return // some error has been reported } conf := loader.Config{ Fset: fset, AllowErrors: true, } conf.TypeChecker.IgnoreFuncBodies = true conf.TypeChecker.FakeImportC = true conf.TypeChecker.DisableUnusedImportCheck = true var tcErrs []error conf.TypeChecker.Error = func(err error) { tcErrs = append(tcErrs, err) } conf.CreateFromFiles(pkg.ImportPath, files...) program, err := conf.Load() if err != nil { for _, err := range tcErrs { errorf("%v", err) } errorf("%v", err) return } p := program.Created[0].Pkg fname := defaultFileName(*lang, p) switch *lang { case "java": w, closer := writer(fname, p) processErr(bind.GenJava(w, fset, p)) closer() case "go": w, closer := writer(fname, p) processErr(bind.GenGo(w, fset, p)) closer() case "objc": if fname == "" { processErr(bind.GenObjc(os.Stdout, fset, p, true)) processErr(bind.GenObjc(os.Stdout, fset, p, false)) } else { hname := fname[:len(fname)-2] + ".h" w, closer := writer(hname, p) processErr(bind.GenObjc(w, fset, p, true)) closer() w, closer = writer(fname, p) processErr(bind.GenObjc(w, fset, p, false)) closer() } default: errorf("unknown target language: %q", *lang) } }
func (b *binder) GenJava(outdir string) error { className := strings.Title(b.pkg.Name()) javaFile := filepath.Join(outdir, className+".java") if buildX { printcmd("gobind -lang=java %s > %s", b.pkg.Path(), javaFile) } generate := func(w io.Writer) error { return bind.GenJava(w, b.fset, b.pkg) } if err := writeFile(javaFile, generate); err != nil { return err } return nil }