Polishing
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user