Skip to content

Commit 5cf38d0

Browse files
authored
Remove reflective access to java.time classes.
Original Pull Request #1970 Closes #1969
1 parent 8894dd3 commit 5cf38d0

File tree

1 file changed

+69
-3
lines changed

1 file changed

+69
-3
lines changed

Diff for: src/main/java/org/springframework/data/elasticsearch/core/convert/ElasticsearchDateConverter.java

+69-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ final public class ElasticsearchDateConverter {
4747

4848
/**
4949
* Creates an ElasticsearchDateConverter for the given {@link DateFormat}.
50-
*
50+
*
5151
* @param dateFormat must not be @{literal null}
5252
* @return converter
5353
*/
@@ -135,7 +135,7 @@ public Date parse(String input) {
135135
/**
136136
* Creates a {@link DateFormatter} for a given pattern. The pattern can be the name of a {@link DateFormat} enum value
137137
* or a literal pattern.
138-
*
138+
*
139139
* @param pattern the pattern to use
140140
* @return DateFormatter
141141
*/
@@ -172,8 +172,73 @@ private static DateFormatter forPattern(String pattern) {
172172
return new PatternDateFormatter(dateTimeFormatter);
173173
}
174174

175+
@SuppressWarnings("unchecked")
175176
private static <T extends TemporalAccessor> TemporalQuery<T> getTemporalQuery(Class<T> type) {
176177
return temporal -> {
178+
// no reflection for java.time classes (GraalVM native)
179+
if (type == java.time.chrono.HijrahDate.class) {
180+
return (T) java.time.chrono.HijrahDate.from(temporal);
181+
}
182+
if (type == java.time.chrono.JapaneseDate.class) {
183+
return (T) java.time.chrono.JapaneseDate.from(temporal);
184+
}
185+
if (type == java.time.ZonedDateTime.class) {
186+
return (T) java.time.ZonedDateTime.from(temporal);
187+
}
188+
if (type == java.time.LocalDateTime.class) {
189+
return (T) java.time.LocalDateTime.from(temporal);
190+
}
191+
if (type == java.time.chrono.ThaiBuddhistDate.class) {
192+
return (T) java.time.chrono.ThaiBuddhistDate.from(temporal);
193+
}
194+
if (type == java.time.LocalTime.class) {
195+
return (T) java.time.LocalTime.from(temporal);
196+
}
197+
if (type == java.time.ZoneOffset.class) {
198+
return (T) java.time.ZoneOffset.from(temporal);
199+
}
200+
if (type == java.time.OffsetTime.class) {
201+
return (T) java.time.OffsetTime.from(temporal);
202+
}
203+
if (type == java.time.chrono.ChronoLocalDate.class) {
204+
return (T) java.time.chrono.ChronoLocalDate.from(temporal);
205+
}
206+
if (type == java.time.Month.class) {
207+
return (T) java.time.Month.from(temporal);
208+
}
209+
if (type == java.time.chrono.ChronoLocalDateTime.class) {
210+
return (T) java.time.chrono.ChronoLocalDateTime.from(temporal);
211+
}
212+
if (type == java.time.MonthDay.class) {
213+
return (T) java.time.MonthDay.from(temporal);
214+
}
215+
if (type == java.time.Instant.class) {
216+
return (T) java.time.Instant.from(temporal);
217+
}
218+
if (type == java.time.OffsetDateTime.class) {
219+
return (T) java.time.OffsetDateTime.from(temporal);
220+
}
221+
if (type == java.time.chrono.ChronoZonedDateTime.class) {
222+
return (T) java.time.chrono.ChronoZonedDateTime.from(temporal);
223+
}
224+
if (type == java.time.chrono.MinguoDate.class) {
225+
return (T) java.time.chrono.MinguoDate.from(temporal);
226+
}
227+
if (type == java.time.Year.class) {
228+
return (T) java.time.Year.from(temporal);
229+
}
230+
if (type == java.time.DayOfWeek.class) {
231+
return (T) java.time.DayOfWeek.from(temporal);
232+
}
233+
if (type == java.time.LocalDate.class) {
234+
return (T) java.time.LocalDate.from(temporal);
235+
}
236+
if (type == java.time.YearMonth.class) {
237+
return (T) java.time.YearMonth.from(temporal);
238+
}
239+
240+
// for implementations not covered until here use reflection to check for the existence of a static
241+
// from(TemporalAccessor) method
177242
try {
178243
Method method = type.getMethod("from", TemporalAccessor.class);
179244
Object o = method.invoke(null, temporal);
@@ -184,7 +249,8 @@ private static <T extends TemporalAccessor> TemporalQuery<T> getTemporalQuery(Cl
184249
throw new ConversionException("could not create object of class " + type.getName(), e);
185250
}
186251
};
187-
} // endregion
252+
}
253+
// endregion
188254

189255
/**
190256
* a DateFormatter to convert epoch milliseconds

0 commit comments

Comments
 (0)