An open API service indexing awesome lists of open source software.

https://github.com/wnameless/apt-constant-generator

Annotation Processing Tool for Java Constant generating
https://github.com/wnameless/apt-constant-generator

java java-annotation-processing java-annotation-processor

Last synced: 2 months ago
JSON representation

Annotation Processing Tool for Java Constant generating

Awesome Lists containing this project

README

        

[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.wnameless.apt/apt-constant-generator/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.wnameless.apt/apt-constant-generator)

apt-constant-generator
=============
Annotation Processing Tool for Java Constant generating.

## `Goal` - generating classes of Constants based on Java class names
Java Constants(public static final) are widely used during development, however they are lack of flexibility. This apt-constant-generator provides a workaround toward this common issue.

## `Purpose` - easier refactoring and maintenance by utilizing Java Constants
For example, Java annotation only accepts Constant or literal for String. Even though the annotation value may relate to class name somehow, we can't assign Class#getSimpleName or Class#getName to an annotation String attribute which makes refactoring and maintenance difficult sometimes.

## `Furthermore` - optional Constants by annotation driven configuration
Constants such as String, primitive, Class and Enum can also be generated by annotation driven configuration and stored in the same generated class.

# Maven Repo
```xml

com.github.wnameless.apt
apt-constant-generator
${newestVersion}

```

# DEMO
Traditional approach:
```java
@RequestMapping("/bars")
@Controller
public class BarController {}
// Spring Controller for Bar entity
```

Solution brought by apt-constant-generator:
```java
// NRBar is a class of constants, which is automatically generated by spring-boot-up-apt
@RequestMapping(NRBar.RESOURCE_PATH)
@Controller
public class BarController {}
```
```java
@NamedResource
public class Bar {}
```

# Quick Start
Annotate `@NamedResource` with any class, enum or interface
```java
@NamedResource
public class Bar {}
```

Maven dependencies
```xml

com.github.wnameless.apt
apt-constant-generator
${newestVersion}

```

Maven plugins
```xml

org.apache.maven.plugins
maven-compiler-plugin



com.github.wnameless.apt
apt-constant-generator
${newestVersion}


```

# Feature List
| Name | option | Description | Since |
| --- | --- | --- | --- |
| [NamedResource](#1.0.0-1) || Generate a class of constants based on the annotated class name | v1.0.0 |
|| [default](#1.0.0-1.1) | Default behavior of @NamedResource | v1.0.0 |
|| [injectable](#1.0.0-1.2) | Add jakarta.inject.Named or javax.inject.Named to the generated class | v1.0.0 |
|| [markerInterface](#1.0.0-1.3) | Option to implement marker interface INamedResource for the generated class | v1.0.0 |
|| [classNamePrefix
classNameSuffix](#1.0.0-1.4) | Add prefix or suffix to the generated class name | v1.0.0 |
|| [singular](#1.0.0-1.5) | Change singular name for the annotated class | v1.0.0 |
|| [plural](#1.0.0-1.6) | Change plural name for the annotated class | v1.0.0 |
|| [inferredConstants](#1.0.0-1.7) | Infer other String constants by given naming formats | v1.0.0 |
|| [literalSingularConstants](#1.0.0-1.8) | Create constants of literal singular names in different naming formats | v1.0.0 |
|| [literalPluralConstants](#1.0.0-1.9) | Create constants of literal plural names in different naming formats | v1.0.0 |
|| [constants](#1.0.0-1.10) | Add optional String constants | v1.0.0 |
|| [primitiveConstants](#1.0.0-1.11) | Add optional primitive constants | v1.0.0 |
|| [classConstants](#1.0.0-1.12) | Add optional Class constants | v1.0.0 |
|| [enumConstants](#1.0.0-1.13) | Add optional Enum constants | v1.0.0 |

### [:top:](#top) NamedResource
#### [:top:](#top) Annotation option: `default`
_Config_:
```java
@NamedResource
public SimpleNamedResource {}
```
_Generated_: NRSimpleNamedResource (NR is the default class name prefix)
```java
NRSimpleNamedResource.SINGULAR // SimpleNamedResource
NRSimpleNamedResource.PLURAL // SimpleNamedResources

NRSimpleNamedResource.CLASS_SIMPLE_NAME // SimpleNamedResource
NRSimpleNamedResource.CLASS_NAME // model.SimpleNamedResource
NRSimpleNamedResource.PACKAGE_NAME // model

// 5 naming formats: UPPER_CAMEL, LOWER_CAMEL, UPPER_UNDERSCORE, LOWER_UNDERSCORE, LOWER_HYPHEN
NRSimpleNamedResource.UPPER_CAMEL_SINGULAR // SimpleNamedResource
NRSimpleNamedResource.LOWER_CAMEL_SINGULAR // simpleNamedResource
NRSimpleNamedResource.UPPER_UNDERSCORE_SINGULAR // SIMPLE_NAMED_RESOURCE
NRSimpleNamedResource.LOWER_UNDERSCORE_SINGULAR // simple_named_resource
NRSimpleNamedResource.LOWER_HYPHEN_SINGULAR // simple-named-resource
NRSimpleNamedResource.UPPER_CAMEL_PLURAL // SimpleNamedResources
NRSimpleNamedResource.LOWER_CAMEL_PLURAL // simpleNamedResources
NRSimpleNamedResource.UPPER_UNDERSCORE_PLURAL // SIMPLE_NAMED_RESOURCES
NRSimpleNamedResource.LOWER_UNDERSCORE_PLURAL // simple_named_resources
NRSimpleNamedResource.LOWER_HYPHEN_PLURAL // simple-named-resources

// Default inferred constants: RESOURCE, RESOURCES, RESOURCE_PATH
NRSimpleNamedResource.RESOURCE // simple-named-resource
NRSimpleNamedResource.RESOURCES // simple-named-resources
NRSimpleNamedResource.RESOURCE_PATH // /simple-named-resources
```

#### [:top:](#top) Annotation option: `injectable`
_Config_:
```java
// Default value is InjectType.NONE
@NamedResource(injectable = InjectType.JAKARTA) // InjectType.JAVAX is available
public class JakartaNamedResource {}
```
_Generated_:
```java
@Named // jakarta.inject.Named
public final class NRJakartaNamedResource implements INamedResource {
...
}
```

#### [:top:](#top) Annotation option: `markerInterface`
_Config_:
```java
@NamedResource(markerInterface = false) // Default value is true
public class NoMarkerNamedResource {}
```
_Generated_:
```java
public final class NRJakartaNamedResource { // By default, all generated classes implement INamedResource marker interface
...
}
```

#### [:top:](#top) Annotation option: `classNamePrefix` and `classNameSuffix`
_Config_:
```java
@NamedResource(classNamePrefix = "A", classNameSuffix = "Z")
public class PrefixSuffixNamedResource {}
```
_Generated_:
```java
public final class APrefixSuffixNamedResourceZ implements INamedResource {
...
}
```

#### [:top:](#top) Annotation option: `singular`
_Config_:
```java
@NamedResource(singular = @Naming(value = "alt-sing-name-rsc", format = NamingFormat.LOWER_HYPHEN))
public class AlteredSingularNamedResource {}
```
_Generated_:
```java
// JUnit
assertEquals("alt-sing-name-rsc", NRAlteredSingularNamedResource.SINGULAR);
// singular opt affects plural opt,
// however plural opt can be overridden if both singular and plural opts are existed
assertEquals("alt-sing-name-rscs", NRAlteredSingularNamedResource.PLURAL);
```

#### [:top:](#top) Annotation option: `plural`
_Config_:
```java
@NamedResource(plural = @Naming(value = "alt-pl-name-rscs", format = NamingFormat.LOWER_HYPHEN))
public class AlteredPluralOnlyNamedResource {}
```
_Generated_:
```java
// JUnit
// plural opt won't affect singlur opt
assertEquals("AlteredPluralOnlyNamedResource", NRAlteredPluralOnlyNamedResource.SINGULAR);
assertEquals("alt-pl-name-rscs", NRAlteredPluralOnlyNamedResource.PLURAL);
```

#### [:top:](#top) Annotation option: `inferredConstants`
_Config_:
```java
@NamedResource(inferredConstants = {
@InferredConstant(name = "ROOT", plural = true,
format = NamingFormat.UPPER_CAMEL, prefix = "/"),
@InferredConstant(name = "LIST_ITEM_TEMPLATE", plural = false,
format = NamingFormat.LOWER_UNDERSCORE, suffix = "_{}")})
public class InferredConstantsNamedResource {}
```
_Generated_:
```java
// JUnit
assertEquals("/InferredConstantsNamedResources", NRInferredConstantsNamedResource.ROOT);
assertEquals("inferred_constants_named_resource_{}", NRInferredConstantsNamedResource.LIST_ITEM_TEMPLATE);
```

#### [:top:](#top) Annotation option: `literalSingularConstants`
_Config_:
```java
@NamedResource(singular = @Naming(value = "MountEverest", format = NamingFormat.UPPER_CAMEL),
literalSingularConstants = {
NamingFormat.LOWER_CAMEL, NamingFormat.UPPER_CAMEL,
NamingFormat.LOWER_UNDERSCORE, NamingFormat.UPPER_UNDERSCORE
})
```
_Generated_:
```java
// JUnit
assertEquals("mountEverest", NRLiteralSingularNamedResource.mountEverest);
assertEquals("mount_everest", NRLiteralSingularNamedResource.mount_everest);
assertEquals("MountEverest", NRLiteralSingularNamedResource.MountEverest);
assertEquals("MOUNT_EVEREST", NRLiteralSingularNamedResource.MOUNT_EVEREST);
```

#### [:top:](#top) Annotation option: `literalPluralConstants`
_Config_:
```java
@NamedResource(plural = @Naming(value = "bellBottoms", format = NamingFormat.LOWER_CAMEL),
literalPluralConstants = {
NamingFormat.LOWER_CAMEL, NamingFormat.UPPER_CAMEL,
NamingFormat.LOWER_UNDERSCORE, NamingFormat.UPPER_UNDERSCORE
})
```
_Generated_:
```java
// JUnit
assertEquals("bellBottoms", NRLiteralPluralNamedResource.bellBottoms);
assertEquals("BellBottoms", NRLiteralPluralNamedResource.BellBottoms);
assertEquals("bell_bottoms", NRLiteralPluralNamedResource.bell_bottoms);
assertEquals("BELL_BOTTOMS", NRLiteralPluralNamedResource.BELL_BOTTOMS);
```

#### [:top:](#top) Annotation option: `constants`
_Config_:
```java
@NamedResource(constants = {
@Constant(name = "DEATH_CARD", value = "Spade-1"),
@Constant(name = "BEER_CARD", value = "Diamond-7")})
public class ConstantNamedResource {}
```
_Generated_:
```java
// JUnit
assertEquals("Spade-1", NRConstantNamedResource.DEATH_CARD);
assertEquals("Diamond-7", NRConstantNamedResource.BEER_CARD);
```

#### [:top:](#top) Annotation option: `primitive constants`
_Config_:
```java
@NamedResource(
booleanConstants = {@BooleanConstant(name = "LIE", value = false)},
byteConstants = {@ByteConstant(name = "BITS", value = 8)},
charConstants = {@CharConstant(name = "YES", value = 'y')},
shortConstants = {@ShortConstant(name = "UNIX_EPOCH_YEAR", value = 1970)},
intConstants = {@IntConstant(name = "MARS_DISTANCE_KM", value = 225000000)},
longConstants = {@LongConstant(name = "LIGHT_YEAR_M", value = 9460730777119564L)},
floatConstants = {@FloatConstant(name = "GOLDEN_RATIO", value = 1.618f)},
doubleConstants = {@DoubleConstant(name = "PI", value = 3.14159265358979323846)}
)
public class ConstantsNamedResource {}
```
_Generated_:
```java
// JUnit
assertEquals(false, NRConstantsNamedResource.LIE);
assertEquals(8, NRConstantsNamedResource.BITS);
assertEquals('y', NRConstantsNamedResource.YES);
assertEquals(1970, NRConstantsNamedResource.UNIX_EPOCH_YEAR);
assertEquals(225000000, NRConstantsNamedResource.MARS_DISTANCE_KM);
assertEquals(9460730777119564L, NRConstantsNamedResource.LIGHT_YEAR_M);
assertEquals(1.618f, NRConstantsNamedResource.GOLDEN_RATIO);
assertEquals(3.14159265358979323846, NRConstantsNamedResource.PI);
```

#### [:top:](#top) Annotation option: `classConstants`
_Config_:
```java
@NamedResource(
classConstants = {@ClassConstant(name = "INTEGER_CLASS", value = Integer.class)}
)
public class ConstantsNamedResource {}
```
_Generated_:
```java
// JUnit
assertEquals(Integer.class, NRConstantsNamedResource.INTEGER_CLASS);
```

#### [:top:](#top) Annotation option: `enumConstants`
_Config_:
```java
@NamedResource(
enumConstants = {
@EnumConstant(name = "MONTH_SEPTEMBER", valueType = Month.class, valueKey = "SEPTEMBER")}
)
public class ConstantsNamedResource {}
```
_Generated_:
```java
// JUnit
assertEquals(Month.SEPTEMBER, NRConstantsNamedResource.MONTH_SEPTEMBER);
```
_Advanced Usage_:
```java
// Lombok(@FieldNameConstants) - generates String constants for all Enum constant names
@FieldNameConstants
public enum RBG {
@FieldNameConstants.Include RED,
@FieldNameConstants.Include BLUE,
@FieldNameConstants.Include GREEN;
}
```
```java
// To avoid refactoring errors in the future by using RBG.Fields.BLUE as the valueKey
@NamedResource(
enumConstants = {
@EnumConstant(name = "RGB_BLUE", valueType = RBG.class, valueKey = RBG.Fields.BLUE)}
)
public class ConstantsNamedResource {}
```

## MISC
| Note| Since |
| --- | --- |
| Java 17 required. | v1.0.0 |