Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
How to generate JAR file dynamically
compiling Java code in Kotlin
Mitsunori Komatsu
About me
• Mitsunori Komatsu, 

Software engineer at Treasure Data
• github:komamitsu

msgpack-java

(jackson-dataformat-m...
Why did I try it?
• First, I developed just a tiny Lisp
interpreter in Kotlin for exercise
• But basically, interpreters a...
Where can we see the
source code?
https://github.com/komamitsu/tiny-
kotlin-lisp
How did I do that?
• Convert Lisp AST to Java source code
traversing the tree using Visitor pattern
• Compile the Java sou...
Convert Lisp AST to Java source code
traversing the tree using Visitor pattern
@CompileVisitor.kt
Nothing special…
Convert Lisp AST to Java source code
traversing the tree using Visitor pattern
@CompileVisitor.kt
Nothing special…
http://janino-compiler.github.io/janino/
With Janino!!
Janino can not only compile a set of source files
to a set of class ...
Compile the Java source code
and Extract the Java byte code
• It’s really easy to dynamically compile
Java source code and...
Compile the Java source code
and Extract the Java byte code
Compile
Extract
Access a hidden field
ByteArrayClassLoader.clas...
Create a JAR file from the byte
code and dependent classes
Prepare dependent class files
@CompileVisitor.kt
Create the compi...
Create a JAR file from the byte
code and dependent classes
@CompileVisitor.kt
Setup a manifest
Create a JAR file reading cla...
Pitfalls
@CompileVisitor.kt
• When writing Kotlin code, it easily has dependencies
with internal Kotlin classes (e.g
kotli...
Enjoy programming!
Upcoming SlideShare
Loading in …5
×

of

How to generate JAR file dynamically compiling Java code in Kotlin Slide 1 How to generate JAR file dynamically compiling Java code in Kotlin Slide 2 How to generate JAR file dynamically compiling Java code in Kotlin Slide 3 How to generate JAR file dynamically compiling Java code in Kotlin Slide 4 How to generate JAR file dynamically compiling Java code in Kotlin Slide 5 How to generate JAR file dynamically compiling Java code in Kotlin Slide 6 How to generate JAR file dynamically compiling Java code in Kotlin Slide 7 How to generate JAR file dynamically compiling Java code in Kotlin Slide 8 How to generate JAR file dynamically compiling Java code in Kotlin Slide 9 How to generate JAR file dynamically compiling Java code in Kotlin Slide 10 How to generate JAR file dynamically compiling Java code in Kotlin Slide 11 How to generate JAR file dynamically compiling Java code in Kotlin Slide 12 How to generate JAR file dynamically compiling Java code in Kotlin Slide 13 How to generate JAR file dynamically compiling Java code in Kotlin Slide 14
Upcoming SlideShare
What to Upload to SlideShare
Next

1 Like

Share

How to generate JAR file dynamically compiling Java code in Kotlin

How to generate JAR file dynamically compiling Java code in Kotlin

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

How to generate JAR file dynamically compiling Java code in Kotlin

  1. 1. How to generate JAR file dynamically compiling Java code in Kotlin Mitsunori Komatsu
  2. 2. About me • Mitsunori Komatsu, 
 Software engineer at Treasure Data • github:komamitsu
 msgpack-java
 (jackson-dataformat-msgpack),
 fluency (yet another fluent logger java)
 treasuredata_fdw (with td-client-rust)
  3. 3. Why did I try it? • First, I developed just a tiny Lisp interpreter in Kotlin for exercise • But basically, interpreters aren’t so fast. So, I tried dynamically compiling Lisp code to Java byte code • Finally, I tried outputting those Java byte code to a JAR file
  4. 4. Where can we see the source code? https://github.com/komamitsu/tiny- kotlin-lisp
  5. 5. How did I do that? • Convert Lisp AST to Java source code traversing the tree using Visitor pattern • Compile the Java source code using http://janino-compiler.github.io/janino/ • Extract the Java byte code with a bit hack • Create a JAR file from the byte code and dependent classes
  6. 6. Convert Lisp AST to Java source code traversing the tree using Visitor pattern @CompileVisitor.kt Nothing special…
  7. 7. Convert Lisp AST to Java source code traversing the tree using Visitor pattern @CompileVisitor.kt Nothing special…
  8. 8. http://janino-compiler.github.io/janino/ With Janino!! Janino can not only compile a set of source files to a set of class files like JAVAC, but also compile a Java expression, block, class body or source file in memory, load the bytecode and execute it directly in the same JVM. Compile the Java source code
  9. 9. Compile the Java source code and Extract the Java byte code • It’s really easy to dynamically compile Java source code and invoke it with Janino • But Janino doesn’t provide any API to get compiled Java byte code…
  10. 10. Compile the Java source code and Extract the Java byte code Compile Extract Access a hidden field ByteArrayClassLoader.classes in Janino using reflection!@CompileVisitor.kt
  11. 11. Create a JAR file from the byte code and dependent classes Prepare dependent class files @CompileVisitor.kt Create the compiled class file Get Java byte code using Class#getResourceAsStream
  12. 12. Create a JAR file from the byte code and dependent classes @CompileVisitor.kt Setup a manifest Create a JAR file reading class files
  13. 13. Pitfalls @CompileVisitor.kt • When writing Kotlin code, it easily has dependencies with internal Kotlin classes (e.g kotlin.KotlinNullPointerException). So the classes are needed to be included in a JAR file to avoid runtime failures.
  14. 14. Enjoy programming!
  • YujiKubota

    Oct. 23, 2017

How to generate JAR file dynamically compiling Java code in Kotlin

Views

Total views

1,589

On Slideshare

0

From embeds

0

Number of embeds

25

Actions

Downloads

0

Shares

0

Comments

0

Likes

1

×