Saturday, March 20, 2010

Parse Java Source Code with JaxMe JS

You can parse Java Source Code easily with JaxMe JS.

Here is a code examples that demonstrates how to use JaxME JS. The code won't work as it is, but it's useful to demonstrate JaxMe JS usage in practice.

A weakness of JaxMe JS is that it can only parse and generate Java 1.4 source code files. As of version 2, JaxMe JS cannot parse/generate Java 5 source code that uses Java generics and Java annotations features.

        JavaSourceFactory jsf = new JavaSourceFactory();
        JavaParser jp = new JavaParser(jsf);
        Iterator<JavaSource> javaSources = jsf.getJavaSources();
        JavaSource javaSource =;
        ObjectModel model = new ObjectModel(javaSource.getPackageName(), javaSource.getClassName());
        try {
            for (JavaField field : javaSource.getFields()) {
                log.log(Level.INFO, "Processing " + field.asString());
                model.addProperty(field.getName(), Class.forName(field
        } catch (ClassNotFoundException e) {
            throw new ParserException(e);
        return model; 
Put the following on your Maven project's pom.xml to specify the JaxME JS depency. ANTLR must be specified too.
For more information on grammar/language parsing and code generation with Java (and ANTLR), get the book The Definitive Antlr Reference: Building Domain-Specific Languages (Pragmatic Programmers).


Kevin said...

I was playing with this today for a maven generate-sources plugin.

Unfortunately, it's also unable to correctly parse arrays:

Apparently broken since 2005. Would have been nice library if it actually worked.

Keith Power said...

I'm working on a library that will support this type of thing and more for Java 1.5. I'll post here when I have it ready.

Eric Simmons said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Java Course in Chennai

Getit said...

Superb post.
Download Zapya for PC, Zapya free download, Zapya download, Zapya apk to share files and folders

Post a Comment

Copyright 2009 Spring vs Java EE Web Dev. Powered by Blogger Blogger Templates create by Deluxe Templates. WP by Masterplan