Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler as a service in Java

Microsoft Roslyn - compiler as a service is a nice addition to the .NET stack; I was looking for something similar in the Java world. I believe Scala has something similar in the form of compiler plugins but not sure how flexible it is.

The problem I am trying to solve in the Java world is allowing users to write some custom syntax and internally it would get re-wired into a different syntax.

like image 219
GammaVega Avatar asked Sep 09 '12 12:09

GammaVega


2 Answers

I've never used Microsoft Roslyn, so I'm not sure about what it provides. But if what you want is to be able to compile classes at runtime, you should look at the JavaCompiler API which has been a part of standard Java since Java SE 6. If that doesn't suit your needs you should probably take a look at Eclipse's ASTParser.

like image 200
Aleksander Blomskøld Avatar answered Sep 30 '22 07:09

Aleksander Blomskøld


Scala will have support for macros in the next release (2.10). You can already play with the milestone releases to check how it works. Basically, they allow to modify AST at compile time. So you can rewrite any piece of Scala in another piece of scala. Check the scalamacros website for examples and doc.

If you want to compile Scala source at runtime, you can look for Eval in the twitter/util project.

like image 45
paradigmatic Avatar answered Sep 30 '22 09:09

paradigmatic