It is with some pride, that I can announce JSGen today, as a new, and very minor, open source project, under the Apache Software License, 2.0
JSGen is short for Java Source Generation framework. As the name (hopefully) suggests, it is a tool for generating Java source code. It is the result of close to 20 years working on, and with Java source generation. That includes, in particular, the XML data binding framework JaxMe, its predecessor Apache JaxMe, and a lot of application programming in my professional work.
It is my opinion, that
- Java source generators have a tendency to grow into CLOB's over time, becoming less maintainable, and understandable.
- Java source generators do typically contain a real lot of boilerplate code, organizing things like import lists, and syntactical details, rather than their actual purpose.
In contrast, JSGen provides an object model, that aims to make source generation just yet another developers task, on which modern software engineering principles can be applied. JSGen will support you by
- Creating import lists semiautomatically (with the exception of static imports)
- Supporting multiple formatting code styles (An Eclipse-like format, the default, and an Apache-Maven-like alternative format.) Switching between code styles is as easy as replacing one configuration object with another.
- Enabling a more structured approach to source code generation. (Example: Implement the case of handling a single instance, and use that to handle the case of a collection.)
Let's have a look at an example, which is quoted from the JUnit tests. We intend to create a simple HelloWorld.java. Here's, how we would do that with JSGen.
JSGFactory factory = JSGFactory.create();
Source jsb =
factory.newSource("com.foo.myapp.Main").makePublic();
Method mainMethod =
jsb.newMethod("main").makePublic().makeStatic();
mainMethod.parameter(JQName.STRING_ARRAY, "pArgs");
mainMethod.body().line(System.class,
".out.println(",
q("Hello, world!"), ");");
File targetJavaDir = new File("target/generated-sources/mysrc");
FileJavaSourceWriter fjsw = new
FileJavaSourceWriter(targetJavaDir);
// You might prefer MAVEN_FORMATTER in the next line.
fjsw.setFormatter(DEFAULT_FORMATTER);
fjsw.write(factory);
Some things I'd finally like to note:
- JSGen is a complete rewrite of two mature, and reliable predecessors, namely JaxMeJS, and Apache JaxMe JS. As such, it is based on a mature API, and a real lot of applied experience. It may be new, but it should be quite reliable, nevertheless.
- It provides all the features, of the predecessors, but adds
- Support for Generics
- Support for Annotations
- Switchable Code formatters
- Builders heavily used in the API
This announcement has intentionally been deferred until the point, where JSGen has succesfully been used for a professional project in my professional work.
Finally, a few links: