Search code examples
jsweet

Handling Java annotations in JSweet


I'm attempting to transpile Java model classes to JavaScript using JSweet. The model classes contain JPA annotations like @Column. The transpilation fails as soon as it encounters import javax.persistence.Column. The JPA annotations are irrelevant in JavaScript and should not be transpiled. Can this be done without changing the Java code?

More generally, is there a way to have JSweet ignore import statements, e.g., when all references to the imported packages are in @Erased methods?


Solution

  • Normally, JSweet just erases unknown annotations, so your code should transpile fine.

    First thing to check: do you have a JPA jar in your classpath or in your Maven dependencies? JSweet uses javac, which requires all types to be in the classpath. I guess that the @Column annotation should be in there: https://mvnrepository.com/artifact/javax.persistence/persistence-api/1.0.2

    As for the second part of your question, JSweet v2 provides an API to tune the generation of the code. See the specs. In the PrinterAdapter API, you can override the needsImport method to return null when the import is not needed. However I believe that you don't need this for your case since annotations are erased automatically.