From 3f46e530e7ff0793a57a67ef6a18753591459d97 Mon Sep 17 00:00:00 2001 From: chrys exaucet Date: Sat, 5 Feb 2022 21:28:05 +0000 Subject: [PATCH] BAEL-5123: Annotation Processor for Hibernate Validator (#11673) * feat(wip): add Hibernate Validator AP example * chore: change AP version to 6.2.0.Final See https://hibernate.atlassian.net/browse/HV-1868 * revert(style): remove unnecessary formatter See https://github.com/eugenp/tutorials/pull/11673#discussion_r784444016 * fix: remove unauthorised annotation The AP was triggering a compile time error because the annotation @NotBlank is disallowed for the Profile data type. That's why the CI was failing. This customer class is unrelated to the article being written. * fix(style): revert indentation issue See https://github.com/eugenp/tutorials/pull/11673/files#r788146422 --- javaxval/pom.xml | 35 ++++++- .../container/validation/Customer.java | 1 - .../hibernate/validator/ap/Message.java | 95 +++++++++++++++++++ 3 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 javaxval/src/main/java/com/baeldung/javaxval/hibernate/validator/ap/Message.java diff --git a/javaxval/pom.xml b/javaxval/pom.xml index 57369c6f52..7d009f3280 100644 --- a/javaxval/pom.xml +++ b/javaxval/pom.xml @@ -15,7 +15,7 @@ - org.hibernate + org.hibernate.validator hibernate-validator ${hibernate-validator.version} @@ -36,10 +36,41 @@ + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven.compiler.version} + + ${maven.compiler.source} + ${maven.compiler.target} + true + + -Averbose=true + -AmethodConstraintsSupported=true + -AdiagnosticKind=ERROR + + + + org.hibernate.validator + hibernate-validator-annotation-processor + ${hibernate-validator.ap.version} + + + + + + + 6.0.13.Final + 6.2.0.Final + 3.6.1 + 1.8 + 1.8 3.0.0 5.0.2.RELEASE - \ No newline at end of file + diff --git a/javaxval/src/main/java/com/baeldung/javaxval/container/validation/Customer.java b/javaxval/src/main/java/com/baeldung/javaxval/container/validation/Customer.java index 03811635ee..d990647721 100644 --- a/javaxval/src/main/java/com/baeldung/javaxval/container/validation/Customer.java +++ b/javaxval/src/main/java/com/baeldung/javaxval/container/validation/Customer.java @@ -20,7 +20,6 @@ public class Customer { @PositiveOrZero private OptionalInt numberOfOrders; - @NotBlank private Profile profile; public String getName() { diff --git a/javaxval/src/main/java/com/baeldung/javaxval/hibernate/validator/ap/Message.java b/javaxval/src/main/java/com/baeldung/javaxval/hibernate/validator/ap/Message.java new file mode 100644 index 0000000000..55d6dafad7 --- /dev/null +++ b/javaxval/src/main/java/com/baeldung/javaxval/hibernate/validator/ap/Message.java @@ -0,0 +1,95 @@ +package com.baeldung.javaxval.hibernate.validator.ap; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Past; +import java.util.List; +import java.util.Optional; + +public class Message { + + @NotNull(message = "Content cannot be null") + private String content; + + private boolean isDelivered; + + private List<@NotBlank String> recipients; + + // uncomment in order to trigger AP annotation detection + // The annotation @Past is disallowed for this data type. + // @Past + private String createdAt; + + public String getContent() { + return content; + } + + public Message(String content, boolean isDelivered, List<@NotBlank String> recipients, String createdAt) { + this.content = content; + this.isDelivered = isDelivered; + this.recipients = recipients; + this.createdAt = createdAt; + } + + // uncomment in order to trigger AP annotation detection + // The annotation @Min is disallowed for the return type of this method. + // @Min(3) + public boolean broadcast() { + // setup a logic + // to send to recipients + return true; + } + + // uncomment in order to trigger AP annotation detection + // Void methods may not be annotated with constraint annotations. + // @NotNull + public void archive() { + // archive the message + } + + // uncomment in order to trigger AP annotation detection + // Constraint annotations must not be specified at methods, which are no valid JavaBeans getter methods. + // NOTE: add -AmethodConstraintsSupported=false to compiler args before + // @AssertTrue + public boolean delete() { + // delete the message + return false; + } + + public void setContent(String content) { + this.content = content; + } + + public boolean isDelivered() { + return isDelivered; + } + + public void setDelivered(boolean delivered) { + isDelivered = delivered; + } + + public List getRecipients() { + return recipients; + } + + public void setRecipients(List recipients) { + this.recipients = recipients; + } + + public void setCreatedAt(String createdAt) { + this.createdAt = createdAt; + } + + public String getName() { + return content; + } + + public void setName(String content) { + this.content = content; + } + + public Optional<@Past String> getCreatedAt() { + return Optional.ofNullable(createdAt); + } + +}