Skip to content

Commit 6435a03

Browse files
committed
HV-874 Recognizing Java 8 date/time types in the annotation processor
1 parent ab6de14 commit 6435a03

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

annotation-processor/src/main/java/org/hibernate/validator/ap/util/ConstraintHelper.java

+21
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
import org.hibernate.validator.ap.util.TypeNames.BeanValidationTypes;
4040
import org.hibernate.validator.ap.util.TypeNames.HibernateValidatorTypes;
41+
import org.hibernate.validator.ap.util.TypeNames.Java8DateTime;
4142
import org.hibernate.validator.ap.util.TypeNames.JodaTypes;
4243

4344
/**
@@ -169,6 +170,16 @@ public ConstraintHelper(Types typeUtils, AnnotationApiHelper annotationApiHelper
169170
JodaTypes.READABLE_PARTIAL,
170171
JodaTypes.READABLE_INSTANT
171172
);
173+
registerAllowedTypesForBuiltInConstraint(
174+
BeanValidationTypes.FUTURE,
175+
Java8DateTime.CHRONO_LOCAL_DATE,
176+
Java8DateTime.CHRONO_LOCAL_DATE_TIME,
177+
Java8DateTime.CHRONO_ZONED_DATE_TIME,
178+
Java8DateTime.OFFSET_DATE_TIME,
179+
Java8DateTime.INSTANT,
180+
Java8DateTime.YEAR,
181+
Java8DateTime.YEAR_MONTH
182+
);
172183
registerAllowedTypesForBuiltInConstraint( BeanValidationTypes.MAX, Number.class, String.class );
173184
registerAllowedTypesForBuiltInConstraint( BeanValidationTypes.MIN, Number.class, String.class );
174185
registerAllowedTypesForBuiltInConstraint( BeanValidationTypes.NOT_NULL, Object.class );
@@ -179,6 +190,16 @@ public ConstraintHelper(Types typeUtils, AnnotationApiHelper annotationApiHelper
179190
JodaTypes.READABLE_PARTIAL,
180191
JodaTypes.READABLE_INSTANT
181192
);
193+
registerAllowedTypesForBuiltInConstraint(
194+
BeanValidationTypes.PAST,
195+
Java8DateTime.CHRONO_LOCAL_DATE,
196+
Java8DateTime.CHRONO_LOCAL_DATE_TIME,
197+
Java8DateTime.CHRONO_ZONED_DATE_TIME,
198+
Java8DateTime.OFFSET_DATE_TIME,
199+
Java8DateTime.INSTANT,
200+
Java8DateTime.YEAR,
201+
Java8DateTime.YEAR_MONTH
202+
);
182203
registerAllowedTypesForBuiltInConstraint( BeanValidationTypes.PATTERN, String.class );
183204
registerAllowedTypesForBuiltInConstraint(
184205
BeanValidationTypes.SIZE,

annotation-processor/src/main/java/org/hibernate/validator/ap/util/TypeNames.java

+13
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,17 @@ public static class JodaTypes {
8181
public static final String READABLE_PARTIAL = ORG_JODA_TIME + ".ReadablePartial";
8282
public static final String READABLE_INSTANT = ORG_JODA_TIME + ".ReadableInstant";
8383
}
84+
85+
public static class Java8DateTime {
86+
87+
private static final String JAVA_TIME = "java.time";
88+
89+
public static final String CHRONO_LOCAL_DATE = JAVA_TIME + ".chrono.ChronoLocalDate";
90+
public static final String CHRONO_LOCAL_DATE_TIME = JAVA_TIME + ".chrono.ChronoLocalDateTime";
91+
public static final String CHRONO_ZONED_DATE_TIME = JAVA_TIME + ".chrono.ChronoZonedDateTime";
92+
public static final String OFFSET_DATE_TIME = JAVA_TIME + ".OffsetDateTime";
93+
public static final String INSTANT = JAVA_TIME + ".Instant";
94+
public static final String YEAR = JAVA_TIME + ".Year";
95+
public static final String YEAR_MONTH = JAVA_TIME + ".YearMonth";
96+
}
8497
}

annotation-processor/src/test/java/org/hibernate/validator/ap/ConstraintValidationProcessorTest.java

+13
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.hibernate.validator.ap.testmodel.FieldLevelValidationUsingBuiltInConstraints;
2727
import org.hibernate.validator.ap.testmodel.MethodLevelValidationUsingBuiltInConstraints;
2828
import org.hibernate.validator.ap.testmodel.ModelWithDateConstraints;
29+
import org.hibernate.validator.ap.testmodel.ModelWithJava8DateTime;
2930
import org.hibernate.validator.ap.testmodel.ModelWithJodaTypes;
3031
import org.hibernate.validator.ap.testmodel.ModelWithoutConstraints;
3132
import org.hibernate.validator.ap.testmodel.MultipleConstraintsOfSameType;
@@ -539,4 +540,16 @@ public void timeConstraintsAllowedAtJodaTypes() {
539540
new DiagnosticExpectation( Kind.ERROR, 60 )
540541
);
541542
}
543+
544+
@Test
545+
public void timeConstraintsAllowedAtJava8DateTime() {
546+
547+
File sourceFile = compilerHelper.getSourceFile( ModelWithJava8DateTime.class );
548+
549+
boolean compilationResult = compilerHelper.compile(
550+
new ConstraintValidationProcessor(), diagnostics, sourceFile
551+
);
552+
553+
assertTrue( compilationResult, "Java 8 date/time API types fails at @Future/@Past." );
554+
}
542555
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual contributors
4+
* by the @authors tag. See the copyright.txt in the distribution for a
5+
* full listing of individual contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.hibernate.validator.ap.testmodel;
18+
19+
import java.time.Instant;
20+
import java.time.LocalDate;
21+
import java.time.LocalDateTime;
22+
import java.time.OffsetDateTime;
23+
import java.time.Year;
24+
import java.time.YearMonth;
25+
import java.time.ZonedDateTime;
26+
import javax.validation.constraints.Future;
27+
import javax.validation.constraints.Past;
28+
29+
/**
30+
* @author Khalid Alqinyah
31+
*/
32+
public class ModelWithJava8DateTime {
33+
@Past
34+
@Future
35+
public LocalDate localDate;
36+
37+
@Past
38+
@Future
39+
public LocalDateTime localDateTime;
40+
41+
@Past
42+
@Future
43+
public ZonedDateTime zonedDateTime;
44+
45+
@Past
46+
@Future
47+
public Instant instant;
48+
49+
@Past
50+
@Future
51+
public OffsetDateTime offsetDateTime;
52+
53+
@Past
54+
@Future
55+
public Year year;
56+
57+
@Past
58+
@Future
59+
public YearMonth yearMonth;
60+
}

0 commit comments

Comments
 (0)