• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Parsing Java Source Code Files

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a tool in Java that will generate API documentation similar to Javadoc, but without the requirement for special tags or comments in the source code file. The result will be professional looking API documentation without cluttered source code.

I would like to build my tool with some existing code that parses a java source code file and provides methods that will allow me to access the different parts of the source code file. In particular, I need to be able to pull out method declarations and variable declarations.

I know that this type of java source code parser must exist as part of an existing IDE like Eclipse or JEdit. Does anyone know of a tool or group of classes that will help me parse a Java source code file and access its elements?

Thanks for any information.

Landon
 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to answer my own question, after digging around the Javadoc for Eclipse. I wanted to post my answer here, in case someone else was interested.

The package in Eclipse that contains the classes I need to use is:

org.eclipse.jdt.core.dom

In particular, I think I will be using the subclasses of the AST class and also the ASTParser class.

Landon
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parsing source code is very tough because language syntax is so complex. For your task, you might look into the doclet APIs. The JavaDoc tool does the heavy lifting (and parsing) and hands all the parsed information to the doclet to write whatever it wants. The standard doclet writes JavaDoc as we know it, but you can make your own to generate code or other documentation formats.

Simple Example

Doclet.com has many more examples of custom doclets and links to lots of info.
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are 3 popular parser generators, all written in Java themselves, which have Java grammars already available: Antlr (on antlr.org), SableCC (on sablecc.org) and JavaCC (on dev.java.net). Those would simplify the job significantly.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would strongly encourage you to look at Stan's doclet suggestion. Writing a doclet is a breeze; any of the other suggestions will entail significantly more work.
 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestions guys. I looked at Sun's Javadoc API, and it is just what I needed.

Thanks,

Landon
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic