From 434719f21a7cf17e116fe78d8c80f0b286d7f131 Mon Sep 17 00:00:00 2001 From: Scott Leberknight <174812+sleberknight@users.noreply.github.com> Date: Sun, 10 Mar 2024 19:24:44 -0400 Subject: [PATCH] Provide kiwi validation constraints via service loader mechanism (#1113) * Rename ValidationMessages.properties to ContributorValidationMessages.properties * Add jakarta.validation.ConstraintValidator in META-INF/services which defines the services that will be loaded via service loader * Remove the value in the validatedBy attribute of Constraint annotations on all kiwi custom constraints to avoid duplicate definitions of them. Closes #1112 --- .../java/org/kiwiproject/validation/DirectoryPath.java | 2 +- .../java/org/kiwiproject/validation/FieldRange.java | 2 +- src/main/java/org/kiwiproject/validation/FilePath.java | 2 +- src/main/java/org/kiwiproject/validation/InEnum.java | 2 +- src/main/java/org/kiwiproject/validation/IntValue.java | 2 +- .../java/org/kiwiproject/validation/Ipv4Address.java | 2 +- .../java/org/kiwiproject/validation/Ipv4AndPort.java | 2 +- .../java/org/kiwiproject/validation/LongValue.java | 2 +- src/main/java/org/kiwiproject/validation/Range.java | 2 +- src/main/java/org/kiwiproject/validation/Required.java | 2 +- ...erties => ContributorValidationMessages.properties} | 0 .../services/jakarta.validation.ConstraintValidator | 10 ++++++++++ 12 files changed, 20 insertions(+), 10 deletions(-) rename src/main/resources/{ValidationMessages.properties => ContributorValidationMessages.properties} (100%) create mode 100644 src/main/resources/META-INF/services/jakarta.validation.ConstraintValidator diff --git a/src/main/java/org/kiwiproject/validation/DirectoryPath.java b/src/main/java/org/kiwiproject/validation/DirectoryPath.java index 49a8d140..53f4a700 100644 --- a/src/main/java/org/kiwiproject/validation/DirectoryPath.java +++ b/src/main/java/org/kiwiproject/validation/DirectoryPath.java @@ -50,7 +50,7 @@ * location such as a configuration service, so users should understand the usage risks and mitigate when possible. */ @Documented -@Constraint(validatedBy = {DirectoryPathValidator.class}) +@Constraint(validatedBy = {}) @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) @Retention(RUNTIME) public @interface DirectoryPath { diff --git a/src/main/java/org/kiwiproject/validation/FieldRange.java b/src/main/java/org/kiwiproject/validation/FieldRange.java index 8e394903..66688a99 100644 --- a/src/main/java/org/kiwiproject/validation/FieldRange.java +++ b/src/main/java/org/kiwiproject/validation/FieldRange.java @@ -49,7 +49,7 @@ * single day (again because there is no date component to indicate otherwise), 12:15 AM always comes before 23:56 PM. */ @Documented -@Constraint(validatedBy = {FieldRangeValidator.class}) +@Constraint(validatedBy = {}) @Target({TYPE}) @Retention(RUNTIME) @Repeatable(FieldRanges.class) diff --git a/src/main/java/org/kiwiproject/validation/FilePath.java b/src/main/java/org/kiwiproject/validation/FilePath.java index 512bd9b7..e7289c00 100644 --- a/src/main/java/org/kiwiproject/validation/FilePath.java +++ b/src/main/java/org/kiwiproject/validation/FilePath.java @@ -40,7 +40,7 @@ * location such as a configuration service, so users should understand the usage risks and mitigate when possible. */ @Documented -@Constraint(validatedBy = {FilePathValidator.class}) +@Constraint(validatedBy = {}) @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) @Retention(RUNTIME) public @interface FilePath { diff --git a/src/main/java/org/kiwiproject/validation/InEnum.java b/src/main/java/org/kiwiproject/validation/InEnum.java index f2bf0885..849dc0ef 100644 --- a/src/main/java/org/kiwiproject/validation/InEnum.java +++ b/src/main/java/org/kiwiproject/validation/InEnum.java @@ -42,7 +42,7 @@ * */ @Documented -@Constraint(validatedBy = {InEnumValidator.class}) +@Constraint(validatedBy = {}) @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) @Retention(RUNTIME) public @interface InEnum { diff --git a/src/main/java/org/kiwiproject/validation/IntValue.java b/src/main/java/org/kiwiproject/validation/IntValue.java index 23958e43..82ad97cf 100644 --- a/src/main/java/org/kiwiproject/validation/IntValue.java +++ b/src/main/java/org/kiwiproject/validation/IntValue.java @@ -19,7 +19,7 @@ * The annotated element must have a value that can be converted to a Java int or {@link Integer}. */ @Documented -@Constraint(validatedBy = IntValueValidator.class) +@Constraint(validatedBy = {}) @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) public @interface IntValue { diff --git a/src/main/java/org/kiwiproject/validation/Ipv4Address.java b/src/main/java/org/kiwiproject/validation/Ipv4Address.java index a2d0a240..565d6c16 100644 --- a/src/main/java/org/kiwiproject/validation/Ipv4Address.java +++ b/src/main/java/org/kiwiproject/validation/Ipv4Address.java @@ -21,7 +21,7 @@ * See IPv4 on Wikipedia. */ @Documented -@Constraint(validatedBy = {Ipv4AddressValidator.class}) +@Constraint(validatedBy = {}) @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) @Retention(RUNTIME) public @interface Ipv4Address { diff --git a/src/main/java/org/kiwiproject/validation/Ipv4AndPort.java b/src/main/java/org/kiwiproject/validation/Ipv4AndPort.java index a7b44671..437f9a5c 100644 --- a/src/main/java/org/kiwiproject/validation/Ipv4AndPort.java +++ b/src/main/java/org/kiwiproject/validation/Ipv4AndPort.java @@ -26,7 +26,7 @@ * range). */ @Documented -@Constraint(validatedBy = {Ipv4AndPortValidator.class}) +@Constraint(validatedBy = {}) @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) @Retention(RUNTIME) public @interface Ipv4AndPort { diff --git a/src/main/java/org/kiwiproject/validation/LongValue.java b/src/main/java/org/kiwiproject/validation/LongValue.java index b8fbfa24..be4079db 100644 --- a/src/main/java/org/kiwiproject/validation/LongValue.java +++ b/src/main/java/org/kiwiproject/validation/LongValue.java @@ -19,7 +19,7 @@ * The annotated element must have a value that can be converted to a Java long or {@link java.lang.Long}. */ @Documented -@Constraint(validatedBy = LongValueValidator.class) +@Constraint(validatedBy = {}) @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) public @interface LongValue { diff --git a/src/main/java/org/kiwiproject/validation/Range.java b/src/main/java/org/kiwiproject/validation/Range.java index 03d6cf93..76076f23 100644 --- a/src/main/java/org/kiwiproject/validation/Range.java +++ b/src/main/java/org/kiwiproject/validation/Range.java @@ -55,7 +55,7 @@ * therefore when the {@link RangeValidator} is instantiated. */ @Documented -@Constraint(validatedBy = {RangeValidator.class}) +@Constraint(validatedBy = {}) @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) @Retention(RUNTIME) public @interface Range { diff --git a/src/main/java/org/kiwiproject/validation/Required.java b/src/main/java/org/kiwiproject/validation/Required.java index e47bbf69..7bbfee64 100644 --- a/src/main/java/org/kiwiproject/validation/Required.java +++ b/src/main/java/org/kiwiproject/validation/Required.java @@ -32,7 +32,7 @@ * that method reflectively and use its return value to determine whether the object is empty or not. */ @Documented -@Constraint(validatedBy = {RequiredValidator.class}) +@Constraint(validatedBy = {}) @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) @Retention(RUNTIME) public @interface Required { diff --git a/src/main/resources/ValidationMessages.properties b/src/main/resources/ContributorValidationMessages.properties similarity index 100% rename from src/main/resources/ValidationMessages.properties rename to src/main/resources/ContributorValidationMessages.properties diff --git a/src/main/resources/META-INF/services/jakarta.validation.ConstraintValidator b/src/main/resources/META-INF/services/jakarta.validation.ConstraintValidator new file mode 100644 index 00000000..08fa6b8e --- /dev/null +++ b/src/main/resources/META-INF/services/jakarta.validation.ConstraintValidator @@ -0,0 +1,10 @@ +org.kiwiproject.validation.DirectoryPathValidator +org.kiwiproject.validation.FieldRangeValidator +org.kiwiproject.validation.FilePathValidator +org.kiwiproject.validation.InEnumValidator +org.kiwiproject.validation.IntValueValidator +org.kiwiproject.validation.Ipv4AddressValidator +org.kiwiproject.validation.Ipv4AndPortValidator +org.kiwiproject.validation.LongValueValidator +org.kiwiproject.validation.RequiredValidator +org.kiwiproject.validation.RangeValidator