Skip to content

Commit ee6a4e7

Browse files
committed
Add predicates for Serialization hints
Closes gh-28772
1 parent 7751c44 commit ee6a4e7

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

spring-core/src/main/java/org/springframework/aot/hint/RuntimeHintsPredicates.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public abstract class RuntimeHintsPredicates {
3737

3838
private static final ResourceHintsPredicates resource = new ResourceHintsPredicates();
3939

40+
private static final SerializationHintsPredicates serialization = new SerializationHintsPredicates();
41+
4042
private static final ProxyHintsPredicates proxies = new ProxyHintsPredicates();
4143

4244

@@ -59,6 +61,14 @@ public static ResourceHintsPredicates resource() {
5961
return resource;
6062
}
6163

64+
/**
65+
* Return a predicate generator for {@link SerializationHints serialization hints}.
66+
* @return the predicate generator
67+
*/
68+
public static SerializationHintsPredicates serialization() {
69+
return serialization;
70+
}
71+
6272
/**
6373
* Return a predicate generator for {@link ProxyHints proxy hints}.
6474
* @return the predicate generator
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.aot.hint;
18+
19+
import java.util.function.Predicate;
20+
21+
import org.springframework.util.Assert;
22+
23+
/**
24+
* Generator of {@link SerializationHints} predicates, testing whether the
25+
* given hints match the expected behavior for serialization.
26+
*
27+
* @author Stephane Nicoll
28+
* @since 6.0
29+
*/
30+
public class SerializationHintsPredicates {
31+
32+
SerializationHintsPredicates() {
33+
}
34+
35+
/**
36+
* Return a predicate that checks whether a {@link SerializationHints
37+
* serialization hint} is registered for the given type.
38+
* @param type the type to check
39+
* @return the {@link RuntimeHints} predicate
40+
* @see java.lang.reflect.Proxy
41+
*/
42+
public Predicate<RuntimeHints> onType(Class<?> type) {
43+
Assert.notNull(type, "'type' should not be null");
44+
return onType(TypeReference.of(type));
45+
}
46+
47+
/**
48+
* Return a predicate that checks whether a {@link SerializationHints
49+
* serialization hint} is registered for the given type reference.
50+
* @param typeReference the type to check
51+
* @return the {@link RuntimeHints} predicate
52+
* @see java.lang.reflect.Proxy
53+
*/
54+
public Predicate<RuntimeHints> onType(TypeReference typeReference) {
55+
Assert.notNull(typeReference, "'typeReference' should not be null");
56+
return hints -> hints.serialization().javaSerialization().anyMatch(
57+
hint -> hint.getType().equals(typeReference));
58+
}
59+
60+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.aot.hint;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
/**
24+
* Tests for {@link SerializationHintsPredicates}.
25+
*
26+
* @author Stephane Nicoll
27+
*/
28+
class SerializationHintsPredicatesTests {
29+
30+
private final SerializationHintsPredicates serialization = new SerializationHintsPredicates();
31+
32+
private final RuntimeHints runtimeHints = new RuntimeHints();
33+
34+
@Test
35+
void shouldMatchRegisteredClass() {
36+
runtimeHints.serialization().registerType(String.class);
37+
assertThat(serialization.onType(String.class).test(runtimeHints)).isTrue();
38+
}
39+
40+
@Test
41+
void shouldMatchRegisteredTypeReference() {
42+
runtimeHints.serialization().registerType(TypeReference.of(String.class));
43+
assertThat(serialization.onType(String.class).test(runtimeHints)).isTrue();
44+
}
45+
46+
@Test
47+
void shouldNotMatchUnregisteredType() {
48+
runtimeHints.serialization().registerType(Integer.class);
49+
assertThat(serialization.onType(Long.class).test(runtimeHints)).isFalse();
50+
}
51+
52+
}

0 commit comments

Comments
 (0)