blob: ab3c6e3d53f4e589221f12e6507cd1ddb002aaca (
plain)
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
|
package org.robolectric.shadows;
import static android.os.Build.VERSION_CODES.O;
import static org.robolectric.util.reflector.Reflector.reflector;
import libcore.util.NativeAllocationRegistry;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.nativeruntime.NativeAllocationRegistryNatives;
import org.robolectric.shadows.ShadowNativeAllocationRegistry.Picker;
import org.robolectric.util.reflector.Accessor;
import org.robolectric.util.reflector.Direct;
import org.robolectric.util.reflector.ForType;
/** Shadow for {@link NativeAllocationRegistry} that is backed by native code */
@Implements(
value = NativeAllocationRegistry.class,
minSdk = O,
isInAndroidSdk = false,
shadowPicker = Picker.class)
public class ShadowNativeAllocationRegistry {
@RealObject protected NativeAllocationRegistry realNativeAllocationRegistry;
@Implementation
protected Runnable registerNativeAllocation(Object referent, long nativePtr) {
// Avoid registering native allocations for classes where native methods are no-ops (like
// Binder), or for classes that simulate native pointers (like binary resources) but don't
// actually use native libraries.
if (nativePtr != 0 && hasValidFreeFunction()) {
return reflector(NativeAllocationRegistryReflector.class, realNativeAllocationRegistry)
.registerNativeAllocation(referent, nativePtr);
} else {
return () -> {};
}
}
private boolean hasValidFreeFunction() {
return reflector(NativeAllocationRegistryReflector.class, realNativeAllocationRegistry)
.getFreeFunction()
!= 0;
}
@Implementation
protected static void applyFreeFunction(long freeFunction, long nativePtr) {
NativeAllocationRegistryNatives.applyFreeFunction(freeFunction, nativePtr);
}
@ForType(NativeAllocationRegistry.class)
interface NativeAllocationRegistryReflector {
@Direct
Runnable registerNativeAllocation(Object referent, long nativePtr);
@Accessor("freeFunction")
long getFreeFunction();
}
/** Shadow picker for {@link NativeAllocationRegistry}. */
public static final class Picker extends GraphicsShadowPicker<Object> {
public Picker() {
super(ShadowNoopNativeAllocationRegistry.class, ShadowNativeAllocationRegistry.class);
}
}
}
|