Code Generator

Code Generator #

A Proxima HOCON configuration already captures all entities, attribute names, scalar vs. wildcard distinctions, and serialization schemes. Rather than repeating string literals ("user", "event.*") and casting raw AttributeDescriptor<?> instances in application code, Proxima provides a Java code generator that compiles your HOCON model into a strongly-typed Model class.

You can invoke the generator in three ways:

  1. Apache Maven plugin (proxima-compiler-java-maven-plugin)
  2. Gradle task (JavaExec invoking ModelGenerator)
  3. Direct CLI invocation (proxima-compiler-java-cli)

Using the Generated Model #

The generated class (named Model by default) provides compile-time typed accessors for every entity and attribute declared in your configuration.

Constructing the Model #

// Production: creates a validated Repository from Config
Model model = Model.of(ConfigFactory.load().resolve());

// Tests: creates a test Repository (Repository.ofTest)
Model testModel = Model.ofTest(ConfigFactory.load("test-reference.conf").resolve());

// Wrap an existing Repository instance
Repository repo = Repository.of(ConfigFactory.load().resolve());
Model wrapped = Model.wrap(repo);

Accessing Typed Attribute Descriptors #

Scalar attributes generate Regular<T> descriptors and wildcard attributes generate Wildcard<T> descriptors:

// Scalar attribute `details` of entity `user`
Regular<UserDetails> detailsDescriptor = model.getUser().getDetailsDescriptor();

// Wildcard attribute `event.*` of entity `user`
Wildcard<BaseEvent> eventDescriptor = model.getUser().getEventDescriptor();

Parsing StreamElements Safely #

Use valueOf(StreamElement) to deserialize values with compile-time type safety:

StreamElement element = ...;

if (element.getAttributeDescriptor().equals(detailsDescriptor)) {
  Optional<UserDetails> details = detailsDescriptor.valueOf(element);
  details.ifPresent(d -> log.info("User email: {}", d.getEmail()));
} else if (element.getAttributeDescriptor().equals(eventDescriptor)) {
  Optional<BaseEvent> baseEvent = eventDescriptor.valueOf(element);
  // Extract wildcard suffix if needed:
  String suffix = eventDescriptor.extractSuffix(element.getAttribute());
}

Creating StreamElements (Upserts and Deletes) #

Typed descriptors also provide convenient builders for creating StreamElements without manual serialization:

// Scalar upsert (UUID auto-generated)
StreamElement upsertDetails = detailsDescriptor.upsert(
    "user1",
    Instant.now(),
    UserDetails.newBuilder()
        .setEmail("me@somewhere.org")
        .build());

// Wildcard upsert with explicit suffix
StreamElement upsertEvent = eventDescriptor.upsert(
    "user1",
    String.valueOf(System.currentTimeMillis()),
    System.currentTimeMillis(),
    baseEvent);

// Delete a single scalar attribute
StreamElement deleteDetails = detailsDescriptor.delete("user1", System.currentTimeMillis());

// Delete a single wildcard entry
StreamElement deleteOneEvent = eventDescriptor.delete(
    "user1", "1700000000000", System.currentTimeMillis());

// Delete ALL wildcard entries matching `event.*` for "user1"
StreamElement deleteAllEvents = eventDescriptor.deleteWildcard(
    "user1", System.currentTimeMillis());

Operator Compiler Plugins #

The generator supports ServiceLoader-discovered OperatorGeneratorFactory plugins that augment the generated Model with operator-specific convenience methods.

For example, adding cz.o2.proxima:proxima-direct-compiler-plugin to the generator classpath adds DirectDataOperator helpers (such as getOperator() and getCommitLog(...)) directly onto the generated Model.


Using the Maven Plugin #

When building with Apache Maven, configure proxima-compiler-java-maven-plugin in the generate-sources phase:

<build>
  <plugins>
    <plugin>
      <groupId>cz.o2.proxima</groupId>
      <artifactId>proxima-compiler-java-maven-plugin</artifactId>
      <version>0.15.0</version>
      <configuration>
        <outputDir>generated-sources/model</outputDir>
        <javaPackage>cz.o2.proxima.example.model</javaPackage>
        <className>Model</className>
        <config>${basedir}/src/main/resources/reference.conf</config>
      </configuration>
      <executions>
        <execution>
          <phase>generate-sources</phase>
          <goals>
            <goal>compile</goal>
          </goals>
        </execution>
      </executions>
      <dependencies>
        <!-- Optional: generate DirectDataOperator helpers -->
        <dependency>
          <groupId>cz.o2.proxima</groupId>
          <artifactId>proxima-direct-compiler-plugin</artifactId>
          <version>0.15.0</version>
        </dependency>
        <!-- Scheme serializers and model classes referenced by reference.conf -->
        <dependency>
          <groupId>cz.o2.proxima</groupId>
          <artifactId>proxima-scheme-proto</artifactId>
          <version>0.15.0</version>
        </dependency>
        <dependency>
          <groupId>cz.o2.proxima.example</groupId>
          <artifactId>example-proto</artifactId>
          <version>${project.version}</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>

Plugin parameters (CompileMojo):

  • outputDir (required): Relative path under target/ or absolute output directory. Automatically registered as a compile source root.
  • javaPackage (required): Target Java package for the generated class.
  • config (required): Path to the HOCON configuration file.
  • className (optional, default "Model"): Simple name of the generated class.

Using Gradle #

In Gradle projects, register a JavaExec task that invokes cz.o2.proxima.generator.ModelGenerator before compileJava and add the output directory to sourceSets.main.java.srcDirs:

dependencies {
  implementation "cz.o2.proxima:proxima-core:0.15.0"
  implementation "cz.o2.proxima:proxima-scheme-proto:0.15.0"
  implementation project(':example-proto')

  compileOnly "cz.o2.proxima:proxima-compiler-java-cli:0.15.0"
  compileOnly "cz.o2.proxima:proxima-direct-compiler-plugin:0.15.0"
  compileOnly "cz.o2.proxima:proxima-direct-core:0.15.0"
}

tasks.register('generateModel', JavaExec) {
  group = 'Build'
  description = 'Generate typed Proxima Model sources'
  classpath = configurations.compileClasspath
  mainClass = 'cz.o2.proxima.generator.ModelGenerator'
  args '-t', "${buildDir}/generated/sources/",
       '-p', 'cz.o2.proxima.example.model',
       '-c', 'Model',
       '-f', 'src/main/resources/reference.conf'
}

tasks.named('compileJava') {
  dependsOn generateModel
}

sourceSets.main.java.srcDirs += ["${buildDir}/generated/sources/"]

Direct CLI Invocation #

You can also invoke ModelGenerator directly from the command line using cz.o2.proxima:proxima-compiler-java-cli:

$ java -jar proxima-compiler-java-cli-0.15.0.jar
Missing required options: p, f
usage: ModelGeneratorInvoker
 -c,--class <arg>     Name of generated class (default: Model)
 -f,--file <arg>      Path to the configuration file
 -p,--package <arg>   Package of the generated class
 -t,--target <arg>    Target directory (default: ./)

Basic invocation (for configs using only built-in core schemes):

$ java -jar proxima-compiler-java-cli-0.15.0.jar \
    -p cz.o2.proxima.example.model \
    -t ./generated-sources \
    -f src/main/resources/reference.conf

When your configuration references external schemes (such as proto: or avro:) or custom generated message JARs, include them on the classpath and invoke ModelGenerator as the main class:

$ java -cp proxima-compiler-java-cli-0.15.0.jar:proxima-scheme-proto-0.15.0.jar:my-protos.jar \
    cz.o2.proxima.generator.ModelGenerator \
    -p cz.o2.proxima.example.model \
    -t ./generated-sources \
    -f src/main/resources/reference.conf