16
16
package org .springframework .data .projection ;
17
17
18
18
import java .lang .reflect .Array ;
19
+ import java .lang .reflect .Method ;
19
20
import java .util .Arrays ;
20
21
import java .util .Collection ;
21
22
import java .util .Collections ;
22
23
import java .util .List ;
23
24
import java .util .Map ;
24
25
import java .util .Map .Entry ;
25
26
27
+ import kotlin .reflect .KFunction ;
26
28
import org .aopalliance .intercept .MethodInterceptor ;
27
29
import org .aopalliance .intercept .MethodInvocation ;
28
30
import org .springframework .core .CollectionFactory ;
31
+ import org .springframework .core .KotlinDetector ;
29
32
import org .springframework .core .convert .ConversionService ;
33
+ import org .springframework .data .util .KotlinReflectionUtils ;
30
34
import org .springframework .data .util .NullableWrapper ;
31
35
import org .springframework .data .util .NullableWrapperConverters ;
32
36
import org .springframework .data .util .TypeInformation ;
44
48
* @author Mark Paluch
45
49
* @author Christoph Strobl
46
50
* @author Johannes Englmeier
51
+ * @author Yanming Zhou
47
52
* @since 1.10
48
53
*/
49
54
class ProjectingMethodInterceptor implements MethodInterceptor {
@@ -64,11 +69,13 @@ class ProjectingMethodInterceptor implements MethodInterceptor {
64
69
@ Override
65
70
public Object invoke (@ SuppressWarnings ("null" ) @ NonNull MethodInvocation invocation ) throws Throwable {
66
71
67
- TypeInformation <?> type = TypeInformation .fromReturnTypeOf (invocation .getMethod ());
72
+ Method method = invocation .getMethod ();
73
+ TypeInformation <?> type = TypeInformation .fromReturnTypeOf (method );
68
74
TypeInformation <?> resultType = type ;
69
75
TypeInformation <?> typeToReturn = type ;
70
76
71
77
Object result = delegate .invoke (invocation );
78
+
72
79
boolean applyWrapper = false ;
73
80
74
81
if (NullableWrapperConverters .supports (type .getType ())
@@ -83,6 +90,14 @@ public Object invoke(@SuppressWarnings("null") @NonNull MethodInvocation invocat
83
90
return conversionService .convert (new NullableWrapper (result ), typeToReturn .getType ());
84
91
}
85
92
93
+ if (result == null ) {
94
+ KFunction <?> function = KotlinDetector .isKotlinType (method .getDeclaringClass ()) ?
95
+ KotlinReflectionUtils .findKotlinFunction (method ) : null ;
96
+ if (function != null && !function .getReturnType ().isMarkedNullable ()) {
97
+ throw new IllegalArgumentException ("Kotlin function '%s' requires non-null return value" .formatted (method .toString ()));
98
+ }
99
+ }
100
+
86
101
return result ;
87
102
}
88
103
0 commit comments