Skip to main content

Overview

ActiveJ Codegen is an efficient Java code generator with streamlined and simple API. It is built on top of ObjectWeb ASM and has zero-overhead performance. ActiveJ Codegen is widely utilised at ActiveJ Serializer, making it the fastest JVM-based serializer in the world. ActiveJ Codegen is one of the ActiveJ technologies, but it has minimal third-party dependencies and can be used as a stand-alone component.

Why ActiveJ Codegen?

  • Utilizes powerful ASM library for code generation and manipulation
  • Provides concise Expressions API that provides Lisp-like DSL for describing new classes
  • Supports caching of generated classes
  • Suitable for dynamic class generation based on runtime parameters (e.g. query processing)
  • Supports relational algebra operations for individual items: aggregate functions, predicates, ordering, group-by, etc.
  • Features automatic type inference
  • Zero-overhead performance
  • Built-in in-memory caching as well an ability to use persistent bytecode cache
  • Support for Records (heterogeneous data containers)

Code generation

Let's say that we want to create a class that implements Person interface.

public interface Greeter {    void sayHello();}

A generated class should simply print "Hello World" when sayHello() method is called.

public class WorldGreeter implements Greeter {    @Override    public void sayHello() {        System.out.println("Hello World");    }}

To generate such class programmatically we should use a ClassBuilder class to define how a class should be created. And we need to use DefiningClassLoader to actually define a new class.

DefiningClassLoader classLoader = DefiningClassLoader.create();
Class<Greeter> greeterClass = ClassBuilder.create(Greeter.class)        .withMethod("sayHello",                call(staticField(System.class, "out"), "println", value("Hello world")))        .defineClass(classLoader);

Notice the call(staticField(System.class, "out"), "println", value("Hello world")) expression.

This is how Expressions DSL looks like. First, we access static field out of System class. Then we call println() method on it, passing value "Hello world" as an argument. This is equivalent to a System.out.println("Hello world)

When a new instance of this class is created and sayHello() method is called, the output is "Hello world" as expected.

Greeter greeter = greeterClass.getDeclaredConstructor().newInstance();greeter.sayHello(); // prints "Hello world" to stdout

This is merely a hint of what ActiveJ Codegen is capable of. For more information, please refer to examples or internal documentation.

Add ActiveJ Codegen to your project

You can add ActiveJ Codegen to your project by importing its Maven repository. These docs cover the most recent release of ActiveJ Codegen v5.0 (see on Github or Maven).