-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
KotlinInlineClassUtil.java
77 lines (67 loc) · 3 KB
/
KotlinInlineClassUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Copyright (c) 2021 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.util;
import org.mockito.internal.exceptions.Reporter;
import org.mockito.internal.stubbing.answers.InvocationInfo;
import org.mockito.invocation.InvocationOnMock;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class KotlinInlineClassUtil {
private static Class<Annotation> jvmInlineAnnotation;
static {
try {
jvmInlineAnnotation = (Class<Annotation>) Class.forName("kotlin.jvm.JvmInline");
} catch (ClassNotFoundException e) {
// Do nothing: kotlin is pre 1.5.0
}
}
// When mocking function, returning inline class, its return type is
// underlying type.
// So, `thenReturn` calls fails, because of non-compatible types.
public static boolean isInlineClassWithAssignableUnderlyingType(
Class<?> inlineClass, Class<?> underlyingType) {
try {
// Since 1.5.0 inline classes have @JvmInline annotation
if (jvmInlineAnnotation == null
|| !inlineClass.isAnnotationPresent(jvmInlineAnnotation)) return false;
// All inline classes have 'box-impl' method, which accepts
// underlying type and returns inline class.
// Make sure that the current inline class is also the class that is compatible with the
// underlying type.
// If we don't make this check, then we would potentially pass a mock of inline type A
// into a method
// that accepts inline type B.
Object ignored = inlineClass.getDeclaredMethod("box-impl", underlyingType);
return true;
} catch (NoSuchMethodException e) {
return false;
}
}
private static Object unboxInlineClassIfPossible(Object boxedValue) {
Class<?> inlineClass = boxedValue.getClass();
try {
Method unboxImpl = inlineClass.getDeclaredMethod("unbox-impl");
return unboxImpl.invoke(boxedValue);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw Reporter.inlineClassWithoutUnboxImpl(inlineClass, e);
}
}
public static Object unboxUnderlyingValueIfNeeded(InvocationOnMock invocation, Object value) {
// Short path - Kotlin 1.5+ is not present.
if (value == null || jvmInlineAnnotation == null) {
return value;
}
Class<?> valueType = value.getClass();
InvocationInfo invocationInfo = new InvocationInfo(invocation);
Class<?> returnType = invocationInfo.getMethod().getReturnType();
if (valueType.isAssignableFrom(returnType)) return value;
if (isInlineClassWithAssignableUnderlyingType(valueType, returnType)) {
return unboxInlineClassIfPossible(value);
} else {
return value;
}
}
}