Constant Generator
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Ralph Schaer a67c4e3ede upgrade
2026-09-17 05:32:34 +02:00
.github/workflows upgrade 2026-08-30 20:49:07 +02:00
.mvn/wrapper upgrade 2026-05-31 15:45:13 +02:00
src Upgrade 2024-06-30 14:08:26 +02:00
.editorconfig Add .editorconfig 2017-10-25 08:26:45 +02:00
.gitignore Upgrade libraries 2020-03-19 19:04:31 +01:00
LICENSE Polish 2015-09-25 08:57:38 +02:00
mvnw Upgrade 2025-07-26 12:43:58 +02:00
mvnw.cmd Upgrade 2025-07-26 12:43:58 +02:00
mySettings.xml Switch from Travis to GitHub Actions 2019-12-03 04:11:13 +01:00
pom.xml upgrade 2026-09-17 05:32:34 +02:00
README.md Switch from Travis to GitHub Actions 2019-12-03 04:11:13 +01:00

constgen

Test Status

Overview

constgen is a Java Annotation Processor that scans for classes annotated with org.springframework.data.mongodb.core.mapping.Document or org.mongodb.morphia.annotations.Entity. For every annotated class constgen creates an additional Java class with the name Cclassname. This new class contains class variables for every field from the origin class.

Example

Source class

@Document
public class User {
	private String id;
	private String email;
	private String passwordResetToken;
	private Date passwordResetTokenValidUntil;
	private boolean deleted;
}

Generated class

public final class CUser {
  public static final String deleted = "deleted";  
  public static final String email = "email";
  public static final String id = "id";   
  public static final String passwordResetToken = "passwordResetToken";
  public static final String passwordResetTokenValidUntil = "passwordResetTokenValidUntil";   
}

Use Case

When you write queries with Morphia or Spring Data MongoDB you need to specify the fields with String parameters. If you have a typo in a field name MongoDB will not complain because it is schemaless. The C classes help you to make this process a little less error prone. In an IDE you will have code completion and it is more refactor friendly. When somebody removes or renames a field you will immediately see compiler errors.

With String

	User user = mongoTemplate.findAndModify(
			Query.query(Criteria.where("email").is("test@test.com")
			                    .and("deleted").is(false)),
			Update.update("passwordResetTokenValidUntil", new Date())
			      .set("passwordResetToken", "test_token"),
			FindAndModifyOptions.options().returnNew(true), User.class);

With C class

	User user = this.mongoTemplate.findAndModify(
			Query.query(Criteria.where(CUser.email).is("test@test.com")
			                    .and(CUser.deleted).is(false)),
			Update.update(CUser.passwordResetTokenValidUntil, new Date())
				  .set(CUser.passwordResetToken, token),
			FindAndModifyOptions.options().returnNew(true), User.class);

Features

constgen ...

  • requires Java 8 to run.
  • scans for classes annotated with org.springframework.data.mongodb.core.mapping.Document or org.mongodb.morphia.annotations.Entity.
  • ignores transient fields.
  • ignores static fields.
  • ignores fields annotated with org.springframework.data.annotation.Transient or org.mongodb.morphia.annotations.Transient.
  • takes into account the annotations org.springframework.data.mongodb.core.mapping.Field and org.mongodb.morphia.annotations.Property and uses the value of the annotation as value for the String constant.

Maven

To activate the annotation processor you add the library as a dependency to your pom.xml. constgen does not need to present at runtime so the dependency can be marked as optional and will not be included in an jar or war.

	<dependency>
		<groupId>ch.rasc</groupId>
		<artifactId>constgen</artifactId>
		<version>1.0.3</version>
		<optional>true</optional>
	</dependency>

Instead of <optional>true</optional> the provided scope <scope>provided</scope> can be specified. It has the same effect and marks the library as a compile-time only dependency.

The immutable project has a good description on how to use annotation processors in Eclipse and IntelliJ.

The Spring Boot Maven plugin includes dependencies that are optional or scope provided in the final jar. To exclude constgen you need to add an exclude configuration.

	<plugin>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-maven-plugin</artifactId>
		<configuration>
			<excludes>
				<exclude>
					<groupId>ch.rasc</groupId>
					<artifactId>constgen</artifactId>
				</exclude>
			</excludes>
		</configuration>
	</plugin>

Changelog

1.0.3 - January 30, 2016

  • Resolves issue #1

1.0.2 - November 22, 2015

  • Do not ignore transient fields. Spring Data Mongo stores these fields.
  • Support annotations (Field and Transient) from bsoncodec-apt

1.0.1 - October 29, 2015

  • Ignore static fields
  • Generate code the correct way with JavaFileObject

1.0.0 - September 26, 2015

  • Initial release

License

Code released under the Apache license.