Skip to content

Commit b560c10

Browse files
committed
Introduce ProxyHints.registerJdkProxy(String...)
Since users might not have a concrete need to work with TypeReference, this commit introduces ProxyHints.registerJdkProxy(String...) to simplify use of the API for registering a dynamic proxy based on fully qualified class names of the required interfaces. Closes gh-28781
1 parent 9db4303 commit b560c10

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*
3030
* @author Stephane Nicoll
3131
* @author Brian Clozel
32+
* @author Sam Brannen
3233
* @since 6.0
3334
*/
3435
public final class JdkProxyHint implements ConditionalHint {
@@ -130,6 +131,17 @@ public Builder proxiedInterfaces(Class<?>... proxiedInterfaces) {
130131
return this;
131132
}
132133

134+
/**
135+
* Add the specified interfaces that the proxy should implement.
136+
* @param proxiedInterfaces the fully qualified class names of interfaces
137+
* the proxy should implement
138+
* @return {@code this}, to facilitate method chaining
139+
*/
140+
public Builder proxiedInterfaces(String... proxiedInterfaces) {
141+
this.proxiedInterfaces.addAll(toTypeReferences(proxiedInterfaces));
142+
return this;
143+
}
144+
133145
/**
134146
* Make this hint conditional on the fact that the specified type
135147
* can be resolved.
@@ -159,6 +171,10 @@ private static List<TypeReference> toTypeReferences(Class<?>... proxiedInterface
159171
return Arrays.stream(proxiedInterfaces).map(TypeReference::of).toList();
160172
}
161173

174+
private static List<TypeReference> toTypeReferences(String... proxiedInterfaces) {
175+
return Arrays.stream(proxiedInterfaces).map(TypeReference::of).toList();
176+
}
177+
162178
}
163179

164180
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* Gather the need for using proxies at runtime.
2828
*
2929
* @author Stephane Nicoll
30+
* @author Sam Brannen
3031
* @since 6.0
3132
*/
3233
public class ProxyHints {
@@ -87,6 +88,18 @@ public ProxyHints registerJdkProxy(Class<?>... proxiedInterfaces) {
8788
jdkProxyHint.proxiedInterfaces(proxiedInterfaces));
8889
}
8990

91+
/**
92+
* Register that a JDK proxy implementing the specified interfaces is
93+
* required.
94+
* @param proxiedInterfaces the fully qualified class names of interfaces the
95+
* proxy should implement
96+
* @return {@code this}, to facilitate method chaining
97+
*/
98+
public ProxyHints registerJdkProxy(String... proxiedInterfaces) {
99+
return registerJdkProxy(jdkProxyHint ->
100+
jdkProxyHint.proxiedInterfaces(proxiedInterfaces));
101+
}
102+
90103
/**
91104
* Register that a class proxy is required for the class defined by the
92105
* specified {@link TypeReference}.

spring-core/src/test/java/org/springframework/aot/hint/ProxyHintsTests.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* Tests for {@link ProxyHints}.
3333
*
3434
* @author Stephane Nicoll
35+
* @author Sam Brannen
3536
*/
3637
class ProxyHintsTests {
3738

@@ -52,10 +53,17 @@ void registerJdkProxyWithConcreteClass() {
5253

5354
@Test
5455
void registerJdkProxyWithInterfaceClassNames() {
56+
this.proxyHints.registerJdkProxy(Function.class.getName(), "com.example.Advised");
57+
assertThat(this.proxyHints.jdkProxies()).singleElement()
58+
.satisfies(proxiedInterfaces(Function.class.getName(), "com.example.Advised"));
59+
}
60+
61+
@Test
62+
void registerJdkProxyWithTypeReferences() {
5563
this.proxyHints.registerJdkProxy(TypeReference.of(Function.class),
5664
TypeReference.of("com.example.Advised"));
57-
assertThat(this.proxyHints.jdkProxies()).singleElement().satisfies(proxiedInterfaces(
58-
Function.class.getName(), "com.example.Advised"));
65+
assertThat(this.proxyHints.jdkProxies()).singleElement()
66+
.satisfies(proxiedInterfaces(Function.class.getName(), "com.example.Advised"));
5967
}
6068

6169
@Test

0 commit comments

Comments
 (0)