com.laamella:gencodegen-java

Generic Code Generator.

License

License

Categories

Categories

Java Languages
GroupId

GroupId

com.laamella
ArtifactId

ArtifactId

gencodegen-java
Last Version

Last Version

0.10
Release Date

Release Date

Type

Type

jar
Description

Description

com.laamella:gencodegen-java
Generic Code Generator.

Download gencodegen-java

How to add to project

<!-- https://jarcasting.com/artifacts/com.laamella/gencodegen-java/ -->
<dependency>
    <groupId>com.laamella</groupId>
    <artifactId>gencodegen-java</artifactId>
    <version>0.10</version>
</dependency>
// https://jarcasting.com/artifacts/com.laamella/gencodegen-java/
implementation 'com.laamella:gencodegen-java:0.10'
// https://jarcasting.com/artifacts/com.laamella/gencodegen-java/
implementation ("com.laamella:gencodegen-java:0.10")
'com.laamella:gencodegen-java:jar:0.10'
<dependency org="com.laamella" name="gencodegen-java" rev="0.10">
  <artifact name="gencodegen-java" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.laamella', module='gencodegen-java', version='0.10')
)
libraryDependencies += "com.laamella" % "gencodegen-java" % "0.10"
[com.laamella/gencodegen-java "0.10"]

Dependencies

compile (2)

Group / Artifact Type Version
com.laamella : gencodegen-core jar 0.10
com.laamella : gencodegen-io jar 0.10

test (3)

Group / Artifact Type Version
junit : junit jar 4.12
org.slf4j : slf4j-log4j12 jar 1.7.14
com.google.guava : guava jar 18.0

Project Modules

There are no modules declared in this project.

gencodegen

gencodegen

This is a generic code generator that is being used in various projects. There is a Java code generator, a C code generator, and the base on which it is easy to build support for more languages.

It solves the following problems:

  • It is hard to keep indentation correct.

  • When generating code by printing strings it is impossible to go back and add something.

  • Generating code with a full Java AST will lead to an unreadable amount of method calls. (DSL hell :D )

  • Generating code with something like Xtext has a big learning curve.

The basic "Block" class solves these problems. By building a hierarchy of pieces of code and rendering them in a separate step, it is possible to add pieces all over the hierarchy at any time. It knows at which indentation it should render itself, and you can change the indentation of child pieces before adding them. It provides only a few methods, and the sub-projects for specific languages only add the bare minimum of DSL functionality.

This seems to be a good balance between being helpful and getting out of the way.

Java Example

Add the dependency to your build file first. The sample is for gencodegen-c, the C generator, so change that into gencodegen-java.

Create a new File, or Compilation Unit:

JavaFile file = new JavaFile("com.laamella.gencodegen.java", "JavaFileTest");

Add some imports. These can be added at any time, since they are in a an object that will get rendered on demand.

file.imports
		.add(String.class)
		.add(Map.class)
		.add("bla.bla.*");

Add a class and give it a field. Notice how gencodegen only takes care of structure, braces and indentation. The field declaration is written out literally.

ClassBody testClass = file.class_("public class JavaFileTest");
testClass.fields.add("public static final int ABC=3;");

Add a method. A method is a separate block. It can be extended even when other methods after it have been defined. "open" and "close" will handle indentation and braces.

testClass.method("@Test public void happyFlow()")
		.add("// TODO write test")
		.add("int i=0;")
		.open("do")
		.add("i++;")
		.close("while(i<100);");

Write the file to the correct place:

file.write(aggregator);

The result:

package com.laamella.gencodegen.java;

import java.lang.String;
import java.util.Map;
import bla.bla.*;

public class JavaFileTest {
	public static final int ABC=3;

	@Test public void happyFlow() {
		// TODO write test
		int i=0;
		do {
			i++;
		} while(i<100);
	}

}
com.laamella

Laamella Gad

Versions

Version
0.10