Java source code parser which supports annotations

不羁岁月 提交于 2020-01-04 10:40:09

问题


I need a Java source code parsing library for Java to programmatically extract method definitions and annotations.

Specifically, given code like :

@WebMethod(operationName = "MyOperation")
public String myOperation(String param1,int param2) { ....
}

I have the following requirements:
1. Extract the name, return type and names and types of the method parameters
2. Extract the annotations associated with the method
3. Finally, create a new source file by removing the annotations

I am currently using JaxMeJS http://ws.apache.org/jaxme/js/jparser.html which satisfies 1. but not 2. or 3.

Could you recommended a parsing library that can fulfill all 3 requirements ?


回答1:


JavaCC comes with a 1.5 parser.




回答2:


Annotation processing is part of the Annotation processing tool in Java which runs as part of the Java compiler. I'm not sure how you can remove the annotations from the source code. This is not supported by APT. (It might be easier to remove the annotations from the compiler byte code than from the source code.)

I've implemented a source code generator based on annotations using APT in the Quickcheck project.

Another root could be to use ASM (or any other byte code manipulation tool) to read the annotations.




回答3:


I think APT, as suggested by Thomas Jung is what you need. Still, if you want to look into other options, do check Java 6 grammar for antlr




回答4:


How about using JDT parser?

JDT means Java Development Tools. Eclipse use this.

I also did my project with it. It is really nice. And there is much info about it.



来源:https://stackoverflow.com/questions/6184714/java-source-code-parser-which-supports-annotations

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