Extract information from a Java source (possible with syntax errors)

S

Stefan Istrate

Hi everybody,

I want to extract information from a Java source file about the
packages, imports, classes, methods and, for every method, about its
local variables. The source file may contain syntax error, so I need
also a skip mechanism. Could you recommend me a good solution provided
by Sun to do this? I don't want to use tools written by any third
party, just Sun libraries. Maybe there is a possibility to invoke the
parser used by javac? A parse tree also would be great.

I've taken a look on sun.tools.java.Parser but it's not so good
documented. Also it seems that it needs to be improved because I'm
getting some NullPointerExceptions when it parses the class body.

Thanks,
Stefan Istrate
 
L

Lew

Stefan said:
Hi everybody,

I want to extract information from a Java source file about the
packages, imports, classes, methods and, for every method, about its
local variables. The source file may contain syntax error, so I need
also a skip mechanism. Could you recommend me a good solution provided
by Sun to do this? I don't want to use tools written by any third
party, just Sun libraries. Maybe there is a possibility to invoke the
parser used by javac? A parse tree also would be great.

I've taken a look on sun.tools.java.Parser but it's not so good
documented. Also it seems that it needs to be improved because I'm
getting some NullPointerExceptions when it parses the class body.

Do not multi-post(send the same message separately to different newsgroups).
We see your question in both groups, but by not cross-posting you fragment the
conversation. Really you only need one of clj.help and clj.programmer.
 
A

Arne Vajhøj

Stefan said:
I want to extract information from a Java source file about the
packages, imports, classes, methods and, for every method, about its
local variables. The source file may contain syntax error, so I need
also a skip mechanism. Could you recommend me a good solution provided
by Sun to do this? I don't want to use tools written by any third
party, just Sun libraries. Maybe there is a possibility to invoke the
parser used by javac? A parse tree also would be great.

I've taken a look on sun.tools.java.Parser but it's not so good
documented. Also it seems that it needs to be improved because I'm
getting some NullPointerExceptions when it parses the class body.

You will need some third party code, because you need a custom
backend for the parser.

Arne
 
A

Albert

Stefan Istrate a écrit :
Hi everybody,

I want to extract information from a Java source file about the
packages, imports, classes, methods and, for every method, about its
local variables. The source file may contain syntax error, so I need
also a skip mechanism. Could you recommend me a good solution provided
by Sun to do this? I don't want to use tools written by any third
party, just Sun libraries. Maybe there is a possibility to invoke the
parser used by javac? A parse tree also would be great.

I've taken a look on sun.tools.java.Parser but it's not so good
documented. Also it seems that it needs to be improved because I'm
getting some NullPointerExceptions when it parses the class body.

Thanks,
Stefan Istrate

If you could trust thrid party tools, like eclipse compiler, you could
get all you asked (AST, imports, etc.). Beside i'm sure eclipse compiler
provide some "not resolved" feature while i doubt sun compiler do.
 
T

Tassilo Horn

Hi Albert,
If you could trust thrid party tools, like eclipse compiler, you could
get all you asked (AST, imports, etc.). Beside i'm sure eclipse
compiler provide some "not resolved" feature while i doubt sun
compiler do.

I'm interested in that topic, too. We have a custom java parser here,
but it's a bit buggy and not really maintainable. So I think about
switching to some free/open source parser where we could hook on.

I already had a look at OpenJDK's javacc internal parser, and it seems
to provide everything I need, but there are comments that those
internals are likely to be subject to changes.

The next thing I will look into are the eclipse JDT.

Do you speak from experience in using the eclipse parser? If so, could
you give any pointers to where I should look into, or maybe even sample
code?

Bye,
Tassilo
 
A

Albert

Tassilo Horn a écrit :
Hi Albert,


I'm interested in that topic, too. We have a custom java parser here,
but it's a bit buggy and not really maintainable. So I think about
switching to some free/open source parser where we could hook on.

I already had a look at OpenJDK's javacc internal parser, and it seems
to provide everything I need, but there are comments that those
internals are likely to be subject to changes.

The next thing I will look into are the eclipse JDT.

Do you speak from experience in using the eclipse parser? If so, could
you give any pointers to where I should look into, or maybe even sample
code?

Nope, i've hardly played and read about the AST bindings but not the
parser itself. Good docs:

http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation_AST/index.html
and
http://www.eclipsecon.org/2004/EclipseCon_2004_TechnicalTrackPresentations/25_Baumer-Mulet.pdf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top