Skip to content

Commit 3923150

Browse files
committed
Polish "Use proper return type in AsyncExecutionInterceptor"
See gh-33957
1 parent 8eb2445 commit 3923150

File tree

2 files changed

+21
-33
lines changed

2 files changed

+21
-33
lines changed

spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncExecutionInterceptor.java

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.aop.interceptor;
1818

1919
import java.lang.reflect.Method;
20-
import java.lang.reflect.Modifier;
2120
import java.util.concurrent.Callable;
2221
import java.util.concurrent.ExecutionException;
2322
import java.util.concurrent.Executor;
@@ -61,7 +60,6 @@
6160
* @author Juergen Hoeller
6261
* @author Chris Beams
6362
* @author Stephane Nicoll
64-
* @author Bao Ngo
6563
* @since 3.0
6664
* @see org.springframework.scheduling.annotation.Async
6765
* @see org.springframework.scheduling.annotation.AsyncAnnotationAdvisor
@@ -127,16 +125,7 @@ public Object invoke(final MethodInvocation invocation) throws Throwable {
127125
return null;
128126
};
129127

130-
return doSubmit( task, executor, determineReturnType( invocation, userMethod ) );
131-
}
132-
133-
private static Class<?> determineReturnType(MethodInvocation invocation, Method userMethod) {
134-
Method originalMethod = invocation.getMethod();
135-
if( Modifier.isAbstract( originalMethod.getModifiers() ) ) {
136-
return userMethod.getReturnType();
137-
}
138-
139-
return originalMethod.getReturnType();
128+
return doSubmit(task, executor, userMethod.getReturnType());
140129
}
141130

142131
/**

spring-aop/src/test/java/org/springframework/aop/interceptor/AsyncExecutionInterceptorTests.java

+19-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,32 +43,31 @@
4343
class AsyncExecutionInterceptorTests {
4444

4545
@Test
46-
public void testInvokeOnInterface() throws Throwable {
47-
AsyncExecutionInterceptor interceptor = spy( new AsyncExecutionInterceptor( null ) );
48-
Impl impl = new Impl();
49-
ArgumentCaptor<Class<?>> classArgumentCaptor = ArgumentCaptor.forClass( Class.class );
46+
@SuppressWarnings("unchecked")
47+
void invokeOnInterfaceWithGeneric() throws Throwable {
48+
AsyncExecutionInterceptor interceptor = spy(new AsyncExecutionInterceptor(null));
49+
FutureRunner impl = new FutureRunner();
5050
MethodInvocation mi = mock();
51-
given( mi.getThis() ).willReturn( impl );
52-
given( mi.getMethod() ).willReturn( I.class.getMethod( "doSomeThing" ) );
53-
interceptor.invoke( mi );
54-
verify( interceptor ).doSubmit(
55-
any( Callable.class ),
56-
any( AsyncTaskExecutor.class ),
57-
classArgumentCaptor.capture()
58-
);
59-
assertThat( classArgumentCaptor.getValue() ).isEqualTo( Future.class );
51+
given(mi.getThis()).willReturn(impl);
52+
given(mi.getMethod()).willReturn(GenericRunner.class.getMethod("run"));
53+
54+
interceptor.invoke(mi);
55+
ArgumentCaptor<Class<?>> classArgumentCaptor = ArgumentCaptor.forClass(Class.class);
56+
verify(interceptor).doSubmit(any(Callable.class), any(AsyncTaskExecutor.class), classArgumentCaptor.capture());
57+
assertThat(classArgumentCaptor.getValue()).isEqualTo(Future.class);
6058
}
6159

6260

63-
private interface I<O> {
64-
O doSomeThing();
61+
interface GenericRunner<O> {
62+
63+
O run();
6564
}
6665

67-
private static final class Impl implements I<Future<Void>> {
66+
static class FutureRunner implements GenericRunner<Future<Void>> {
6867
@Override
69-
public Future<Void> doSomeThing() {
70-
return CompletableFuture.runAsync( () -> {
71-
} );
68+
public Future<Void> run() {
69+
return CompletableFuture.runAsync(() -> {
70+
});
7271
}
7372
}
7473
}

0 commit comments

Comments
 (0)