Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java source code parser

I need to programmatically extract method definition/implementation from Java source file, could you recommend any handy library that I can use? Thanks.

like image 715
eric2323223 Avatar asked Nov 25 '10 02:11

eric2323223


People also ask

What is Java parser?

What is parse in Java? There are many Java classes that have the parse() method. Usually the parse() method receives some string as input, "extracts" the necessary information from it and converts it into an object of the calling class.

How do you write parser in Java?

There are three ways of parsing in Java: Using an existing library. Using a tool or library to build a parser. By building a custom parser from scratch.

How does Java parser work?

A parser is a Java class that extracts attributes from a local file and stores the information in the repository. More specifically, in the case of a document, a parser: Takes in an InputStream or Reader object. Processes the character input, extracting attributes as it goes.

How do you parse a string in Java?

To parse a string in Java, you can use the Java String split() method, Java Scanner class, or StringUtils class. For parsing a string based on the specified condition, these methods use delimiters to split the string.


2 Answers

ANTLR is a parser/generator that has grammars for Java (and other languages). It can produce an abstract syntax tree (AST) that you can navigate and manipulate as you wish.

like image 71
duffymo Avatar answered Oct 23 '22 09:10

duffymo


Another option is Jflex and CUP. As per my usage experience, I liked the syntax more than ANTLR. However ANTLR scores more when it comes to IDE support and documentation..

like image 4
Nrj Avatar answered Oct 23 '22 09:10

Nrj