• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parsing .java file  RSS feed

 
Serghei Jelauc
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys!
I need to extract some information from .java file. For example I need to know all the members, all the methods an so on in particular .java file. Could you give me a hint how can I do this?

Thanks.
 
Scott Dunbar
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you need to read the actual text file or can you operate on a class? If it is the latter you can use the java.lang.reflect classes to determine that information. Otherwise you've got to write a Java file parser. That sounds like a lot less fun.
 
Serghei Jelauc
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately I have to read text (xxx.java, not xxx.class) file. With the .class file all clear. But with the .java ...
 
Barry Higgins
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out a parser called ANTLR

There is already a grammer there for java 1.4, 1.5 (or whatever they call it these days!)

It should be pretty straight forward to traverse through the AST produced to get all your info out. Any problems give post back.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Higgins:
Check out a parser called ANTLR

The URL in the above message was wrong. It is actually

http://www.antlr.org/
 
Roman Merkushen
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try compiling class and then extracting methods and fields using java.lang.reflect package or commons beanutils package.
Roman.
 
Lee George
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Higgins:
[Check out a parser called ANTLR


Why don't you try
JavaCC instead, it's great!
[ December 16, 2004: Message edited by: Lee George ]
 
Free Bird (Kynard)
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Serghei,
Even we faced this kind of a requirement when we were devloping a code analysis tool.

ANTLR(Another Tool For Language Recognition) is a genral purpouse grammar recognition tool, and its a bit difficult getting around using it.

The way we opted out was to write a nice wrapper around StreamTokenizer with the right set of tokens the '{};' delimiters should do...
When you get the right set of tokens u can further break it down by StringTokenizer and you can get your job donr...most imp youn will have to figure out the datastructures u will use to store the variables and class names etc etc...

Hope this help if you need some code help...you are defently welcome...i can pass on the stuff we did

Cheers
Free Bird
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Free Bird (Kynard)

Welcome to JavaRanch! We pretty much have two rules around here: 1). Be Nice and 2). The JavaRanch Naming Policy. Please change your display name to be inaccordance with that policy.

Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!