Win a copy of liveProject: Build an ML Recommender System this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Parsing .java file

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1843
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic