Polishing

This commit is contained in:
Sam Brannen
2023-02-02 14:38:05 +01:00
parent e170c16b02
commit ac6385025b
8 changed files with 95 additions and 89 deletions

View File

@@ -27,31 +27,35 @@ import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
import org.springframework.beans.factory.aot.BeanRegistrationCode;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.MergedAnnotations.Search;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import static org.springframework.core.annotation.MergedAnnotations.SearchStrategy.TYPE_HIERARCHY;
/**
* An AOT {@link BeanRegistrationAotProcessor} that detects the presence of
* {@link RSocketExchange @RSocketExchange} on methods and creates
* the required proxy hints.
* Based on {@code HttpExchangeBeanRegistrationAotProcessor}
* {@link RSocketExchange @RSocketExchange} on methods and creates the required
* proxy hints.
*
* @author Sebastien Deleuze
* @author Olga Maciaszek-Sharma
* @since 6.0
* @since 6.0.5
* @see org.springframework.web.service.annotation.HttpExchangeBeanRegistrationAotProcessor
*/
class RSocketExchangeBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
@Nullable
@Override
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
Class<?> beanClass = registeredBean.getBeanClass();
Set<Class<?>> exchangeInterfaces = new HashSet<>();
Search search = MergedAnnotations.search(TYPE_HIERARCHY);
for (Class<?> interfaceClass : ClassUtils.getAllInterfacesForClass(beanClass)) {
ReflectionUtils.doWithMethods(interfaceClass, method -> {
if (!exchangeInterfaces.contains(interfaceClass) &&
MergedAnnotations.from(method, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.get(RSocketExchange.class).isPresent()) {
search.from(method).isPresent(RSocketExchange.class)) {
exchangeInterfaces.add(interfaceClass);
}
});
@@ -62,6 +66,7 @@ class RSocketExchangeBeanRegistrationAotProcessor implements BeanRegistrationAot
return null;
}
private static class RSocketExchangeBeanRegistrationContribution implements BeanRegistrationAotContribution {
private final Set<Class<?>> rSocketExchangeInterfaces;
@@ -71,14 +76,13 @@ class RSocketExchangeBeanRegistrationAotProcessor implements BeanRegistrationAot
}
@Override
public void applyTo(GenerationContext generationContext,
BeanRegistrationCode beanRegistrationCode) {
public void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode) {
ProxyHints proxyHints = generationContext.getRuntimeHints().proxies();
for (Class<?> httpExchangeInterface : this.rSocketExchangeInterfaces) {
proxyHints.registerJdkProxy(AopProxyUtils
.completeJdkProxyInterfaces(httpExchangeInterface));
for (Class<?> rSocketExchangeInterface : this.rSocketExchangeInterfaces) {
proxyHints.registerJdkProxy(AopProxyUtils.completeJdkProxyInterfaces(rSocketExchangeInterface));
}
}
}
}

View File

@@ -29,19 +29,19 @@ import org.springframework.core.MethodParameter;
/**
* A {@link ReflectiveProcessor} implementation for {@link RSocketExchange @RSocketExchange}
* annotated methods. In addition to registering reflection hints for invoking
* the annotated method, this implementation handles reflection-based
* binding for return types and parameters.
* Based on {@code HttpExchangeReflectiveProcessor}.
* the annotated method, this implementation handles reflection-based binding for
* return types and parameters.
*
* @author Sebastien Deleuze
* @author Olga Maciaszek-Sharma
* @since 6.0
* @since 6.0.5
* @see org.springframework.web.service.annotation.HttpExchangeReflectiveProcessor
*/
public class RSocketExchangeReflectiveProcessor implements ReflectiveProcessor {
class RSocketExchangeReflectiveProcessor implements ReflectiveProcessor {
private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
@Override
public void registerReflectionHints(ReflectionHints hints, AnnotatedElement element) {
if (element instanceof Method method) {
@@ -61,8 +61,8 @@ public class RSocketExchangeReflectiveProcessor implements ReflectiveProcessor {
protected void registerReturnTypeHints(ReflectionHints hints, MethodParameter returnTypeParameter) {
if (!void.class.equals(returnTypeParameter.getParameterType())) {
this.bindingRegistrar.registerReflectionHints(hints, returnTypeParameter
.getGenericParameterType());
this.bindingRegistrar.registerReflectionHints(hints, returnTypeParameter.getGenericParameterType());
}
}
}

View File

@@ -1,2 +1,2 @@
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\
org.springframework.messaging.rsocket.service.RSocketExchangeBeanRegistrationAotProcessor
org.springframework.messaging.rsocket.service.RSocketExchangeBeanRegistrationAotProcessor

View File

@@ -16,16 +16,14 @@
package org.springframework.messaging.rsocket.service;
import org.junit.jupiter.api.Test;
import org.springframework.aop.SpringProxy;
import org.springframework.aop.framework.Advised;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.test.generate.TestGenerationContext;
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
import org.springframework.beans.factory.aot.BeanRegistrationCode;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.beans.factory.support.RootBeanDefinition;
@@ -35,50 +33,52 @@ import org.springframework.messaging.handler.annotation.Payload;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.proxies;
/**
* Tests for {@link RSocketExchangeBeanRegistrationAotProcessor}.
*
* @author Sebastien Deleuze
* @author Olga Maciaszek-Sharma
* @since 6.0.5
*/
class RSocketExchangeBeanRegistrationAotProcessorTests {
private final RSocketExchangeBeanRegistrationAotProcessor processor =
new RSocketExchangeBeanRegistrationAotProcessor();
private final GenerationContext generationContext = new TestGenerationContext();
private final RuntimeHints runtimeHints = this.generationContext.getRuntimeHints();
@Test
void shouldProcessesAnnotatedInterface() {
process(AnnotatedInterface.class);
assertThat(RuntimeHintsPredicates.proxies().forInterfaces(AnnotatedInterface.class,
SpringProxy.class, Advised.class, DecoratingProxy.class))
.accepts(this.generationContext.getRuntimeHints());
assertThat(proxies().forInterfaces(AnnotatedInterface.class, SpringProxy.class, Advised.class, DecoratingProxy.class))
.accepts(this.runtimeHints);
}
@Test
void shouldSkipNonAnnotatedInterface() {
process(NonAnnotatedInterface.class);
assertThat(this.generationContext.getRuntimeHints().proxies().jdkProxyHints()).isEmpty();
assertThat(this.runtimeHints.proxies().jdkProxyHints()).isEmpty();
}
void process(Class<?> beanClass) {
BeanRegistrationAotContribution contribution = createContribution(beanClass);
if (contribution != null) {
contribution.applyTo(this.generationContext, mock(BeanRegistrationCode.class));
contribution.applyTo(this.generationContext, mock());
}
}
@Nullable
private BeanRegistrationAotContribution createContribution(Class<?> beanClass) {
private static BeanRegistrationAotContribution createContribution(Class<?> beanClass) {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
beanFactory.registerBeanDefinition(beanClass.getName(), new RootBeanDefinition(beanClass));
return this.processor.processAheadOfTime(RegisteredBean.of(beanFactory, beanClass.getName()));
return new RSocketExchangeBeanRegistrationAotProcessor()
.processAheadOfTime(RegisteredBean.of(beanFactory, beanClass.getName()));
}
interface NonAnnotatedInterface {
void notExchange();
@@ -91,5 +91,3 @@ class RSocketExchangeBeanRegistrationAotProcessorTests {
}
}

View File

@@ -21,18 +21,19 @@ import java.lang.reflect.Method;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.util.MimeType;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection;
/**
* Tests for {@link RSocketExchangeReflectiveProcessor}.
*
* @author Sebastien Deleuze
* @author Olga Maciaszek-Sharma
* @since 6.0.5
*/
class RSocketExchangeReflectiveProcessorTests {
@@ -40,6 +41,7 @@ class RSocketExchangeReflectiveProcessorTests {
private final RuntimeHints hints = new RuntimeHints();
@Test
void shouldRegisterReflectionHintsForMethod() throws NoSuchMethodException {
Method method = SampleService.class.getDeclaredMethod("get", Request.class, Variable.class,
@@ -47,36 +49,23 @@ class RSocketExchangeReflectiveProcessorTests {
processor.registerReflectionHints(hints.reflection(), method);
assertThat(RuntimeHintsPredicates.reflection().onType(SampleService.class))
.accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleService.class, "get"))
.accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onType(Response.class))
.accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(Response.class, "getMessage"))
.accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(Response.class, "setMessage"))
.accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onType(Request.class))
.accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(Request.class, "getMessage"))
.accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(Request.class, "setMessage"))
.accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onType(Variable.class))
.accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(Variable.class, "getValue"))
.accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(Variable.class, "setValue"))
.accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onType(Metadata.class))
.accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(Metadata.class, "getValue"))
.accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(Metadata.class, "setValue"))
.accepts(hints);
assertThat(reflection().onType(SampleService.class)).accepts(hints);
assertThat(reflection().onMethod(SampleService.class, "get")).accepts(hints);
assertThat(reflection().onType(Response.class)).accepts(hints);
assertThat(reflection().onMethod(Response.class, "getMessage")).accepts(hints);
assertThat(reflection().onMethod(Response.class, "setMessage")).accepts(hints);
assertThat(reflection().onType(Request.class)).accepts(hints);
assertThat(reflection().onMethod(Request.class, "getMessage")).accepts(hints);
assertThat(reflection().onMethod(Request.class, "setMessage")).accepts(hints);
assertThat(reflection().onType(Variable.class)).accepts(hints);
assertThat(reflection().onMethod(Variable.class, "getValue")).accepts(hints);
assertThat(reflection().onMethod(Variable.class, "setValue")).accepts(hints);
assertThat(reflection().onType(Metadata.class)).accepts(hints);
assertThat(reflection().onMethod(Metadata.class, "getValue")).accepts(hints);
assertThat(reflection().onMethod(Metadata.class, "setValue")).accepts(hints);
}
interface SampleService {
@RSocketExchange
@@ -148,4 +137,5 @@ class RSocketExchangeReflectiveProcessorTests {
this.value = value;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,28 +27,33 @@ import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
import org.springframework.beans.factory.aot.BeanRegistrationCode;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.MergedAnnotations.Search;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import static org.springframework.core.annotation.MergedAnnotations.SearchStrategy.TYPE_HIERARCHY;
/**
* AOT {@code BeanRegistrationAotProcessor} that detects the presence of
* {@link HttpExchange @HttpExchange} on methods and creates
* the required proxy hints.
* {@link HttpExchange @HttpExchange} on methods and creates the required proxy
* hints.
*
* @author Sebastien Deleuze
* @since 6.0
*/
class HttpExchangeBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
@Nullable
@Override
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
Class<?> beanClass = registeredBean.getBeanClass();
List<Class<?>> exchangeInterfaces = new ArrayList<>();
Search search = MergedAnnotations.search(TYPE_HIERARCHY);
for (Class<?> interfaceClass : ClassUtils.getAllInterfacesForClass(beanClass)) {
ReflectionUtils.doWithMethods(interfaceClass, method -> {
if (!exchangeInterfaces.contains(interfaceClass) &&
MergedAnnotations.from(method, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.get(HttpExchange.class).isPresent()) {
search.from(method).isPresent(HttpExchange.class)) {
exchangeInterfaces.add(interfaceClass);
}
});
@@ -59,6 +64,7 @@ class HttpExchangeBeanRegistrationAotProcessor implements BeanRegistrationAotPro
return null;
}
private static class HttpExchangeBeanRegistrationAotContribution implements BeanRegistrationAotContribution {
private final List<Class<?>> httpExchangeInterfaces;
@@ -74,5 +80,7 @@ class HttpExchangeBeanRegistrationAotProcessor implements BeanRegistrationAotPro
proxyHints.registerJdkProxy(AopProxyUtils.completeJdkProxyInterfaces(httpExchangeInterface));
}
}
}
}

View File

@@ -21,7 +21,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.aop.SpringProxy;
import org.springframework.aop.framework.Advised;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.test.generate.TestGenerationContext;
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@@ -32,6 +32,7 @@ import org.springframework.lang.Nullable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.proxies;
/**
* Tests for {@link HttpExchangeBeanRegistrationAotProcessor}.
@@ -40,23 +41,25 @@ import static org.mockito.Mockito.mock;
*/
class HttpExchangeBeanRegistrationAotProcessorTests {
private final HttpExchangeBeanRegistrationAotProcessor processor = new HttpExchangeBeanRegistrationAotProcessor();
private final GenerationContext generationContext = new TestGenerationContext();
private final RuntimeHints runtimeHints = this.generationContext.getRuntimeHints();
@Test
void shouldSkipNonAnnotatedInterface() {
process(NonAnnotatedInterface.class);
assertThat(this.generationContext.getRuntimeHints().proxies().jdkProxyHints()).isEmpty();
assertThat(this.runtimeHints.proxies().jdkProxyHints()).isEmpty();
}
@Test
void shouldProcessAnnotatedInterface() {
process(AnnotatedInterface.class);
assertThat(RuntimeHintsPredicates.proxies().forInterfaces(AnnotatedInterface.class, SpringProxy.class, Advised.class,
DecoratingProxy.class)).accepts(this.generationContext.getRuntimeHints());
assertThat(proxies().forInterfaces(AnnotatedInterface.class, SpringProxy.class, Advised.class, DecoratingProxy.class))
.accepts(this.runtimeHints);
}
private void process(Class<?> beanClass) {
BeanRegistrationAotContribution contribution = createContribution(beanClass);
if (contribution != null) {
@@ -65,12 +68,14 @@ class HttpExchangeBeanRegistrationAotProcessorTests {
}
@Nullable
private BeanRegistrationAotContribution createContribution(Class<?> beanClass) {
private static BeanRegistrationAotContribution createContribution(Class<?> beanClass) {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
beanFactory.registerBeanDefinition(beanClass.getName(), new RootBeanDefinition(beanClass));
return this.processor.processAheadOfTime(RegisteredBean.of(beanFactory, beanClass.getName()));
return new HttpExchangeBeanRegistrationAotProcessor()
.processAheadOfTime(RegisteredBean.of(beanFactory, beanClass.getName()));
}
interface NonAnnotatedInterface {
void notExchange();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,10 +21,10 @@ import java.lang.reflect.Method;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.web.bind.annotation.RequestBody;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection;
/**
* Tests for {@link HttpExchangeReflectiveProcessor}.
@@ -37,26 +37,27 @@ class HttpExchangeReflectiveProcessorTests {
private final RuntimeHints hints = new RuntimeHints();
@Test
void registerReflectiveHintsForMethodWithReturnValue() throws NoSuchMethodException {
Method method = SampleService.class.getDeclaredMethod("get");
processor.registerReflectionHints(hints.reflection(), method);
assertThat(RuntimeHintsPredicates.reflection().onType(SampleService.class)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleService.class, "get")).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onType(Response.class)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(Response.class, "getMessage")).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(Response.class, "setMessage")).accepts(hints);
assertThat(reflection().onType(SampleService.class)).accepts(hints);
assertThat(reflection().onMethod(SampleService.class, "get")).accepts(hints);
assertThat(reflection().onType(Response.class)).accepts(hints);
assertThat(reflection().onMethod(Response.class, "getMessage")).accepts(hints);
assertThat(reflection().onMethod(Response.class, "setMessage")).accepts(hints);
}
@Test
void registerReflectiveHintsForMethodWithRequestBodyParameter() throws NoSuchMethodException {
Method method = SampleService.class.getDeclaredMethod("post", Request.class);
processor.registerReflectionHints(hints.reflection(), method);
assertThat(RuntimeHintsPredicates.reflection().onType(SampleService.class)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleService.class, "post")).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onType(Request.class)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(Request.class, "getMessage")).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(Request.class, "setMessage")).accepts(hints);
assertThat(reflection().onType(SampleService.class)).accepts(hints);
assertThat(reflection().onMethod(SampleService.class, "post")).accepts(hints);
assertThat(reflection().onType(Request.class)).accepts(hints);
assertThat(reflection().onMethod(Request.class, "getMessage")).accepts(hints);
assertThat(reflection().onMethod(Request.class, "setMessage")).accepts(hints);
}