AST generation with Sonar Toolkit

What I want to do:
AST generation with Sonar for a java class.

Why?
I want to see how an AST looks like when its generated through sonar for learning purposes.

What I did so far
I created a project where I can configure and use the sonar toolkit. And hopefully, somehow use that to generate an AST. However, I cannot find the ConfigurationModel for java anywhere.
Does anyone have any tips to help me further? Perhaps where I can look or what I can do to achieve my goal?

What I want to do:
Abstract syntax tree (AST) generation with Sonar for a java class.

Why?
I want to see how an AST looks like when its generated through sonar. I’m writing a blogpost on how Sonar works.

What I did so far
I created a project where I can configure and use the sonar toolkit. And hopefully, somehow use that to generate an AST. However, I cannot find the ConfigurationModel for java anywhere.
Why is this code not public?
Does anyone have any tips to help me further?

1 Like

Hi Martijn Jan Jaap,

I don’t think you can still use the sonar sslr toolkit for the java language.
The Sonar java analyzer used the sslr library in the past to parse java source code,
but now, it uses the eclipse compiler for Java.

If you are interested in a text representation of the Java analyzer AST, I created this small project that does exactly this: github.com/alban-auzeill/print-java-ast

Good luck,

Alban Auzeill

1 Like

Thank you very much Alban!

Do you know if it is somewhere documented that the sonar java analyzer is using the eclipse compiler? Or is that something I should be able to find in the code of the sonar java analyzer?

The usage of eclipse compiler is not documented.
Our java parser is the module java-frontend, you can look for import org.eclipse.jdt. to see the usage of the eclipse compiler classes. It depends on the org.eclipse.jdt:org.eclipse.jdt.core:3.30.0 dependency.
But the java-frontend public API is not bound to the eclipse compiler, it’s just an underlying implementation detail.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.