mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-25 08:19:01 +00:00
Merge branch '6.2.x'
This commit is contained in:
+116
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright 2002-present 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.core.annotation;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link AnnotatedMethod}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 6.2.11
|
||||
*/
|
||||
class AnnotatedMethodTests {
|
||||
|
||||
@Test
|
||||
void shouldFindAnnotationOnMethodInGenericAbstractSuperclass() {
|
||||
Method processTwo = getMethod("processTwo", String.class);
|
||||
|
||||
AnnotatedMethod annotatedMethod = new AnnotatedMethod(processTwo);
|
||||
|
||||
assertThat(annotatedMethod.hasMethodAnnotation(Handler.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFindAnnotationOnMethodInGenericInterface() {
|
||||
Method processOneAndTwo = getMethod("processOneAndTwo", Long.class, Object.class);
|
||||
|
||||
AnnotatedMethod annotatedMethod = new AnnotatedMethod(processOneAndTwo);
|
||||
|
||||
assertThat(annotatedMethod.hasMethodAnnotation(Handler.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFindAnnotationOnMethodParameterInGenericAbstractSuperclass() {
|
||||
Method processTwo = getMethod("processTwo", String.class);
|
||||
|
||||
AnnotatedMethod annotatedMethod = new AnnotatedMethod(processTwo);
|
||||
MethodParameter[] methodParameters = annotatedMethod.getMethodParameters();
|
||||
|
||||
assertThat(methodParameters).hasSize(1);
|
||||
assertThat(methodParameters[0].hasParameterAnnotation(Param.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFindAnnotationOnMethodParameterInGenericInterface() {
|
||||
Method processOneAndTwo = getMethod("processOneAndTwo", Long.class, Object.class);
|
||||
|
||||
AnnotatedMethod annotatedMethod = new AnnotatedMethod(processOneAndTwo);
|
||||
MethodParameter[] methodParameters = annotatedMethod.getMethodParameters();
|
||||
|
||||
assertThat(methodParameters).hasSize(2);
|
||||
assertThat(methodParameters[0].hasParameterAnnotation(Param.class)).isFalse();
|
||||
assertThat(methodParameters[1].hasParameterAnnotation(Param.class)).isTrue();
|
||||
}
|
||||
|
||||
|
||||
private static Method getMethod(String name, Class<?>...parameterTypes) {
|
||||
return ClassUtils.getMethod(GenericInterfaceImpl.class, name, parameterTypes);
|
||||
}
|
||||
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface Handler {
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface Param {
|
||||
}
|
||||
|
||||
interface GenericInterface<A, B> {
|
||||
|
||||
@Handler
|
||||
void processOneAndTwo(A value1, @Param B value2);
|
||||
}
|
||||
|
||||
abstract static class GenericAbstractSuperclass<C> implements GenericInterface<Long, C> {
|
||||
|
||||
@Override
|
||||
public void processOneAndTwo(Long value1, C value2) {
|
||||
}
|
||||
|
||||
@Handler
|
||||
public abstract void processTwo(@Param C value);
|
||||
}
|
||||
|
||||
static class GenericInterfaceImpl extends GenericAbstractSuperclass<String> {
|
||||
|
||||
@Override
|
||||
public void processTwo(String value) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+29
@@ -946,6 +946,15 @@ class MergedAnnotationsTests {
|
||||
Order.class).getDistance()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getFromMethodWithUnresolvedGenericsInGenericTypeHierarchy() {
|
||||
// The following method is GenericAbstractSuperclass.processOneAndTwo(java.lang.Long, C),
|
||||
// where 'C' is an unresolved generic, for which ResolvableType.resolve() returns null.
|
||||
Method method = ClassUtils.getMethod(GenericInterfaceImpl.class, "processOneAndTwo", Long.class, Object.class);
|
||||
assertThat(MergedAnnotations.from(method, SearchStrategy.TYPE_HIERARCHY)
|
||||
.get(Transactional.class).isDirectlyPresent()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getFromMethodWithInterfaceOnSuper() throws Exception {
|
||||
Method method = SubOfImplementsInterfaceWithAnnotatedMethod.class.getMethod("foo");
|
||||
@@ -3014,6 +3023,26 @@ class MergedAnnotationsTests {
|
||||
}
|
||||
}
|
||||
|
||||
interface GenericInterface<A, B> {
|
||||
|
||||
@Transactional
|
||||
void processOneAndTwo(A value1, B value2);
|
||||
}
|
||||
|
||||
abstract static class GenericAbstractSuperclass<C> implements GenericInterface<Long, C> {
|
||||
|
||||
@Override
|
||||
public void processOneAndTwo(Long value1, C value2) {
|
||||
}
|
||||
}
|
||||
|
||||
static class GenericInterfaceImpl extends GenericAbstractSuperclass<String> {
|
||||
// The compiler does not require us to declare a concrete
|
||||
// processOneAndTwo(Long, String) method, and we intentionally
|
||||
// do not declare one here.
|
||||
}
|
||||
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Inherited
|
||||
@interface MyRepeatableContainer {
|
||||
|
||||
Reference in New Issue
Block a user