Dynamic runtime code generation without the overhead of reflection

What is ActiveCodegen?

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

Why ActiveCodegen?

Elegant

Concise API that encapsulates most of the complexity involved in working with bytecode.

Powerful

Dynamically creates classes for runtime query processing. Features automatic type inference with zero-overhead performance.

Simple

Built on top of the ObjectWeb ASM and basically works as embedded Lisp.

Flexible

Supports relational algebra operations for individual items: aggregate functions, predicates, ordering, group-by, etc.

ActiveCodegen use-case

ActiveCodegen can be used for code generation in different scenarios. For example, POJO containers that store the results of processing user data, predicates that filter data placed in database and so on.

Imagine that a service needs to find all adult users in its system. It produces such query to the database:
SELECT id, name, age FROM users WHERE age >= 18;
First, it is necessary to build a data container class (POJO):
class User {
      long id;
      String name;
      int age;
}
The process of a new class generation is simple and minimalistic. Use the builder pattern to produce the fields and methods of a new class:
Class<?> userClass = ClassBuilder.create(Object.class)
    .withField(id, long.class)
    .withField(name, String.class)
    .withField(age, int.class)
    .build()
Now let’s move on to the predicate generation:
class UserPredicate implements Predicate<User> {
    boolean test (User user) {
        return user.age >= 18;
    }
}
Methods production is a little bit trickier. ActiveCodegen provides special expression DSL for method body generation:
Class<Predicate> filter = ClassBuilder.create(Predicate.class)
    .withMethod(test, boolean.class, userClass,
                cmpGe(property(arg(0), age), value(18)))
    .build()

Add ActiveCodegen to your project

You can add ActiveCodegen to your project by importing ActiveCodegen Maven repository.
<dependency>
    <groupId>io.activej</groupId>
    <artifactId>activej-codegen</artifactId>
    <version>1.0</version>
</dependency>

Project Background

ActiveCodegen is one of the ActiveJ technologies that also include cloud solutions, lightning-fast serializers, dependency injection and others technologies. To learn more about ActiveJ technologies, visit website of the core ActiveJ technology - ActiveJ platform.