Skip to content

Commit 3f46e53

Browse files
authored
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 eugenp/tutorials#11673 (comment) * 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
1 parent b97323e commit 3f46e53

3 files changed

Lines changed: 128 additions & 3 deletions

File tree

javaxval/pom.xml

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
<dependencies>
1717
<dependency>
18-
<groupId>org.hibernate</groupId>
18+
<groupId>org.hibernate.validator</groupId>
1919
<artifactId>hibernate-validator</artifactId>
2020
<version>${hibernate-validator.version}</version>
2121
</dependency>
@@ -36,10 +36,41 @@
3636
</dependency>
3737
</dependencies>
3838

39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.apache.maven.plugins</groupId>
43+
<artifactId>maven-compiler-plugin</artifactId>
44+
<version>${maven.compiler.version}</version>
45+
<configuration>
46+
<source>${maven.compiler.source}</source>
47+
<target>${maven.compiler.target}</target>
48+
<fork>true</fork>
49+
<compilerArgs>
50+
<arg>-Averbose=true</arg>
51+
<arg>-AmethodConstraintsSupported=true</arg>
52+
<arg>-AdiagnosticKind=ERROR</arg>
53+
</compilerArgs>
54+
<annotationProcessorPaths>
55+
<path>
56+
<groupId>org.hibernate.validator</groupId>
57+
<artifactId>hibernate-validator-annotation-processor</artifactId>
58+
<version>${hibernate-validator.ap.version}</version>
59+
</path>
60+
</annotationProcessorPaths>
61+
</configuration>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
3966
<properties>
4067
<hibernate-validator.version>6.0.13.Final</hibernate-validator.version>
68+
<hibernate-validator.ap.version>6.2.0.Final</hibernate-validator.ap.version>
69+
<maven.compiler.version>3.6.1</maven.compiler.version>
70+
<maven.compiler.source>1.8</maven.compiler.source>
71+
<maven.compiler.target>1.8</maven.compiler.target>
4172
<javax.el.version>3.0.0</javax.el.version>
4273
<org.springframework.version>5.0.2.RELEASE</org.springframework.version>
4374
</properties>
4475

45-
</project>
76+
</project>

javaxval/src/main/java/com/baeldung/javaxval/container/validation/Customer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public class Customer {
2020
@PositiveOrZero
2121
private OptionalInt numberOfOrders;
2222

23-
@NotBlank
2423
private Profile profile;
2524

2625
public String getName() {
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.baeldung.javaxval.hibernate.validator.ap;
2+
3+
import javax.validation.constraints.NotBlank;
4+
import javax.validation.constraints.NotNull;
5+
import javax.validation.constraints.Past;
6+
import java.util.List;
7+
import java.util.Optional;
8+
9+
public class Message {
10+
11+
@NotNull(message = "Content cannot be null")
12+
private String content;
13+
14+
private boolean isDelivered;
15+
16+
private List<@NotBlank String> recipients;
17+
18+
// uncomment in order to trigger AP annotation detection
19+
// The annotation @Past is disallowed for this data type.
20+
// @Past
21+
private String createdAt;
22+
23+
public String getContent() {
24+
return content;
25+
}
26+
27+
public Message(String content, boolean isDelivered, List<@NotBlank String> recipients, String createdAt) {
28+
this.content = content;
29+
this.isDelivered = isDelivered;
30+
this.recipients = recipients;
31+
this.createdAt = createdAt;
32+
}
33+
34+
// uncomment in order to trigger AP annotation detection
35+
// The annotation @Min is disallowed for the return type of this method.
36+
// @Min(3)
37+
public boolean broadcast() {
38+
// setup a logic
39+
// to send to recipients
40+
return true;
41+
}
42+
43+
// uncomment in order to trigger AP annotation detection
44+
// Void methods may not be annotated with constraint annotations.
45+
// @NotNull
46+
public void archive() {
47+
// archive the message
48+
}
49+
50+
// uncomment in order to trigger AP annotation detection
51+
// Constraint annotations must not be specified at methods, which are no valid JavaBeans getter methods.
52+
// NOTE: add <arg>-AmethodConstraintsSupported=false</arg> to compiler args before
53+
// @AssertTrue
54+
public boolean delete() {
55+
// delete the message
56+
return false;
57+
}
58+
59+
public void setContent(String content) {
60+
this.content = content;
61+
}
62+
63+
public boolean isDelivered() {
64+
return isDelivered;
65+
}
66+
67+
public void setDelivered(boolean delivered) {
68+
isDelivered = delivered;
69+
}
70+
71+
public List<String> getRecipients() {
72+
return recipients;
73+
}
74+
75+
public void setRecipients(List<String> recipients) {
76+
this.recipients = recipients;
77+
}
78+
79+
public void setCreatedAt(String createdAt) {
80+
this.createdAt = createdAt;
81+
}
82+
83+
public String getName() {
84+
return content;
85+
}
86+
87+
public void setName(String content) {
88+
this.content = content;
89+
}
90+
91+
public Optional<@Past String> getCreatedAt() {
92+
return Optional.ofNullable(createdAt);
93+
}
94+
95+
}

0 commit comments

Comments
 (0)