As the title says, I want to parse some Java source code in Java. I'm pretty sure there are other java libraries that already perform this, but I couldn't find any.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Janino can parse, compile and execute Java code.
回答2:
Antlr has a grammar file for Java. See this.
回答3:
You may be looking for something like ANTLR: http://www.antlr.org/
回答4:
Eclipse exposes the Syntax Tree of it's own Java compiler. You can simply access the elements.
See here.
回答5:
JavaCC has a Java 1.5 grammar including generics.
the tokens are probably more fine-grained then what I want
Your first requirement is to get an accurate parse, and you will realistically only get that from an existing parser. What you do with the result is up to you.