Browse Source

Stop using RuntimeHintsUtils

Due to gh-29053, we can stop using RuntimeHintsUtils to
register SynthesizedAnnotation proxies.

Closes gh-29059
pull/29130/head
Sébastien Deleuze 4 years ago
parent
commit
9cfe79186d
  1. 37
      spring-beans/src/main/java/org/springframework/beans/factory/annotation/BeanFactoryAnnotationsRuntimeHints.java
  2. 1
      spring-beans/src/main/resources/META-INF/spring/aot.factories
  3. 13
      spring-core/src/main/java/org/springframework/aot/hint/annotation/ReflectiveRuntimeHintsRegistrar.java
  4. 17
      spring-core/src/test/java/org/springframework/aot/hint/annotation/ReflectiveRuntimeHintsRegistrarTests.java
  5. 43
      spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/MessagingAnnotationsRuntimeHints.java
  6. 38
      spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/SimpAnnotationsRuntimeHints.java
  7. 4
      spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java
  8. 3
      spring-test/src/test/java/org/springframework/test/context/aot/TestAotProcessorTests.java
  9. 3
      spring-tx/src/main/java/org/springframework/transaction/annotation/TransactionRuntimeHints.java
  10. 48
      spring-web/src/main/java/org/springframework/web/bind/annotation/WebAnnotationsRuntimeHintsRegistrar.java
  11. 3
      spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java
  12. 3
      spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java

37
spring-beans/src/main/java/org/springframework/beans/factory/annotation/BeanFactoryAnnotationsRuntimeHints.java

@ -1,37 +0,0 @@
/*
* Copyright 2002-2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.annotation;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.support.RuntimeHintsUtils;
import org.springframework.lang.Nullable;
/**
* {@link RuntimeHintsRegistrar} implementation for bean factory annotations.
*
* @author Stephane Nicoll
* @since 6.0
*/
class BeanFactoryAnnotationsRuntimeHints implements RuntimeHintsRegistrar {
@Override
@SuppressWarnings("deprecation")
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
RuntimeHintsUtils.registerSynthesizedAnnotation(hints, Qualifier.class);
}
}

1
spring-beans/src/main/resources/META-INF/spring/aot.factories

@ -1,5 +1,4 @@
org.springframework.aot.hint.RuntimeHintsRegistrar=\ org.springframework.aot.hint.RuntimeHintsRegistrar=\
org.springframework.beans.factory.annotation.BeanFactoryAnnotationsRuntimeHints,\
org.springframework.beans.factory.annotation.JakartaAnnotationsRuntimeHints org.springframework.beans.factory.annotation.JakartaAnnotationsRuntimeHints
org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor=\ org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor=\

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

@ -28,8 +28,6 @@ import java.util.function.Consumer;
import org.springframework.aot.hint.ReflectionHints; import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.support.RuntimeHintsUtils;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations; import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
@ -62,20 +60,9 @@ public class ReflectiveRuntimeHintsRegistrar {
entries.forEach(entry -> { entries.forEach(entry -> {
AnnotatedElement element = entry.element(); AnnotatedElement element = entry.element();
entry.processor().registerReflectionHints(runtimeHints.reflection(), element); entry.processor().registerReflectionHints(runtimeHints.reflection(), element);
registerAnnotationIfNecessary(runtimeHints, element);
}); });
} }
@SuppressWarnings("deprecation")
private void registerAnnotationIfNecessary(RuntimeHints hints, AnnotatedElement element) {
MergedAnnotation<Reflective> reflectiveAnnotation = MergedAnnotations.from(element)
.get(Reflective.class);
MergedAnnotation<?> metaSource = reflectiveAnnotation.getMetaSource();
if (metaSource != null) {
RuntimeHintsUtils.registerAnnotationIfNecessary(hints, metaSource);
}
}
private void processType(Set<Entry> entries, Class<?> typeToProcess) { private void processType(Set<Entry> entries, Class<?> typeToProcess) {
if (isReflective(typeToProcess)) { if (isReflective(typeToProcess)) {
entries.add(createEntry(typeToProcess)); entries.add(createEntry(typeToProcess));

17
spring-core/src/test/java/org/springframework/aot/hint/annotation/ReflectiveRuntimeHintsRegistrarTests.java

@ -86,23 +86,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
.satisfies(methodHint -> assertThat(methodHint.getName()).isEqualTo("managed"))); .satisfies(methodHint -> assertThat(methodHint.getName()).isEqualTo("managed")));
} }
@Test
void shouldNotRegisterAnnotationProxyIfNotNeeded() {
process(SampleMethodMetaAnnotatedBean.class);
RuntimeHints runtimeHints = this.runtimeHints;
assertThat(runtimeHints.proxies().jdkProxies()).isEmpty();
}
@Test
@SuppressWarnings("deprecation")
void shouldRegisterAnnotationProxy() {
process(SampleMethodMetaAnnotatedBeanWithAlias.class);
RuntimeHints runtimeHints = this.runtimeHints;
assertThat(RuntimeHintsPredicates.proxies()
.forInterfaces(SampleInvoker.class, org.springframework.core.annotation.SynthesizedAnnotation.class))
.accepts(runtimeHints);
}
@Test @Test
void shouldProcessAnnotationOnInterface() { void shouldProcessAnnotationOnInterface() {
process(SampleMethodAnnotatedBeanWithInterface.class); process(SampleMethodAnnotatedBeanWithInterface.class);

43
spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/MessagingAnnotationsRuntimeHints.java

@ -1,43 +0,0 @@
/*
* Copyright 2002-2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.messaging.handler.annotation;
import java.util.stream.Stream;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.support.RuntimeHintsUtils;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
/**
* {@link RuntimeHintsRegistrar} implementation that makes messaging
* annotations available at runtime.
*
* @author Sebastien Deleuze
* @since 6.0
*/
public class MessagingAnnotationsRuntimeHints implements RuntimeHintsRegistrar {
@Override
@SuppressWarnings("deprecation")
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
Stream.of(Controller.class, Header.class, Headers.class, Payload.class).forEach(annotationType ->
RuntimeHintsUtils.registerSynthesizedAnnotation(hints, annotationType));
}
}

38
spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/SimpAnnotationsRuntimeHints.java

@ -1,38 +0,0 @@
/*
* Copyright 2002-2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.messaging.simp.annotation;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.support.RuntimeHintsUtils;
/**
* {@link RuntimeHintsRegistrar} implementation that makes Simp annotations
* available at runtime.
*
* @author Sebastien Deleuze
* @since 6.0
*/
public class SimpAnnotationsRuntimeHints implements RuntimeHintsRegistrar {
@Override
@SuppressWarnings("deprecation")
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
RuntimeHintsUtils.registerSynthesizedAnnotation(hints, SendToUser.class);
}
}

4
spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java

@ -27,7 +27,6 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.context.event.SmartApplicationListener; import org.springframework.context.event.SmartApplicationListener;
import org.springframework.core.task.TaskExecutor; import org.springframework.core.task.TaskExecutor;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
@ -41,12 +40,10 @@ import org.springframework.messaging.converter.KotlinSerializationJsonMessageCon
import org.springframework.messaging.converter.MappingJackson2MessageConverter; import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.converter.MessageConverter; import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.converter.StringMessageConverter; import org.springframework.messaging.converter.StringMessageConverter;
import org.springframework.messaging.handler.annotation.MessagingAnnotationsRuntimeHints;
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver; import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler; import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler;
import org.springframework.messaging.simp.SimpLogging; import org.springframework.messaging.simp.SimpLogging;
import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SimpAnnotationsRuntimeHints;
import org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler; import org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler;
import org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler; import org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler;
import org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler; import org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler;
@ -98,7 +95,6 @@ import org.springframework.validation.beanvalidation.OptionalValidatorFactoryBea
* @author Sebastien Deleuze * @author Sebastien Deleuze
* @since 4.0 * @since 4.0
*/ */
@ImportRuntimeHints({ MessagingAnnotationsRuntimeHints.class, SimpAnnotationsRuntimeHints.class })
public abstract class AbstractMessageBrokerConfiguration implements ApplicationContextAware { public abstract class AbstractMessageBrokerConfiguration implements ApplicationContextAware {
private static final String MVC_VALIDATOR_NAME = "mvcValidator"; private static final String MVC_VALIDATOR_NAME = "mvcValidator";

3
spring-test/src/test/java/org/springframework/test/context/aot/TestAotProcessorTests.java

@ -70,8 +70,7 @@ class TestAotProcessorTests extends AbstractAotTests {
assertThat(findFiles(resourceOutput)).contains( assertThat(findFiles(resourceOutput)).contains(
"META-INF/native-image/org.example/app-tests/reflect-config.json", "META-INF/native-image/org.example/app-tests/reflect-config.json",
"META-INF/native-image/org.example/app-tests/resource-config.json", "META-INF/native-image/org.example/app-tests/resource-config.json");
"META-INF/native-image/org.example/app-tests/proxy-config.json");
} }
private void copy(Class<?> testClass, Path destination) { private void copy(Class<?> testClass, Path destination) {

3
spring-tx/src/main/java/org/springframework/transaction/annotation/TransactionRuntimeHints.java

@ -21,7 +21,6 @@ import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar; import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeHint; import org.springframework.aot.hint.TypeHint;
import org.springframework.aot.hint.TypeReference; import org.springframework.aot.hint.TypeReference;
import org.springframework.aot.hint.support.RuntimeHintsUtils;
import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionDefinition;
/** /**
@ -35,9 +34,7 @@ import org.springframework.transaction.TransactionDefinition;
class TransactionRuntimeHints implements RuntimeHintsRegistrar { class TransactionRuntimeHints implements RuntimeHintsRegistrar {
@Override @Override
@SuppressWarnings("deprecation")
public void registerHints(RuntimeHints hints, ClassLoader classLoader) { public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
RuntimeHintsUtils.registerSynthesizedAnnotation(hints, Transactional.class);
hints.reflection().registerTypes(TypeReference.listOf( hints.reflection().registerTypes(TypeReference.listOf(
Isolation.class, Propagation.class, TransactionDefinition.class), Isolation.class, Propagation.class, TransactionDefinition.class),
TypeHint.builtWith(MemberCategory.DECLARED_FIELDS)); TypeHint.builtWith(MemberCategory.DECLARED_FIELDS));

48
spring-web/src/main/java/org/springframework/web/bind/annotation/WebAnnotationsRuntimeHintsRegistrar.java

@ -1,48 +0,0 @@
/*
* Copyright 2002-2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.bind.annotation;
import java.util.stream.Stream;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.support.RuntimeHintsUtils;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
/**
* {@link RuntimeHintsRegistrar} implementation that makes web binding
* annotations available at runtime.
*
* @author Stephane Nicoll
* @since 6.0
*/
public final class WebAnnotationsRuntimeHintsRegistrar implements RuntimeHintsRegistrar {
@Override
@SuppressWarnings("deprecation")
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
Stream.of(Controller.class, ControllerAdvice.class, CookieValue.class,
CrossOrigin.class, MatrixVariable.class, ModelAttribute.class,
PathVariable.class, RequestAttribute.class, RequestHeader.class,
RequestMapping.class, RequestParam.class, RequestPart.class,
ResponseStatus.class, SessionAttribute.class, SessionAttributes.class)
.forEach(annotationType ->
RuntimeHintsUtils.registerSynthesizedAnnotation(hints, annotationType));
}
}

3
spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java

@ -27,7 +27,6 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.core.ReactiveAdapterRegistry; import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
@ -46,7 +45,6 @@ import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator; import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean; import org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean;
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.WebAnnotationsRuntimeHintsRegistrar;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.reactive.DispatcherHandler; import org.springframework.web.reactive.DispatcherHandler;
@ -85,7 +83,6 @@ import org.springframework.web.server.i18n.LocaleContextResolver;
* @author Brian Clozel * @author Brian Clozel
* @since 5.0 * @since 5.0
*/ */
@ImportRuntimeHints(WebAnnotationsRuntimeHintsRegistrar.class)
public class WebFluxConfigurationSupport implements ApplicationContextAware { public class WebFluxConfigurationSupport implements ApplicationContextAware {
@Nullable @Nullable

3
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java

@ -32,7 +32,6 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.core.SpringProperties; import org.springframework.core.SpringProperties;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
@ -71,7 +70,6 @@ import org.springframework.validation.beanvalidation.OptionalValidatorFactoryBea
import org.springframework.web.HttpRequestHandler; import org.springframework.web.HttpRequestHandler;
import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.WebAnnotationsRuntimeHintsRegistrar;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.context.ServletContextAware; import org.springframework.web.context.ServletContextAware;
import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsConfiguration;
@ -190,7 +188,6 @@ import org.springframework.web.util.pattern.PathPatternParser;
* @see EnableWebMvc * @see EnableWebMvc
* @see WebMvcConfigurer * @see WebMvcConfigurer
*/ */
@ImportRuntimeHints(WebAnnotationsRuntimeHintsRegistrar.class)
public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware { public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {
/** /**

Loading…
Cancel
Save