Javaparser: Comparing Between Eclipse JDT and JavaParser

Created on 2 Nov 2018  路  9Comments  路  Source: javaparser/javaparser

As far as I know, the most powerful two state-of-the-art parsers for Java are Eclipse JDT and JavaParser. Based upon one of them, I am going to build my application. However, it is hard to choose, so I what to ask:

What are the differences between Eclipse JDT and JavaParser, or what are their particular advantages & disadvantages?

Question (JP usage)

Most helpful comment

One question: in Eclipse JDTParser we can parse code fragments by setting the kind of code, it supports 4 kinds of code:

ASTParser.K_COMPILATION_UNIT
ASTParser.K_CLASS_BODY_DECLARATIONS)
ASTParser.K_EXPRESSION)
ASTParser.K_STATEMENTS))

      ASTParser parser = ASTParser.newParser(8);
      // set the kind of the target code string
      parser.setKind(kind);

In this way, not only a complete compilation unit but also incomplete code fragments can be parsed, I want to ask: does JavaParser provide such a feature?
See also: https://www.programcreek.com/2012/08/parse-a-sequence-of-java-statements-by-using-jdt-astparser/

Yes, you can just parse at whatever granularity level you want. And do symbol resolutions on that snippets.

Can you show me some documents or examples? Thanks!

Sure, just take a look at the Javadoc for the JavaParser class.

There are methods such as:

  • parseExpression
  • parseImport
  • parseAnnotation
  • parseResource

etc.

All 9 comments

I have used the Eclipse JDT before. JavaParser is way, way easier to use. You can configure it in a few lines while JDT comes with a tons of dependencies, OSGi, and an extremely complex configuration (basically you have to recreate an Eclipse project to make it work).

Also, JavaParser is _listening_ to its users, so over time we strive to make it more usable and clear.

That said JDT is probably more mature on the symbol resolving part, as they have much larger resources for the development. If you consider only the parsing part than JP is as mature as it could be.

Here are some feasible alternatives: http://spoon.gforge.inria.fr/ (beware of the license), and I'm pretty sure someone wrote a wrapper for the JDT to make it easier to use.

Thanks for the reply, actually I have been playing with the 3 parsers for a while. Have to say, JavaParser is the most developer-friendly one.

I got to know them from the gumtree project:

https://github.com/GumTreeDiff/gumtree
https://github.com/SpoonLabs/gumtree-spoon-ast-diff

In which it provides 3 backends for Java, each based on one parser (though the other two are not as mature as the one based on JDTParser).

In the Issues page of Spoon, there is also a question (https://github.com/INRIA/spoon/issues/1303) discussing the differences between it and JavaParser, in which they hightlight the "navigation between usage and declaration" feature (maybe that's because it's based on JDT).

Here are some feasible alternatives: http://spoon.gforge.inria.fr/ (beware of the license), and I'm pretty sure someone wrote a wrapper for the JDT to make it easier to use.

Well, I thought it was appropriated to answer to the thread on spoon

Spoon is using Eclipse JDT compiler. Spoon let's compile java sources by JDT compiler, which produces JDT AST. Then spoon transforms that JDT compiler specific and complex AST to java developer friendly Spoon AST.

One question: in Eclipse JDTParser we can parse code fragments by setting the kind of code, it supports 4 kinds of code:

ASTParser.K_COMPILATION_UNIT
ASTParser.K_CLASS_BODY_DECLARATIONS)
ASTParser.K_EXPRESSION)
ASTParser.K_STATEMENTS))

      ASTParser parser = ASTParser.newParser(8);
      // set the kind of the target code string
      parser.setKind(kind);

In this way, not only a complete compilation unit but also incomplete code fragments can be parsed, I want to ask: does JavaParser provide such a feature?

See also: https://www.programcreek.com/2012/08/parse-a-sequence-of-java-statements-by-using-jdt-astparser/

One question: in Eclipse JDTParser we can parse code fragments by setting the kind of code, it supports 4 kinds of code:

ASTParser.K_COMPILATION_UNIT
ASTParser.K_CLASS_BODY_DECLARATIONS)
ASTParser.K_EXPRESSION)
ASTParser.K_STATEMENTS))

      ASTParser parser = ASTParser.newParser(8);
      // set the kind of the target code string
      parser.setKind(kind);

In this way, not only a complete compilation unit but also incomplete code fragments can be parsed, I want to ask: does JavaParser provide such a feature?

See also: https://www.programcreek.com/2012/08/parse-a-sequence-of-java-statements-by-using-jdt-astparser/

Yes, you can just parse at whatever granularity level you want. And do symbol resolutions on that snippets.

One question: in Eclipse JDTParser we can parse code fragments by setting the kind of code, it supports 4 kinds of code:

ASTParser.K_COMPILATION_UNIT
ASTParser.K_CLASS_BODY_DECLARATIONS)
ASTParser.K_EXPRESSION)
ASTParser.K_STATEMENTS))

      ASTParser parser = ASTParser.newParser(8);
      // set the kind of the target code string
      parser.setKind(kind);

In this way, not only a complete compilation unit but also incomplete code fragments can be parsed, I want to ask: does JavaParser provide such a feature?
See also: https://www.programcreek.com/2012/08/parse-a-sequence-of-java-statements-by-using-jdt-astparser/

Yes, you can just parse at whatever granularity level you want. And do symbol resolutions on that snippets.

Can you show me some documents or examples? Thanks!

One question: in Eclipse JDTParser we can parse code fragments by setting the kind of code, it supports 4 kinds of code:

ASTParser.K_COMPILATION_UNIT
ASTParser.K_CLASS_BODY_DECLARATIONS)
ASTParser.K_EXPRESSION)
ASTParser.K_STATEMENTS))

      ASTParser parser = ASTParser.newParser(8);
      // set the kind of the target code string
      parser.setKind(kind);

In this way, not only a complete compilation unit but also incomplete code fragments can be parsed, I want to ask: does JavaParser provide such a feature?
See also: https://www.programcreek.com/2012/08/parse-a-sequence-of-java-statements-by-using-jdt-astparser/

Yes, you can just parse at whatever granularity level you want. And do symbol resolutions on that snippets.

Can you show me some documents or examples? Thanks!

Sure, just take a look at the Javadoc for the JavaParser class.

There are methods such as:

  • parseExpression
  • parseImport
  • parseAnnotation
  • parseResource

etc.

Was this page helpful?
0 / 5 - 0 ratings