views:

199

answers:

4

I'm looking to generate an XML representation of the AST for a given java class (by parsing its source). Overall, what I want to do is write XSLT queries to find meta patterns in the source code - very much like PMD does.

There was an open source utility that started this, but went stale. Anyone know of a utility to do this?

-Mike

A: 

I am not sure about an api, but know that you can reverse engineer a package in ArgoUML and export to XMI. You might look into how the reverse engineering is done, and how the XMI export is done and bridge them.

Matthew Sowders
+1  A: 

You probably already know this, but Eclipse seems to have an integrated AST parser. You could generate XML with JAXB from this or do it manually.

API and Tutorial

moxn
A: 

This is a tutorial for using Eclipse's AST parser, probably meant more for plugin authors looking to provide refactoring or similar features, but I would imagine you could use write to XML instead.

Austin Mills
+1  A: 

I know both ANTLR and JavaCC have full Java parsers. It should be straightforward to build an AST if they don't already, walk the tree and generate XML from that.

The Java Front end for the DMS Software Reengineering Toolkit parses Java, builds ASTs for it, and has a switch option on the parser to export XML directly.

There's a question about how you might use this in practice. The AST for a programming language isn't really interesting; you can only ask context-free questions.

To form more interesting queries, you usually need a bunch of auxiliary facts, such as the meaning of each symbol use (e.g., a symbol table mapping identifiers to types, and an map from instances to symbol table entries), and possibly data flow (given an assignment, where are the consumers of its result) and call facts (who calls what? complicated by Java's OO properties). If you want to ask a serious question of a "java program" (e.g., a set of cooperating classes) you'll need to collect all this data for a set of files and the cross linkages between them.

Simple AST-exporting tools won't provide this kind of information. A tool like DMS's Java front end does, and can handles thousands of files at once.

With all this additional information, queries are harder to write, too. XSLT won't come even close.

Ira Baxter
Thanks! I was just looking into this one.
MikeMontana