<commit_before>---
layout      : post
title       : Object-Oriented Approach to Code Generation
description : Introducing a small open source library for writing web services
headline    : AGE OF JAVA
category    : projects
modified    : 2015-06-02
tags        : [Automatic Programming, CodeGen, Generation, Java, Speedment]
---

Code Generation is a common way to reduce the unhealthy load of boring tasks often put on us eager code monkeys. Many code generation frameworks I have seen use a template-replace-repeat approach where you write a template for how the generated code file should look and then replace certain keywords and repeat other sections to produce the specific file you want.

A problem with this approach that annoys me is that it is really difficult to know if the generated code will work or not until you compile it. You might have changed the name of one class and suddenly the generated code won't build. To handle this issue [I started a project called CodeGen](https://github.com/Pyknic/CodeGen) that aim to be completely object-oriented so that you can benefit from type-safety all the way from template to executable code. The main user case for the generator is the [Speedment software](https://github.com/speedment/speedment), but it can be used in a variety of projects.

Consider the following code:

```java
final Generator generator = new JavaGenerator();

final File file = File.of("org/example/Foo.java")
    .add(Class.of("Foo").public_()
        .add(Field.of("x", DOUBLE_PRIMITIVE).final_())<commit_msg>Update description in codegen article<commit_after>---
layout      : post
title       : Object-Oriented Approach to Code Generation
description : Introducing CodeGen, an MVC-oriented Code Generator for Java
headline    : AGE OF JAVA
category    : projects
modified    : 2015-06-02
tags        : [Automatic Programming, CodeGen, Generation, Java, Speedment]
---

Code Generation is a common way to reduce the unhealthy load of boring tasks often put on us eager code monkeys. Many code generation frameworks I have seen use a template-replace-repeat approach where you write a template for how the generated code file should look and then replace certain keywords and repeat other sections to produce the specific file you want.

A problem with this approach that annoys me is that it is really difficult to know if the generated code will work or not until you compile it. You might have changed the name of one class and suddenly the generated code won't build. To handle this issue [I started a project called CodeGen](https://github.com/Pyknic/CodeGen) that aim to be completely object-oriented so that you can benefit from type-safety all the way from template to executable code. The main user case for the generator is the [Speedment software](https://github.com/speedment/speedment), but it can be used in a variety of projects.

Consider the following code:

```java
final Generator generator = new JavaGenerator();

final File file = File.of("org/example/Foo.java")
    .add(Class.of("Foo").public_()
        .add(Field.of("x", DOUBLE_PRIMITIVE).final_())
{'fraction_non_alphanumeric': 0.06241956241956242, 'fraction_numerical': 0.005148005148005148, 'mean_word_length': 4.13036303630363, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4554758', 'n_tokens_mistral': 792, 'n_tokens_neox': 750, 'n_words': 419}