Browse Source

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
pull/28783/head
Sam Brannen 4 years ago
parent
commit
b560c10d4c
  1. 16
      spring-core/src/main/java/org/springframework/aot/hint/JdkProxyHint.java
  2. 13
      spring-core/src/main/java/org/springframework/aot/hint/ProxyHints.java
  3. 12
      spring-core/src/test/java/org/springframework/aot/hint/ProxyHintsTests.java

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

@ -29,6 +29,7 @@ import org.springframework.lang.Nullable; @@ -29,6 +29,7 @@ import org.springframework.lang.Nullable;
*
* @author Stephane Nicoll
* @author Brian Clozel
* @author Sam Brannen
* @since 6.0
*/
public final class JdkProxyHint implements ConditionalHint {
@ -130,6 +131,17 @@ public final class JdkProxyHint implements ConditionalHint { @@ -130,6 +131,17 @@ public final class JdkProxyHint implements ConditionalHint {
return this;
}
/**
* Add the specified interfaces that the proxy should implement.
* @param proxiedInterfaces the fully qualified class names of interfaces
* the proxy should implement
* @return {@code this}, to facilitate method chaining
*/
public Builder proxiedInterfaces(String... proxiedInterfaces) {
this.proxiedInterfaces.addAll(toTypeReferences(proxiedInterfaces));
return this;
}
/**
* Make this hint conditional on the fact that the specified type
* can be resolved.
@ -159,6 +171,10 @@ public final class JdkProxyHint implements ConditionalHint { @@ -159,6 +171,10 @@ public final class JdkProxyHint implements ConditionalHint {
return Arrays.stream(proxiedInterfaces).map(TypeReference::of).toList();
}
private static List<TypeReference> toTypeReferences(String... proxiedInterfaces) {
return Arrays.stream(proxiedInterfaces).map(TypeReference::of).toList();
}
}
}

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

@ -27,6 +27,7 @@ import org.springframework.aot.hint.ClassProxyHint.Builder; @@ -27,6 +27,7 @@ import org.springframework.aot.hint.ClassProxyHint.Builder;
* Gather the need for using proxies at runtime.
*
* @author Stephane Nicoll
* @author Sam Brannen
* @since 6.0
*/
public class ProxyHints {
@ -87,6 +88,18 @@ public class ProxyHints { @@ -87,6 +88,18 @@ public class ProxyHints {
jdkProxyHint.proxiedInterfaces(proxiedInterfaces));
}
/**
* Register that a JDK proxy implementing the specified interfaces is
* required.
* @param proxiedInterfaces the fully qualified class names of interfaces the
* proxy should implement
* @return {@code this}, to facilitate method chaining
*/
public ProxyHints registerJdkProxy(String... proxiedInterfaces) {
return registerJdkProxy(jdkProxyHint ->
jdkProxyHint.proxiedInterfaces(proxiedInterfaces));
}
/**
* Register that a class proxy is required for the class defined by the
* specified {@link TypeReference}.

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

@ -32,6 +32,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException @@ -32,6 +32,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
* Tests for {@link ProxyHints}.
*
* @author Stephane Nicoll
* @author Sam Brannen
*/
class ProxyHintsTests {
@ -52,10 +53,17 @@ class ProxyHintsTests { @@ -52,10 +53,17 @@ class ProxyHintsTests {
@Test
void registerJdkProxyWithInterfaceClassNames() {
this.proxyHints.registerJdkProxy(Function.class.getName(), "com.example.Advised");
assertThat(this.proxyHints.jdkProxies()).singleElement()
.satisfies(proxiedInterfaces(Function.class.getName(), "com.example.Advised"));
}
@Test
void registerJdkProxyWithTypeReferences() {
this.proxyHints.registerJdkProxy(TypeReference.of(Function.class),
TypeReference.of("com.example.Advised"));
assertThat(this.proxyHints.jdkProxies()).singleElement().satisfies(proxiedInterfaces(
Function.class.getName(), "com.example.Advised"));
assertThat(this.proxyHints.jdkProxies()).singleElement()
.satisfies(proxiedInterfaces(Function.class.getName(), "com.example.Advised"));
}
@Test

Loading…
Cancel
Save