Skip to content

Commit 1365a87

Browse files
committed
add ours typed-ids-index-java-classes-processor module
1 parent e62eb14 commit 1365a87

File tree

21 files changed

+181
-69
lines changed

21 files changed

+181
-69
lines changed

README.md

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,69 @@ data class User(id: Id) {
141141
}
142142
```
143143

144-
## Usage: Enabling automatic type registration with Hibernate ORM
144+
## Typed IDs indexing at compile time
145145

146-
Install the [newest version](https://central.sonatype.com/artifact/org.atteo.classindex/classindex) of [org.atteo.classindex:classindex](https://github.com/atteo/classindex), and register it as an annotation processor.
147-
The classes of this library are annotated with `@IndexSubclasses`, so once the classindex library is correctly installed,
148-
you should see `META-INF/services/org.framefork.typedIds.uuid.ObjectUuid` being populated somewhere in your build output directory.
149-
The `ObjectUuidTypesContributor` should then read it, and register the types automatically when Hibernate ORM is initialized.
146+
This library provides a mechanism to index your ID classes at compile time, which is useful for pleasant integrations with various frameworks.
150147

151-
When used with a Kotlin project, you might want to explicitly add also a `kapt("org.atteo.classindex:classindex")` dependency.
148+
### Setting up subtype indexing in a Java project
152149

153-
Without the automatic registration, the field has to be annotated like this
150+
Set up the `org.framefork:typed-ids-index-java-classes-processor` as an annotation processor.
151+
152+
It's based on [org.atteo.classindex:classindex](https://github.com/atteo/classindex), and when executed,
153+
it writes (somewhere in your build output directory) to `META-INF/services/org.framefork.typedIds.uuid.ObjectUuid` or `META-INF/services/org.framefork.typedIds.bigint.ObjectBigIntId`,
154+
which can be later read by the standard `java.util.ServiceLoader` mechanism.
155+
156+
With Gradle, register the processor like this:
157+
158+
```kotlin
159+
dependencies {
160+
annotationProcessor("org.framefork:typed-ids-index-java-classes-processor")
161+
testAnnotationProcessor("org.framefork:typed-ids-index-java-classes-processor")
162+
}
163+
```
164+
165+
With Maven, you can register it as an optional dependency if annotation processors discovery works in your project
166+
167+
```xml
168+
<dependencies>
169+
<dependency>
170+
<groupId>org.framefork</groupId>
171+
<artifactId>typed-ids-index-java-classes-processor</artifactId>
172+
<optional>true</optional>
173+
</dependency>
174+
</dependencies>
175+
```
176+
177+
Or if you want to be safe, you can register the processor explicitly
178+
179+
```xml
180+
<plugin>
181+
<groupId>org.apache.maven.plugins</groupId>
182+
<artifactId>maven-compiler-plugin</artifactId>
183+
<configuration>
184+
<annotationProcessorPaths>
185+
<path>
186+
<groupId>org.framefork</groupId>
187+
<artifactId>typed-ids-index-java-classes-processor</artifactId>
188+
</path>
189+
</annotationProcessorPaths>
190+
</configuration>
191+
</plugin>
192+
```
193+
194+
### Setting up subtype indexing in a Kotlin project
195+
196+
With Kotlin, you have to register the indexer as KAPT processor:
197+
198+
```kotlin
199+
dependencies {
200+
kapt("org.framefork:typed-ids-index-java-classes-processor") // instead of annotationProcessor(...)
201+
}
202+
```
203+
204+
## Usage: Automatic type registration with Hibernate ORM
205+
206+
Without the Typed IDs indexing at compile time, the field has to be annotated like this (the `@Type(...)` is the important part):
154207
155208
```java
156209
@Entity
@@ -169,7 +222,9 @@ public class User
169222
// ...
170223
```
171224
172-
But with the class indexer installed, The system will know that the `User.Id` should be handled by `ObjectUuidType` and the `@Type(...)` can be dropped.
225+
But with the index, the `ObjectUuidTypesContributor` reads it, and registers the types automatically when Hibernate ORM is initialized.
226+
227+
With the classes indexed, the system will know that the `User.Id` should be handled by `ObjectUuidType` and the `@Type(...)` can be dropped.
173228
This also simplifies usage on every other place, where Hibernate might need to resolve a type for the `Id` instance, like queries.
174229
175230
## More examples

gradle/libs.versions.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
[versions]
2-
kotlin = "2.0.20"
32
googleAutoService = "1.1.1"
43
errorProne = "2.35.1"
5-
ateoClassindex = "3.13"
64
junit-jupiter = "5.10.3"
75
jackson = "2.18.1"
86
testcontainers = "1.20.3"
@@ -15,7 +13,7 @@ jackson-annotations = { group = "com.fasterxml.jackson.core", name = "jackson-an
1513
autoService-processor = { group = "com.google.auto.service", name = "auto-service", version.ref = "googleAutoService" }
1614
autoService-annotations = { group = "com.google.auto.service", name = "auto-service-annotations", version.ref = "googleAutoService" }
1715
errorprone-annotations = { group = "com.google.errorprone", name = "error_prone_annotations", version.ref = "errorProne" }
18-
ateoClassindex = { group = "org.atteo.classindex", name = "classindex", version.ref = "ateoClassindex" }
16+
ateoClassindex = { group = "org.atteo.classindex", name = "classindex", version = "3.13" }
1917
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
2018
assertj = { module = "org.assertj:assertj-core", version = "3.11.1" }
2119
testcontainers-bom = { module = "org.testcontainers:testcontainers-bom", version.ref = "testcontainers" }

modules/typed-ids-hibernate-62/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ dependencies {
66
api(project(":typed-ids"))
77
api(libs.hibernate.orm.v62)
88
api(libs.hypersistence.utils.hibernate62)
9-
api(libs.ateoClassindex)
109

1110
compileOnly(libs.jetbrains.annotations)
1211

modules/typed-ids-hibernate-62/src/main/java/org/framefork/typedIds/bigint/hibernate/ObjectBigIntIdTypesContributor.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.auto.service.AutoService;
44
import org.framefork.typedIds.TypedIdsRegistry;
5-
import org.framefork.typedIds.common.ReflectionHacks;
65
import org.hibernate.boot.model.TypeContributions;
76
import org.hibernate.boot.model.TypeContributor;
87
import org.hibernate.service.ServiceRegistry;
@@ -15,8 +14,6 @@
1514
public class ObjectBigIntIdTypesContributor implements TypeContributor
1615
{
1716

18-
private static final boolean CLASS_INDEX_PRESENT = ReflectionHacks.classExists("org.atteo.classindex.ClassIndex");
19-
2017
@Override
2118
public void contribute(
2219
final TypeContributions typeContributions,
@@ -28,10 +25,6 @@ public void contribute(
2825

2926
private void contributeIndexedTypes(final TypeContributions typeContributions)
3027
{
31-
if (!CLASS_INDEX_PRESENT) {
32-
return;
33-
}
34-
3528
TypeConfiguration typeConfiguration = typeContributions.getTypeConfiguration();
3629
JdbcType bigintJdbcType = typeConfiguration.getJdbcTypeRegistry().getDescriptor(SqlTypes.BIGINT);
3730

modules/typed-ids-hibernate-62/src/main/java/org/framefork/typedIds/uuid/hibernate/ObjectUuidTypesContributor.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.auto.service.AutoService;
44
import org.framefork.typedIds.TypedIdsRegistry;
5-
import org.framefork.typedIds.common.ReflectionHacks;
65
import org.framefork.typedIds.uuid.hibernate.jdbc.BinaryUuidJdbcType;
76
import org.framefork.typedIds.uuid.hibernate.jdbc.NativeUuidJdbcType;
87
import org.hibernate.boot.model.TypeContributions;
@@ -30,8 +29,6 @@ public class ObjectUuidTypesContributor implements TypeContributor
3029
*/
3130
private static final DatabaseVersion MARIADB_NATIVE_UUID_SINCE = DatabaseVersion.make(10, 7);
3231

33-
private static final boolean CLASS_INDEX_PRESENT = ReflectionHacks.classExists("org.atteo.classindex.ClassIndex");
34-
3532
@Override
3633
public void contribute(
3734
final TypeContributions typeContributions,
@@ -65,10 +62,6 @@ private void remapUuidType(final TypeContributions typeContributions, final Serv
6562

6663
private void contributeIndexedTypes(final TypeContributions typeContributions)
6764
{
68-
if (!CLASS_INDEX_PRESENT) {
69-
return;
70-
}
71-
7265
TypeConfiguration typeConfiguration = typeContributions.getTypeConfiguration();
7366
JdbcType uuidJdbcType = typeConfiguration.getJdbcTypeRegistry().getDescriptor(SqlTypes.UUID);
7467

modules/typed-ids-hibernate-63/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ dependencies {
66
api(project(":typed-ids"))
77
api(libs.hibernate.orm.v63)
88
api(libs.hypersistence.utils.hibernate63)
9-
api(libs.ateoClassindex)
109

1110
compileOnly(libs.jetbrains.annotations)
1211

modules/typed-ids-hibernate-63/src/main/java/org/framefork/typedIds/bigint/hibernate/ObjectBigIntIdTypesContributor.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.auto.service.AutoService;
44
import org.framefork.typedIds.TypedIdsRegistry;
5-
import org.framefork.typedIds.common.ReflectionHacks;
65
import org.hibernate.boot.model.TypeContributions;
76
import org.hibernate.boot.model.TypeContributor;
87
import org.hibernate.service.ServiceRegistry;
@@ -15,8 +14,6 @@
1514
public class ObjectBigIntIdTypesContributor implements TypeContributor
1615
{
1716

18-
private static final boolean CLASS_INDEX_PRESENT = ReflectionHacks.classExists("org.atteo.classindex.ClassIndex");
19-
2017
@Override
2118
public void contribute(
2219
final TypeContributions typeContributions,
@@ -28,10 +25,6 @@ public void contribute(
2825

2926
private void contributeIndexedTypes(final TypeContributions typeContributions)
3027
{
31-
if (!CLASS_INDEX_PRESENT) {
32-
return;
33-
}
34-
3528
TypeConfiguration typeConfiguration = typeContributions.getTypeConfiguration();
3629
JdbcType bigintJdbcType = typeConfiguration.getJdbcTypeRegistry().getDescriptor(SqlTypes.BIGINT);
3730

modules/typed-ids-hibernate-63/src/main/java/org/framefork/typedIds/uuid/hibernate/ObjectUuidTypesContributor.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.auto.service.AutoService;
44
import org.framefork.typedIds.TypedIdsRegistry;
5-
import org.framefork.typedIds.common.ReflectionHacks;
65
import org.framefork.typedIds.uuid.hibernate.jdbc.BinaryUuidJdbcType;
76
import org.framefork.typedIds.uuid.hibernate.jdbc.NativeUuidJdbcType;
87
import org.hibernate.boot.model.TypeContributions;
@@ -30,8 +29,6 @@ public class ObjectUuidTypesContributor implements TypeContributor
3029
*/
3130
private static final DatabaseVersion MARIADB_NATIVE_UUID_SINCE = DatabaseVersion.make(10, 7);
3231

33-
private static final boolean CLASS_INDEX_PRESENT = ReflectionHacks.classExists("org.atteo.classindex.ClassIndex");
34-
3532
@Override
3633
public void contribute(
3734
final TypeContributions typeContributions,
@@ -65,10 +62,6 @@ private void remapUuidType(final TypeContributions typeContributions, final Serv
6562

6663
private void contributeIndexedTypes(final TypeContributions typeContributions)
6764
{
68-
if (!CLASS_INDEX_PRESENT) {
69-
return;
70-
}
71-
7265
TypeConfiguration typeConfiguration = typeContributions.getTypeConfiguration();
7366
JdbcType uuidJdbcType = typeConfiguration.getJdbcTypeRegistry().getDescriptor(SqlTypes.UUID);
7467

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
plugins {
2+
id("framefork.java-public")
3+
}
4+
5+
dependencies {
6+
api(project(":typed-ids"))
7+
8+
api(libs.errorprone.annotations)
9+
api(libs.ateoClassindex)
10+
11+
compileOnly(libs.jetbrains.annotations)
12+
13+
compileOnly(libs.autoService.annotations)
14+
annotationProcessor(libs.autoService.processor)
15+
}
16+
17+
project.description = "TypeIds for safer code - Java Compiler Annotation Processor for indexing your TypedId classes"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.framefork.typedIds;
2+
3+
import com.google.auto.service.AutoService;
4+
import org.atteo.classindex.processor.ClassIndexProcessor;
5+
import org.framefork.typedIds.bigint.ObjectBigIntId;
6+
import org.framefork.typedIds.uuid.ObjectUuid;
7+
8+
import javax.annotation.processing.Processor;
9+
10+
@AutoService(Processor.class)
11+
public class TypedIdsClassIndexProcessor extends ClassIndexProcessor
12+
{
13+
14+
@SuppressWarnings("rawtypes")
15+
public TypedIdsClassIndexProcessor()
16+
{
17+
indexSubclasses(ObjectUuid.class);
18+
indexSubclasses(ObjectBigIntId.class);
19+
}
20+
21+
}

0 commit comments

Comments
 (0)