0

Im looking for a parser that could parse .java file and create some object, which should held informations about this file's methods, attributes. classes, interfaces, methods parameters and annotations.

Do you know any?

I found https://code.google.com/p/javaparser/, but im not sure if it can handle annotations, which are important for me..

any advice?

thanks

3
  • 1
    hmmm why not use reflection to retrieve this information? Commented Jun 27, 2013 at 13:35
  • 1
    If you're not sure if it can handle annotations, it would be preferable it you did some more research prior to posting here... Commented Jun 27, 2013 at 13:35
  • 1
    There are many parsers available for Java code, each with its own intended purpose and advantages/disadvantages. Based on the amount of detail in the original post, 1) there's no way to determine what would be appropriate for your case and 2) these parsers are highly non-trivial to use, and there's no indication that you've researched this space enough to be able to actually use one that gets suggested. Commented Jun 27, 2013 at 16:04

3 Answers 3

1

Why not use the standard Java Compiler API? I know it was discussed within the latest Java Magazine issue -- for some source samples, see here.

Sign up to request clarification or add additional context in comments.

1 Comment

This + Java Annotation Processors should be enough for all code generation needs.
0

You could use the Java Grammar (http://docs.oracle.com/javase/specs/jls/se7/html/jls-18.html) and use a parser generator (for example YACC) or Scala ParserCombinators to generate an AST. In this grammar, annotations are contained.

Comments

0

I am using JavaParser to parse java Code, and yes - it can handle annotations, for example - getting method's annotations:

List<AnnotationExpr> annotations = method.getAnnotations();

Adding annotations (by name) to a method:

annotations.add(new NormalAnnotationExpr( ASTHelper.createNameExpr("Override"), null));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.