Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

4 views
Skip to first unread message

Stefan Istrate

unread,
Apr 7, 2009, 7:31:27 AM4/7/09
to stefan....@gmail.com
Hi everybody,

I want to extract informations from a Java source file about the
packages, imports, classes, methods and, for every method, about it's
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

Roedy Green

unread,
Apr 7, 2009, 8:57:45 AM4/7/09
to
On Tue, 7 Apr 2009 04:31:27 -0700 (PDT), Stefan Istrate
<stefan....@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>I want to extract informations from a Java source file about the
>packages, imports, classes, methods and, for every method, about it's
>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.

See http://mindprod.com/jgloss/parser.html

The catch is most parsers demand perfect compilable code.

Another approach is to write a finite state automaton, a sort of
parser light. I wrote one to analyse Java with the purpose of
colourising it. It works with fragments and errors.

See http://mindprod.com/products1.html#JDISPLAY

You could extract most of the information with a set of regexes. See
http://mindprod.com/jgloss/regex.html

I think you meant static and instance variables, not local, right?

You can also analyse the class files which are already parsed and are
much more amenable to machine analysis. See
http://mindprod.com/jgloss/javaclassformat.html
No comments or local variable names but nearly everything else is in
there.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"At this point, 29 percent of fish and seafood species have collapsed - that is,
their catch has declined by 90 percent. It is a very clear trend, and it is accelerating.
If the long-term trend continues, all fish and seafood species are projected to collapse
within my lifetime -- by 2048."
~ Dr. Boris Worm of Dalhousie University

Lew

unread,
Apr 7, 2009, 9:55:26 AM4/7/09
to
Stefan Istrate wrote:
> 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.

--
Lew

Stefan Istrate

unread,
Apr 8, 2009, 3:53:46 AM4/8/09
to

Sorry for double posting. I will continue only on this thread.

Thank you, Roedy, for your suggestions. Some material on that site
seems useful.

In the meantime, I've found the class
com.sun.tools.javac.main.JavaParser from tools.jar archive (which
seems provided by Sun). I can get a parse tree from that and explore
it very well. Did someone used tools.jar before? Is anything special I
should care about when using it for my own project?

Stefan

Message has been deleted
0 new messages