Dynamic runtime code generation without the overhead of reflection

What is ActiveJ Codegen?

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
  • Support for Records (heterogeneous data containers)

ActiveJ Codegen use-case

ActiveJ Codegen 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. ActiveJ Codegen 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 ActiveJ Codegen to your project

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

Project Background

ActiveJ Codegen is one of the ActiveJ technologies that also include cloud solutions, lightning-fast serializers, dependency injection and other technologies. To learn more about ActiveJ technologies, visit ActiveJ website.