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
>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
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
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