Examples / tutorials for usage of javax.lang.model or ANTLR JavaParser to get information on Java Source Code

Deadly 提交于 2019-12-21 05:25:10

问题


I would like to create an automatic Flowchart-like visualization to simple Java Logic, for this I need to parse Java Source code, I have 2 candidates, ANTLR and javax.lang.model of Java 6. Neither are easy.

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

I want to find simple variable declarations, assignments, and flows (if, for, switch, boolean conditions etc)

Is there a simple example or tutorial for either of these? I found very few ANTLR examples (non of them are working out of the box without significant "homework") and absolutely none for javax.lang.model


回答1:


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




回答2:


Is there a particular reason you want to generate a parser yourself? IMO, it would be easier to let an existing parser create an AST for you which you "simply" traverse in order 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 hits.

At a first glance, these look like suitable candidates:

  • Eclipse JDT API
  • javaparser

You could use ANTLR of course, there are many ANTLR grammars for Java available for you, but there is quite the learning curve when choosing this tool (or some other parser generator, for that matter). If you do choose ANTLR, I'm more than happy to answer any questions regarding it.

Best of luck!




回答3:


I suggest you to go with Java Soot a Java Optimization Framework. Which will help you to parse the java source, can generate the CFG and various options available.

Also you can find eclipse plugin here



来源:https://stackoverflow.com/questions/5373199/examples-tutorials-for-usage-of-javax-lang-model-or-antlr-javaparser-to-get-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!