Constant Generator

Annotation processor that creates constants for fields

License

License

GroupId

GroupId

ch.rasc
ArtifactId

ArtifactId

constgen
Last Version

Last Version

1.0.3
Release Date

Release Date

Type

Type

jar
Description

Description

Constant Generator
Annotation processor that creates constants for fields
Project URL

Project URL

https://github.com/ralscha/constgen/
Source Code Management

Source Code Management

https://github.com/ralscha/constgen.git

Download constgen

How to add to project

<!-- https://jarcasting.com/artifacts/ch.rasc/constgen/ -->
<dependency>
    <groupId>ch.rasc</groupId>
    <artifactId>constgen</artifactId>
    <version>1.0.3</version>
</dependency>
// https://jarcasting.com/artifacts/ch.rasc/constgen/
implementation 'ch.rasc:constgen:1.0.3'
// https://jarcasting.com/artifacts/ch.rasc/constgen/
implementation ("ch.rasc:constgen:1.0.3")
'ch.rasc:constgen:jar:1.0.3'
<dependency org="ch.rasc" name="constgen" rev="1.0.3">
  <artifact name="constgen" type="jar" />
</dependency>
@Grapes(
@Grab(group='ch.rasc', module='constgen', version='1.0.3')
)
libraryDependencies += "ch.rasc" % "constgen" % "1.0.3"
[ch.rasc/constgen "1.0.3"]

Dependencies

compile (2)

Group / Artifact Type Version
com.squareup : javapoet jar 1.5.1
com.google.auto.service : auto-service Optional jar 1.0-rc2

test (4)

Group / Artifact Type Version
junit : junit jar 4.12
org.springframework.data : spring-data-mongodb jar 1.8.2.RELEASE
org.mongodb.morphia : morphia jar 1.1.1
com.google.testing.compile : compile-testing jar 0.8

Project Modules

There are no modules declared in this project.

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("[email protected]")
			                    .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("[email protected]")
			                    .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.

Links

Versions

Version
1.0.3
1.0.2
1.0.1
1.0.0