Skip to content

Commit ce9e2e4

Browse files
committed
Register reflection hints for date ObjectToObjectConverter conventions
Closes gh-29429
1 parent af170f6 commit ce9e2e4

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.convert.support;
18+
19+
import java.time.LocalDate;
20+
import java.util.Collections;
21+
import java.util.List;
22+
23+
import org.springframework.aot.hint.ExecutableMode;
24+
import org.springframework.aot.hint.RuntimeHints;
25+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
26+
import org.springframework.aot.hint.TypeReference;
27+
import org.springframework.lang.Nullable;
28+
29+
/**
30+
* {@link RuntimeHintsRegistrar} to register hints for {@link ObjectToObjectConverter}
31+
* popular conventions.
32+
*
33+
* @author Sebastien Deleuze
34+
* @since 6.0
35+
*/
36+
class ObjectToObjectConverterRuntimeHints implements RuntimeHintsRegistrar {
37+
38+
@Override
39+
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
40+
TypeReference sqlDateTypeReference = TypeReference.of("java.sql.Date");
41+
hints.reflection().registerTypeIfPresent(classLoader, sqlDateTypeReference.getName(), hint -> hint
42+
.withMethod("toLocalDate", Collections.emptyList(), ExecutableMode.INVOKE)
43+
.onReachableType(sqlDateTypeReference)
44+
.withMethod("valueOf", List.of(TypeReference.of(LocalDate.class)), ExecutableMode.INVOKE)
45+
.onReachableType(sqlDateTypeReference));
46+
}
47+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
org.springframework.aot.hint.RuntimeHintsRegistrar=\
2-
org.springframework.aot.hint.support.SpringFactoriesLoaderRuntimeHints
2+
org.springframework.aot.hint.support.SpringFactoriesLoaderRuntimeHints,\
3+
org.springframework.core.convert.support.ObjectToObjectConverterRuntimeHints
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.convert.support;
18+
19+
import java.time.LocalDate;
20+
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.aot.hint.RuntimeHints;
25+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
26+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
27+
import org.springframework.core.io.support.SpringFactoriesLoader;
28+
import org.springframework.util.ClassUtils;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* Tests for {@link ObjectToObjectConverterRuntimeHints}.
34+
*
35+
* @author Sebastien Deleuze
36+
*/
37+
class ObjectToObjectConverterRuntimeHintsTests {
38+
39+
private RuntimeHints hints;
40+
41+
@BeforeEach
42+
void setup() {
43+
this.hints = new RuntimeHints();
44+
SpringFactoriesLoader.forResourceLocation("META-INF/spring/aot.factories")
45+
.load(RuntimeHintsRegistrar.class).forEach(registrar -> registrar
46+
.registerHints(this.hints, ClassUtils.getDefaultClassLoader()));
47+
}
48+
49+
@Test
50+
void javaSqlDateHasHints() throws NoSuchMethodException {
51+
assertThat(RuntimeHintsPredicates.reflection().onMethod(java.sql.Date.class, "toLocalDate")).accepts(this.hints);
52+
assertThat(RuntimeHintsPredicates.reflection().onMethod(java.sql.Date.class.getMethod("valueOf", LocalDate.class))).accepts(this.hints);
53+
}
54+
55+
}

0 commit comments

Comments
 (0)