Java bindings for clang/libclang?

Hello,

I am wondering whether there exist Java bindings for libclang, or some other
way to access the clang API from Java code (other than using JNI directly - I'm
thinking of something like a Java wrapper library, possibly implemented using
JNI)?

My motivation for asking this is that I'd like to use clang in a plugin for a
Java-based IDE and the plugin must be written in Java.

Thanks,
Nate

Nope.

-eric

You can use the Oliver Chafik JNAerator/Bridj project to auto-generate a bindings class
from the clang and llvm headers. The generated classes could use some refactoring and
cleanup, and I'm working on making a clean set of wrappers out of it. But with a little
effort you can get it to work as-is.

Kevin

Hi Nathan,

I am wondering whether there exist Java bindings for libclang, or some other
way to access the clang API from Java code (other than using JNI directly - I'm
thinking of something like a Java wrapper library, possibly implemented using
JNI)?

Another JNA variant (besides Kevin suggestion for semi-automatic generation of complete binding) might be straight-forward translation of current FFI based bindings ($CLANG/bindings/python).

Or, you might test if it will be zippy enough with in-process JIT-ed FFI/IPC instead of classical bindings:

libclang - LiaJIT FFI - msgpack - zeromq-INPROC:// - msgpack - java

http://luajit.org/ext_ffi_tutorial.html#zlib
http://www.zeromq.org/bindings:lua

At least, the FFI part will be faster than JNA or current Python FFI bindings (because Python FFI is not JIT-ed to machine code). The size of LuaJIT.so on x86_64 is about 400K.

This way advantages - a) Like JNA, easier (compared to JNI) following of libclang evolution, but with possible better performance b) potential direct reuse of your libclang as zeromq/msgpack service by other projects and c) practical experience in the build server direction of research.

Kind regards,
Alek

P.S. Also, you can use Python bindings via JPype for the initial sketching (or generating other bindings).

http://jpype.sourceforge.net/

You can also try to write core part of your plugin in C or C++ and then bind it to
Java interface.

> I am wondering whether there exist Java bindings for libclang, or some other
> way to access the clang API from Java code (other than using JNI directly - I'm
> thinking of something like a Java wrapper library, possibly implemented using
> JNI)?

You can use the Oliver Chafik JNAerator/Bridj project to auto-generate a bindings class
from the clang and llvm headers. The generated classes could use some refactoring and
cleanup, and I'm working on making a clean set of wrappers out of it.

Cool! Would you be interested in sharing that once it's finished?

Thanks to everyone else for your suggestions too. I wasn't aware of these easier
alternatives to JNI, I will definitely look into them.

Nate

Yep, I plan to make it available when it's in shape. I mostly like how it's looking,
now, but I've only tested a small fraction of the surface area of the apis --
enough to know that it's going to work, not enough to say that everything's
correct. I want to put together a working example that exercises at least
a reasonable subset; when that's working I'll probably github it.

Kevin