14 changed files with 457 additions and 1 deletions
@ -0,0 +1,37 @@ |
|||||||
|
/* |
||||||
|
* 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); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,95 @@ |
|||||||
|
/* |
||||||
|
* 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.aot.hint.support; |
||||||
|
|
||||||
|
import java.util.function.Consumer; |
||||||
|
|
||||||
|
import org.springframework.aot.hint.MemberCategory; |
||||||
|
import org.springframework.aot.hint.RuntimeHints; |
||||||
|
import org.springframework.aot.hint.TypeHint; |
||||||
|
import org.springframework.aot.hint.TypeHint.Builder; |
||||||
|
import org.springframework.core.annotation.AliasFor; |
||||||
|
import org.springframework.core.annotation.MergedAnnotation; |
||||||
|
|
||||||
|
/** |
||||||
|
* Utility methods for runtime hints support code. |
||||||
|
* |
||||||
|
* @author Stephane Nicoll |
||||||
|
* @author Sam Brannen |
||||||
|
* @since 6.0 |
||||||
|
*/ |
||||||
|
public abstract class RuntimeHintsUtils { |
||||||
|
|
||||||
|
/** |
||||||
|
* A {@link TypeHint} customizer suitable for an annotation. Make sure |
||||||
|
* that its attributes are visible. |
||||||
|
* @deprecated as annotation attributes are visible without additional hints |
||||||
|
*/ |
||||||
|
@Deprecated |
||||||
|
public static final Consumer<Builder> ANNOTATION_HINT = hint -> |
||||||
|
hint.withMembers(MemberCategory.INVOKE_DECLARED_METHODS); |
||||||
|
|
||||||
|
/** |
||||||
|
* Register the necessary hints so that the specified annotation is visible |
||||||
|
* at runtime. |
||||||
|
* @param hints the {@link RuntimeHints} instance to use |
||||||
|
* @param annotationType the annotation type |
||||||
|
* @deprecated For removal prior to Spring Framework 6.0 |
||||||
|
*/ |
||||||
|
@Deprecated |
||||||
|
public static void registerAnnotation(RuntimeHints hints, Class<?> annotationType) { |
||||||
|
registerSynthesizedAnnotation(hints, annotationType); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Register the necessary hints so that the specified annotation can be |
||||||
|
* synthesized at runtime if necessary. Such hints are usually required |
||||||
|
* if any of the following apply: |
||||||
|
* <ul> |
||||||
|
* <li>Use {@link AliasFor} for local aliases</li> |
||||||
|
* <li>Has a meta-annotation that uses {@link AliasFor} for attribute overrides</li> |
||||||
|
* <li>Has nested annotations or arrays of annotations that are synthesizable</li> |
||||||
|
* </ul> |
||||||
|
* Consider using {@link #registerAnnotationIfNecessary(RuntimeHints, MergedAnnotation)} |
||||||
|
* that determines if the hints are required. |
||||||
|
* @param hints the {@link RuntimeHints} instance to use |
||||||
|
* @param annotationType the annotation type |
||||||
|
* @deprecated For removal prior to Spring Framework 6.0 |
||||||
|
*/ |
||||||
|
@Deprecated |
||||||
|
@SuppressWarnings("deprecation") |
||||||
|
public static void registerSynthesizedAnnotation(RuntimeHints hints, Class<?> annotationType) { |
||||||
|
hints.proxies().registerJdkProxy(annotationType, |
||||||
|
org.springframework.core.annotation.SynthesizedAnnotation.class); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Determine if the specified annotation can be synthesized at runtime, and |
||||||
|
* register the necessary hints accordingly. |
||||||
|
* @param hints the {@link RuntimeHints} instance to use |
||||||
|
* @param annotation the annotation |
||||||
|
* @see #registerSynthesizedAnnotation(RuntimeHints, Class) |
||||||
|
* @deprecated For removal prior to Spring Framework 6.0 |
||||||
|
*/ |
||||||
|
@Deprecated |
||||||
|
public static void registerAnnotationIfNecessary(RuntimeHints hints, MergedAnnotation<?> annotation) { |
||||||
|
if (annotation.isSynthesizable()) { |
||||||
|
registerSynthesizedAnnotation(hints, annotation.getType()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,150 @@ |
|||||||
|
/* |
||||||
|
* 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.aot.hint.support; |
||||||
|
|
||||||
|
import java.lang.annotation.Retention; |
||||||
|
import java.lang.annotation.RetentionPolicy; |
||||||
|
import java.util.function.Consumer; |
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
import org.springframework.aot.hint.JdkProxyHint; |
||||||
|
import org.springframework.aot.hint.RuntimeHints; |
||||||
|
import org.springframework.aot.hint.TypeReference; |
||||||
|
import org.springframework.core.annotation.AliasFor; |
||||||
|
import org.springframework.core.annotation.MergedAnnotation; |
||||||
|
import org.springframework.core.annotation.MergedAnnotations; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests for {@link RuntimeHintsUtils}. |
||||||
|
* |
||||||
|
* @author Stephane Nicoll |
||||||
|
* @author Sam Brannen |
||||||
|
*/ |
||||||
|
class RuntimeHintsUtilsTests { |
||||||
|
|
||||||
|
private final RuntimeHints hints = new RuntimeHints(); |
||||||
|
|
||||||
|
@Test |
||||||
|
@SuppressWarnings("deprecation") |
||||||
|
void registerSynthesizedAnnotation() { |
||||||
|
RuntimeHintsUtils.registerSynthesizedAnnotation(this.hints, SampleInvoker.class); |
||||||
|
assertThat(this.hints.proxies().jdkProxies()).singleElement() |
||||||
|
.satisfies(annotationProxy(SampleInvoker.class)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
@SuppressWarnings("deprecation") |
||||||
|
void registerAnnotationIfNecessaryWithNonSynthesizedAnnotation() throws NoSuchFieldException { |
||||||
|
MergedAnnotation<SampleInvoker> annotation = MergedAnnotations |
||||||
|
.from(TestBean.class.getField("sampleInvoker")).get(SampleInvoker.class); |
||||||
|
RuntimeHintsUtils.registerAnnotationIfNecessary(this.hints, annotation); |
||||||
|
assertThat(this.hints.proxies().jdkProxies()).isEmpty(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
@SuppressWarnings("deprecation") |
||||||
|
void registerAnnotationIfNecessaryWithLocalAliases() throws NoSuchFieldException { |
||||||
|
MergedAnnotation<LocalMapping> annotation = MergedAnnotations |
||||||
|
.from(TestBean.class.getField("localMapping")).get(LocalMapping.class); |
||||||
|
RuntimeHintsUtils.registerAnnotationIfNecessary(this.hints, annotation); |
||||||
|
assertThat(this.hints.proxies().jdkProxies()).singleElement() |
||||||
|
.satisfies(annotationProxy(LocalMapping.class)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
@SuppressWarnings("deprecation") |
||||||
|
void registerAnnotationIfNecessaryWithMetaAttributeOverride() throws NoSuchFieldException { |
||||||
|
MergedAnnotation<SampleInvoker> annotation = MergedAnnotations |
||||||
|
.from(TestBean.class.getField("retryInvoker")).get(SampleInvoker.class); |
||||||
|
RuntimeHintsUtils.registerAnnotationIfNecessary(this.hints, annotation); |
||||||
|
assertThat(this.hints.proxies().jdkProxies()).singleElement() |
||||||
|
.satisfies(annotationProxy(SampleInvoker.class)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
@SuppressWarnings("deprecation") |
||||||
|
void registerAnnotationIfNecessaryWithSynthesizedAttribute() throws NoSuchFieldException { |
||||||
|
MergedAnnotation<RetryContainer> annotation = MergedAnnotations |
||||||
|
.from(TestBean.class.getField("retryContainer")).get(RetryContainer.class); |
||||||
|
RuntimeHintsUtils.registerAnnotationIfNecessary(this.hints, annotation); |
||||||
|
assertThat(this.hints.proxies().jdkProxies()).singleElement() |
||||||
|
.satisfies(annotationProxy(RetryContainer.class)); |
||||||
|
} |
||||||
|
|
||||||
|
@SuppressWarnings("deprecation") |
||||||
|
private Consumer<JdkProxyHint> annotationProxy(Class<?> type) { |
||||||
|
return jdkProxyHint -> assertThat(jdkProxyHint.getProxiedInterfaces()) |
||||||
|
.containsExactly(TypeReference.of(type), |
||||||
|
TypeReference.of(org.springframework.core.annotation.SynthesizedAnnotation.class)); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
static class TestBean { |
||||||
|
|
||||||
|
@SampleInvoker |
||||||
|
public String sampleInvoker; |
||||||
|
|
||||||
|
@LocalMapping |
||||||
|
public String localMapping; |
||||||
|
|
||||||
|
@RetryInvoker |
||||||
|
public String retryInvoker; |
||||||
|
|
||||||
|
@RetryContainer(retry = @RetryInvoker(3)) |
||||||
|
public String retryContainer; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME) |
||||||
|
@interface LocalMapping { |
||||||
|
|
||||||
|
@AliasFor("retries") |
||||||
|
int value() default 0; |
||||||
|
|
||||||
|
@AliasFor("value") |
||||||
|
int retries() default 0; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME) |
||||||
|
@interface SampleInvoker { |
||||||
|
|
||||||
|
int retries() default 0; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME) |
||||||
|
@SampleInvoker |
||||||
|
@interface RetryInvoker { |
||||||
|
|
||||||
|
@AliasFor(attribute = "retries", annotation = SampleInvoker.class) |
||||||
|
int value() default 1; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME) |
||||||
|
@interface RetryContainer { |
||||||
|
|
||||||
|
RetryInvoker retry(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,43 @@ |
|||||||
|
/* |
||||||
|
* 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)); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,38 @@ |
|||||||
|
/* |
||||||
|
* 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); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,48 @@ |
|||||||
|
/* |
||||||
|
* 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)); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue