• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How to parse a Java source file

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends,

Can any one let me know how to parse a .java file and extract class names, method names, parameter list etc.. from the file.
 
Marshal
Posts: 76464
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

For parsing a .java file, look somewhere like here for the JFlex user's guide. If I remember correctly, JFlex comes with an example for the Java grammar, only it's a bit out of date. You can throw out String and char literals and numbers, as well as keywords and operatoprs, so what remains would be identifiers. It may be a bit difficult to tell what they represent however.

You may find your task easier on a .class file using reflection. I presume you are familiar with the reflection API, but there are details in the Java Tutorials.
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

If you need to work with source files, then all the major Java parser generators (Antlr, SableCC, JavaCC) have grammars for Java source code. It shouldn't be too hard to use those to get the information you're looking for.

If you could somehow use class files this becomes much easier, because java.lang.Class has an API to tell you all that information.
 
reply
    Bookmark Topic Watch Topic
  • New Topic