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);
jp.parse(file);
Iterator<JavaSource> javaSources = jsf.getJavaSources();
JavaSource javaSource = javaSources.next();
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
.getType().toString()));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
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. <dependencies>
<dependency>
<groupId>org.apache.ws.jaxme</groupId>
<artifactId>jaxmejs</artifactId>
<version>0.5.2</version>
</dependency>
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.7</version>
</dependency>
</dependencies>
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)
I was playing with this today for a maven generate-sources plugin.
ReplyDeleteUnfortunately, it's also unable to correctly parse arrays: http://mail-archives.apache.org/mod_mbox//ws-jaxme-dev/200510.mbox/%3COFADE79BC8.3C9FFFCE-ONC1257099.00083D38-C1257099.000AE063@de.ibm.com%3E.
Apparently broken since 2005. Would have been nice library if it actually worked.
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.
ReplyDelete