Examples / Tutorials for using javax.lang.model or ANTLR JavaParser to get Java source code information

I would like to create an automatic visualization similar to Flowchart for simple Java logic, for this I need to analyze the Java source code, I have 2 candidates, ANTLR and javax.lang.model from Java 6. Also not easy.

I have yet to find one working example that will even be remotely close to what I want to achieve.

I want to find simple variable declarations, destinations and streams (if, for, switch, boolean conditions, etc.)

Is there a simple example or tutorial for any of them? I found very few ANTLR examples (none of them work out of the box without significant โ€œhomeworkโ€), and absolutely nothing for javax.lang.model

+4
source share
3 answers

I would try this with the Eclipse Xtext, it uses ANTLR under the hood, it is also (relative) easy to use along with Eclipse GMF for rendering.

+1
source

Is there a specific reason why you want to create a parser yourself? IMO, it would be easier to let an existing parser create an AST for you, which you just go through to collect your data about the source file (s). Using your favorite search engine with the keywords "get ast java source" will result in many relevant queries.

At first glance, they look like suitable candidates:

Of course, you can use ANTLR, there are many ANTLR grammars for Java available to you, but when choosing this option, a learning curve tool (or some other parser generator, for that matter) is enough. If you choose ANTLR, I am more than happy to answer any questions regarding this.

Good luck

+2
source

I suggest you switch from Java Soot to the Java optimization platform . This will help you parse the source of java, can generate CFG and various available options.

You can also find the eclipse plugin here

+1
source

Source: https://habr.com/ru/post/1344529/


All Articles