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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
package org.robolectric.internal.bytecode;
import java.lang.reflect.InvocationTargetException;
import org.robolectric.internal.IShadow;
import org.robolectric.util.ReflectionHelpers;
public class ShadowImpl implements IShadow {
private final ProxyMaker proxyMaker = new ProxyMaker(this::directMethodName);
@Override
@SuppressWarnings("TypeParameterUnusedInFormals")
public <T> T extract(Object instance) {
return (T) ((ShadowedObject) instance).$$robo$getData();
}
@Override public <T> T newInstanceOf(Class<T> clazz) {
return ReflectionHelpers.callConstructor(clazz);
}
@Override
public <T> T newInstance(Class<T> clazz, Class<?>[] parameterTypes, Object[] params) {
return ReflectionHelpers.callConstructor(clazz, ReflectionHelpers.ClassParameter.fromComponentLists(parameterTypes, params));
}
/**
* Returns a proxy object that invokes the original $$robo$$-prefixed methods for {@code
* shadowedObject}.
*
* @deprecated This is incompatible with JDK17+. Use a {@link
* org.robolectric.util.reflector.Reflector} interface with {@link
* org.robolectric.util.reflector.Direct}.
*/
@Deprecated
@Override
public <T> T directlyOn(T shadowedObject, Class<T> clazz) {
return createProxy(shadowedObject, clazz);
}
@Override
@SuppressWarnings(value = {"unchecked", "TypeParameterUnusedInFormals"})
public <R> R directlyOn(
Object shadowedObject,
String clazzName,
String methodName,
ReflectionHelpers.ClassParameter<?>... paramValues) {
try {
Class<Object> aClass =
(Class<Object>) shadowedObject.getClass().getClassLoader().loadClass(clazzName);
return directlyOn(shadowedObject, aClass, methodName, paramValues);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
@Override
@SuppressWarnings(value = {"unchecked", "TypeParameterUnusedInFormals"})
public <R, T> R directlyOn(
T shadowedObject,
Class<T> clazz,
String methodName,
ReflectionHelpers.ClassParameter<?>... paramValues) {
String directMethodName = directMethodName(clazz.getName(), methodName);
return (R)
ReflectionHelpers.callInstanceMethod(clazz, shadowedObject, directMethodName, paramValues);
}
@Override
@SuppressWarnings(value = {"unchecked", "TypeParameterUnusedInFormals"})
public <R, T> R directlyOn(
Class<T> clazz, String methodName, ReflectionHelpers.ClassParameter<?>... paramValues) {
String directMethodName = directMethodName(clazz.getName(), methodName);
return (R) ReflectionHelpers.callStaticMethod(clazz, directMethodName, paramValues);
}
private <T> T createProxy(T shadowedObject, Class<T> clazz) {
return proxyMaker.createProxy(clazz, shadowedObject);
}
@Override
@SuppressWarnings(value = {"unchecked", "TypeParameterUnusedInFormals"})
public <R> R invokeConstructor(
Class<? extends R> clazz, R instance, ReflectionHelpers.ClassParameter<?>... paramValues) {
String directMethodName =
directMethodName(clazz.getName(), ShadowConstants.CONSTRUCTOR_METHOD_NAME);
return (R) ReflectionHelpers.callInstanceMethod(clazz, instance, directMethodName, paramValues);
}
@Override
public String directMethodName(String className, String methodName) {
return ShadowConstants.ROBO_PREFIX
+ className.replace('.', '_').replace('$', '_')
+ "$" + methodName;
}
@Override
public String directNativeMethodName(String className, String methodName) {
return ShadowConstants.ROBO_PREFIX + methodName + "$nativeBinding";
}
@Override
public void directInitialize(Class<?> clazz) {
try {
RobolectricInternals.performStaticInitialization(clazz);
} catch (InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException("failed to initialize " + clazz, e);
}
}
}
|