-
-
Notifications
You must be signed in to change notification settings - Fork 1
Tech Detail Design
There is a Node.js runtime embedded in Jaspiler. It hosts multiple Babel flavored transform plugins that transform the Java code at AST level. Thanks to Javet (an awesome way of embedding Node.js and V8 in Java), Jaspiler is able to run JavaScript code easily.
With the help of Javet, Jaspiler injects a global namespace jaspiler
that enables the transform, AST manipulation, etc.
- Back-end: Jaspiler running in the JVM is called back-end as it serves the read and write requests to the AST from the front-end.
- Front-end: Scripts running in the Node.js are called front-end.
Though Jaspiler API is open if the applications prefer directly referencing the Jaspiler binary, it's quite inconvienent to write plugins in Java because Java applications have to go through the compilation, dependency resolution, runtime version mismatch, ...etc. That seriously slows down development.
Babel provides a much better experience in terms of transforming JavaScript code. So, why not borrow the similar experience in manipulating the Java AST in JavaScript? With Jaspiler, it is from write - compile - deploy - run
to write - run
with much better productivity and flexibility. Also, with the rich Node.js ecosystem, the JavaScript is able to access file system, network, basically not only do whatever a normal Node.js application can do, but also access the Java compiler exposed by Jaspiler.
Jaspiler has its own implementation on the official JavaCompiler
API built-in with JDK. So, the AST is the same as what javac
sees. Yes, that's 100% compatible. This design is at top of the line compared to other third-party implementations.
The plugins are written in a JavaScript file loaded by the Node.js runtime embedded in Jaspiler. The plugin API is very much similar to the the Babel plugin API.
A plugin is able to:
- Traverse the downstream and upstream AST.
- Make changes to the AST.
- Get the internal Java compiler interface detail.
Plugin interface is defined in jaspiler/index.d.ts.
Most of the Jaspiler features are available in both Node.js and JVM.
- If you are a JavaScript developer, please go with the Node.js solution.
- If you are a Java developer, please go with the JVM solution.
Objective | Feature | Node.js | JVM |
---|---|---|---|
Get AST | Transform API | ✔️ | ✔️ |
Transform via visitor | Transform Plugins | ✔️ | ✔️ |
Traverse AST | Transform Plugin Visitor | ✔️ | ✔️ |
Transform via AST | AST | ✔️ | ✔️ |
Declarative transformation | Annotation | ✔️ | |
Imperative transformation | AST | ✔️ | ✔️ |
The Node.js solution is recommended because it outweighs JVM at:
- Compatibility
- Flexibility
- Productivity
The JVM solution has to directly reference the Jaspiler jar file and be compiled so that it's quite hard to change the transformation algorithm on the fly.