This is a follow-up on my last article regarding CodeGen, the MVC-oriented code generator for java.

One of the advantages of the modular approach demonstrated there is that multiple views can be attached to the same generator. By using different “factories”, the rendering can go through the same pipeline and still be divided into different contexts.

In this example, we will render the same “concat-method” model using two different installers. One is the standard Java-installer and one is an example XML-installer.

The XMLInstaller will have two additional views, MethodXMLView and FieldXMLView. Those views will use some of the Java-views (like TypeView) since a type looks the same in both contexts.
public class Main {
    private final static TransformFactory 
        XML = new DefaultTransformFactory("XMLTransformFactory")
            .install(Method.class, MethodXMLView.class)
            .install(Field.class, FieldXMLView.class),

        JAVA = new JavaTransformFactory();

    public static void main(String... args) {
        final Generator gen = new DefaultGenerator(JAVA, XML);"    ");

            Method.of("concat", DefaultType.STRING).public_()
                .add(Field.of("str1", DefaultType.STRING))
                .add(Field.of("str2", DefaultType.STRING))
                .add("return str1 + str2;")
        ).forEach(code -> {
            System.out.println("  " + code.getInstaller().getName() + ":");
public class MethodXMLView implements Transform<Method, String> {
    public Optional<String> render(Generator gen, Method model) {
        return Optional.of(
            "<method name=\"" + model.getName() + "\" type=\"" + 
            gen.on(model.getType()).get() + "\">" + nl() + indent(
                "<params>" + nl() + indent(
                        .filter(c -> XML.equals(c.getFactory()))
                        .map(c -> c.getText())
                ) + nl() + "</params>" + nl() +
                "<code>" + nl() + indent(
                ) + nl() + "</code>"
            ) + nl() + "</methods>"
public class FieldXMLView implements Transform<Field, String> {
    public Optional<String> render(Generator gen, Field model) {
        return Optional.of("<field name=\"" + model.getName() + "\" />");


When the application above is executed, the following will be outputed:

public String concat(String str1, String str2) {
    return str1 + str2;
<method name="concat" type="java.lang.String">
        <field name="str1" />
        <field name="str2" />
        return str1 + str2;

The same model is rendered in two different languages using the same rendering pipeline.