使用 javax.lang.model 或 ANTLR JavaParser 获取 Java 源代码信息的示例/教程
我想为简单的 Java 逻辑创建一个自动的类似流程图的可视化,为此我需要解析 Java 源代码,我有 2 个候选者, ANTLR 和 javax.lang.model 。两者都不容易。
我还没有找到一个与我想要实现的目标相距甚远的工作示例。
我想找到简单的变量声明、赋值和流程(if、for、switch、布尔条件等)
是否有一个简单的示例或教程? 我发现很少有 ANTLR 示例(没有一个是在没有大量“作业”的情况下开箱即用的),并且绝对没有针对 javax.lang.model 的示例
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想自己生成解析器有什么特殊原因吗? IMO,让现有解析器为您创建一个 AST 会更容易,您“简单地”遍历它以收集有关源文件的数据。使用您最喜欢的搜索引擎和关键字“get ast java source”将导致许多相关的点击。
乍一看,这些看起来像是合适的候选人:
当然你可以使用ANTLR,有很多Java 的 ANTLR 语法 可供您使用,但是选择此工具(或其他一些工具)时有相当长的学习曲线解析器生成器,就此而言)。如果您选择 ANTLR,我非常乐意回答有关它的任何问题。
祝你好运!
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:
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!
我会尝试使用 Eclipse Xtext,它在底层使用 ANTLR,与 Eclipse GMF 一起使用它来实现可视化也(相对)容易。
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.
我建议您使用 Java Soot 一个 Java 优化框架。这将帮助您解析java源代码,可以生成CFG和各种可用选项。
您还可以在此处找到 Eclipse 插件
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