Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using Go to call Java libraries

Using Go to call Java libraries

Jumpei Takiyasu

October 13, 2017
Tweet

More Decks by Jumpei Takiyasu

Other Decks in Technology

Transcript

  1. This talk What is Go? How to use Java library

    from Go The Go gopher was designed by Renee French. (http://reneefrench.blogspot.com/)
  2. Go • Simple language spec. • Official development tools •

    Statically typed • Compile to native code • Concurrency etc.. Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
  3. Simple Low learning cost to start. package main import "fmt"

    func main() { fmt.Println("hello world") }
  4. Tools / go-fmt End of coding style war • Tab

    vs Space • Curly brackets { do something $ gofmt ./… # or use editor plugin
  5. Tools / go get Easy to publish and use library/tools

    No central repository $ go get github.com/juntaki/gogtags $ gogtags -v
  6. Motivation My team is using Kotlin - Why? Statically typed

    ʕ ◔ϖ◔ʔ < OK “Kotlin” sounds cute (for Japanese) ʕ ◔ϖ◔ʔ < cute Kotlin can be used with our Java libraries ʕ - ϖ - ʔ < mmm...
  7. javago Generate go wrapper code for Java library with jnigo

    $ javago --classfile java.lang.Math $ ls java java.lang.Math.go
  8. Garbage collection, Memory management Java and C things are wrapped

    by Go object. GoObject C (JNI) Java GetObject / malloc GetObject Object reference Pointer to Object ref. NewGlobalRef Go GC runtime.Finalizer DeleteGlobalRef free Java GC
  9. Method overloading func Mathmax(args ...interface{}) (jnigo.JObject, error) { convertedArgs, err

    := jvm.ConvertAll(args) if err != nil { return nil, err } sigArgs := "" for _, arg := range convertedArgs { sigArgs += arg.Signature() } sigMap := map[string]string{"JJ":"(JJ)J", "FF":"(FF)F", "DD":"(DD)D", "II":"(II)I"} return jvm.CallStaticFunction("java/lang/Math", "max", sigMap[sigArgs], convertedArgs) } Simulated by reflection. Switch function signature by argument signatures