Remove dead code that uses Plan based instrumentation (ClassHandler.Plan)
PiperOrigin-RevId: 474924840
diff --git a/robolectric/src/test/java/org/robolectric/internal/bytecode/ShadowWranglerUnitTest.java b/robolectric/src/test/java/org/robolectric/internal/bytecode/ShadowWranglerUnitTest.java
index 090b257..bbc3596 100644
--- a/robolectric/src/test/java/org/robolectric/internal/bytecode/ShadowWranglerUnitTest.java
+++ b/robolectric/src/test/java/org/robolectric/internal/bytecode/ShadowWranglerUnitTest.java
@@ -34,24 +34,28 @@
}
@Test
- public void getInterceptionHandler_whenCallIsNotRecognized_shouldReturnDoNothingHandler() throws Exception {
+ public void getInterceptionHandler_whenCallIsNotRecognized_shouldReturnDoNothingHandler()
+ throws Exception {
MethodSignature methodSignature = MethodSignature.parse("java/lang/Object/unknownMethod()V");
- Function<Object,Object> handler = interceptors.getInterceptionHandler(methodSignature);
+ Function<Object, Object> handler = interceptors.getInterceptionHandler(methodSignature);
assertThat(handler.call(null, null, new Object[0])).isNull();
}
@Test
- public void getInterceptionHandler_whenInterceptingElderOnLinkedHashMap_shouldReturnNonDoNothingHandler() throws Exception {
+ public void
+ getInterceptionHandler_whenInterceptingElderOnLinkedHashMap_shouldReturnNonDoNothingHandler()
+ throws Exception {
MethodSignature methodSignature =
MethodSignature.parse("java/util/LinkedHashMap/eldest()Ljava/lang/Object;");
- Function<Object,Object> handler = interceptors.getInterceptionHandler(methodSignature);
+ Function<Object, Object> handler = interceptors.getInterceptionHandler(methodSignature);
assertThat(handler).isNotSameInstanceAs(ShadowWrangler.DO_NOTHING_HANDLER);
}
@Test
- public void intercept_elderOnLinkedHashMapHandler_shouldReturnEldestMemberOfLinkedHashMap() throws Throwable {
+ public void intercept_elderOnLinkedHashMapHandler_shouldReturnEldestMemberOfLinkedHashMap()
+ throws Throwable {
LinkedHashMap<Integer, String> map = new LinkedHashMap<>(2);
map.put(1, "one");
map.put(2, "two");
@@ -80,69 +84,27 @@
}
@Test
- public void shadowClassWithSdkRange() throws Throwable {
- ShadowMap shadowMap = new ShadowMap.Builder().addShadowClasses(ShadowDummyClass.class).build();
- String methodName = internalName(DummyClass.class) + "/methodWithoutRange()V";
- assertThat(new ShadowWrangler(shadowMap, sdk18, interceptors).methodInvoked(methodName, false, DummyClass.class)).isNull();
- assertThat(new ShadowWrangler(shadowMap, sdk19, interceptors).methodInvoked(methodName, false, DummyClass.class).describe())
- .contains("ShadowDummyClass.methodWithoutRange()");
- assertThat(new ShadowWrangler(shadowMap, sdk23, interceptors).methodInvoked(methodName, false, DummyClass.class)).isNull();
- }
-
- @Test
- public void shadowMethodWithSdkRange() throws Throwable {
- ShadowMap shadowMap = new ShadowMap.Builder().addShadowClasses(ShadowDummyClass.class).build();
- String methodName = internalName(DummyClass.class) + "/methodFor20()V";
- assertThat(new ShadowWrangler(shadowMap, sdk19, interceptors).methodInvoked(methodName, false, DummyClass.class)).isNull();
- assertThat(new ShadowWrangler(shadowMap, sdk20, interceptors).methodInvoked(methodName, false, DummyClass.class).describe())
- .contains("ShadowDummyClass.methodFor20()");
- assertThat(new ShadowWrangler(shadowMap, sdk21, interceptors).methodInvoked(methodName, false, DummyClass.class)).isNull();
- }
-
- @Test
- public void shadowMethodWithMinSdk() throws Throwable {
- ShadowMap shadowMap = new ShadowMap.Builder().addShadowClasses(ShadowDummyClass.class).build();
- String methodName = internalName(DummyClass.class) + "/methodMin20()V";
- assertThat(new ShadowWrangler(shadowMap, sdk19, interceptors).methodInvoked(methodName, false, DummyClass.class)).isNull();
- assertThat(new ShadowWrangler(shadowMap, sdk20, interceptors).methodInvoked(methodName, false, DummyClass.class).describe())
- .contains("ShadowDummyClass.methodMin20()");
- assertThat(new ShadowWrangler(shadowMap, sdk21, interceptors).methodInvoked(methodName, false, DummyClass.class).describe())
- .contains("ShadowDummyClass.methodMin20()");
- }
-
- @Test
- public void shadowMethodWithMaxSdk() throws Throwable {
- ShadowMap shadowMap = new ShadowMap.Builder().addShadowClasses(ShadowDummyClass.class).build();
- String methodName = internalName(DummyClass.class) + "/methodMax20()V";
- assertThat(new ShadowWrangler(shadowMap, sdk19, interceptors).methodInvoked(methodName, false, DummyClass.class).describe())
- .contains("ShadowDummyClass.methodMax20()");
- assertThat(new ShadowWrangler(shadowMap, sdk20, interceptors).methodInvoked(methodName, false, DummyClass.class).describe())
- .contains("ShadowDummyClass.methodMax20()");
- assertThat(new ShadowWrangler(shadowMap, sdk21, interceptors).methodInvoked(methodName, false, DummyClass.class)).isNull();
- }
-
- @Test
- public void shadowConstructor() throws Throwable {
- ShadowMap shadowMap = new ShadowMap.Builder().addShadowClasses(ShadowDummyClass.class).build();
- String methodName = internalName(DummyClass.class) + "/__constructor__()V";
- assertThat(new ShadowWrangler(shadowMap, sdk19, interceptors).methodInvoked(methodName, false, DummyClass.class)).isNull();
- assertThat(new ShadowWrangler(shadowMap, sdk20, interceptors).methodInvoked(methodName, false, DummyClass.class).describe())
- .contains("ShadowDummyClass.__constructor__()");
- assertThat(new ShadowWrangler(shadowMap, sdk21, interceptors).methodInvoked(methodName, false, DummyClass.class)).isNull();
- }
-
- @Test
- public void whenChildShadowHasNarrowerSdk_createShadowFor_shouldReturnSuperShadowSometimes() throws Exception {
- ShadowMap shadowMap = new ShadowMap.Builder().addShadowClasses(ShadowDummyClass.class, ShadowChildOfDummyClass.class).build();
+ public void whenChildShadowHasNarrowerSdk_createShadowFor_shouldReturnSuperShadowSometimes()
+ throws Exception {
+ ShadowMap shadowMap =
+ new ShadowMap.Builder()
+ .addShadowClasses(ShadowDummyClass.class, ShadowChildOfDummyClass.class)
+ .build();
assertThat(
new ShadowWrangler(shadowMap, sdk18, interceptors)
.createShadowFor(new ChildOfDummyClass()))
.isSameInstanceAs(ShadowWrangler.NO_SHADOW);
- assertThat(new ShadowWrangler(shadowMap, sdk19, interceptors).createShadowFor(new ChildOfDummyClass()))
+ assertThat(
+ new ShadowWrangler(shadowMap, sdk19, interceptors)
+ .createShadowFor(new ChildOfDummyClass()))
.isInstanceOf(ShadowDummyClass.class);
- assertThat(new ShadowWrangler(shadowMap, sdk20, interceptors).createShadowFor(new ChildOfDummyClass()))
+ assertThat(
+ new ShadowWrangler(shadowMap, sdk20, interceptors)
+ .createShadowFor(new ChildOfDummyClass()))
.isInstanceOf(ShadowChildOfDummyClass.class);
- assertThat(new ShadowWrangler(shadowMap, sdk21, interceptors).createShadowFor(new ChildOfDummyClass()))
+ assertThat(
+ new ShadowWrangler(shadowMap, sdk21, interceptors)
+ .createShadowFor(new ChildOfDummyClass()))
.isInstanceOf(ShadowChildOfDummyClass.class);
assertThat(
new ShadowWrangler(shadowMap, sdk22, interceptors)
@@ -150,57 +112,31 @@
.isSameInstanceAs(ShadowWrangler.NO_SHADOW);
}
- @Test
- public void whenChildShadowHasNarrowerSdk_shouldCallAppropriateShadowMethod() throws Exception {
- ShadowMap shadowMap = new ShadowMap.Builder().addShadowClasses(ShadowDummyClass.class, ShadowChildOfDummyClass.class).build();
- String methodName = internalName(ChildOfDummyClass.class) + "/methodWithoutRange()V";
- assertThat(new ShadowWrangler(shadowMap, sdk19, interceptors).methodInvoked(methodName, false, ChildOfDummyClass.class))
- .isNull();
- assertThat(new ShadowWrangler(shadowMap, sdk20, interceptors).methodInvoked(methodName, false, ChildOfDummyClass.class).describe())
- .contains("ShadowChildOfDummyClass.methodWithoutRange()");
- assertThat(new ShadowWrangler(shadowMap, sdk21, interceptors).methodInvoked(methodName, false, ChildOfDummyClass.class).describe())
- .contains("ShadowChildOfDummyClass.methodWithoutRange()");
- }
-
- public static class DummyClass {
- }
+ public static class DummyClass {}
@Implements(value = DummyClass.class, minSdk = 19, maxSdk = 21)
public static class ShadowDummyClass {
@Implementation(minSdk = 20, maxSdk = 20)
- protected void __constructor__() {
- }
-
+ protected void __constructor__() {}
+
@Implementation
- protected void methodWithoutRange() {
- }
+ protected void methodWithoutRange() {}
@Implementation(minSdk = 20, maxSdk = 20)
- protected void methodFor20() {
- }
+ protected void methodFor20() {}
@Implementation(minSdk = 20)
- protected void methodMin20() {
- }
+ protected void methodMin20() {}
@Implementation(maxSdk = 20)
- protected void methodMax20() {
- }
+ protected void methodMax20() {}
}
- public static class ChildOfDummyClass extends DummyClass {
- }
+ public static class ChildOfDummyClass extends DummyClass {}
@Implements(value = ChildOfDummyClass.class, minSdk = 20, maxSdk = 21)
public static class ShadowChildOfDummyClass {
@Implementation
- protected void methodWithoutRange() {
- }
- }
-
- ///////////////////////
-
- private String internalName(Class clazz) {
- return clazz.getName().replaceAll("\\.", "/");
+ protected void methodWithoutRange() {}
}
}
diff --git a/sandbox/src/main/java/org/robolectric/internal/bytecode/ClassHandler.java b/sandbox/src/main/java/org/robolectric/internal/bytecode/ClassHandler.java
index 93f4083..73fc9e8 100644
--- a/sandbox/src/main/java/org/robolectric/internal/bytecode/ClassHandler.java
+++ b/sandbox/src/main/java/org/robolectric/internal/bytecode/ClassHandler.java
@@ -27,9 +27,6 @@
*
* <p>This happens before constructor code executes on the new instance.
*
- * <p>Implementations may return an object which will be associated with the new instance and
- * passed along on future calls to {@link #methodInvoked(String, boolean, Class)}.
- *
* @param instance the newly-created instance
* @return a data value to be associated with the new instance
* @see #getShadowCreator(Class) for newer JVMs
@@ -37,24 +34,6 @@
Object initializing(Object instance);
/**
- * Called by Robolectric when an instrumented method is invoked.
- *
- * <p>Implementations should return an {@link Plan}, which will be invoked with details about the
- * current instance and parameters.
- *
- * <p>Implementations may also return null, in which case the method's original code will be
- * executed.
- *
- * @param signature the JVM internal-format signature of the method being invoked (e.g. {@code
- * android/view/View/measure(II)V})
- * @param isStatic true if the method is static
- * @param theClass the class on which the method is declared
- * @return an execution plan, or null if the original method's code should be executed
- * @see #findShadowMethodHandle(Class, String, MethodType, boolean) for newer JVMs
- */
- Plan methodInvoked(String signature, boolean isStatic, Class<?> theClass);
-
- /**
* Called by Robolectric to determine how to create and initialize a shadow object when a new
* instance of an instrumented class has been instantiated. (but only on JVMs which support the
* {@code invokedynamic} instruction).
@@ -87,7 +66,6 @@
* @param methodType the method type
* @param isStatic true if the method is static
* @return a method handle to invoke, or null if the original method's code should be executed
- * @see #methodInvoked(String, boolean, Class) for older JVMs
* @see ShadowInvalidator for invalidating the returned {@link MethodHandle}
*/
MethodHandle findShadowMethodHandle(
@@ -120,14 +98,4 @@
* @return the stripped stack trace
*/
<T extends Throwable> T stripStackTrace(T throwable);
-
- /**
- * An execution plan, which can be invoked to satisfy a requested method call.
- */
- interface Plan {
-
- Object run(Object instance, Object[] params) throws Throwable;
-
- String describe();
- }
}
diff --git a/sandbox/src/main/java/org/robolectric/internal/bytecode/ClassInstrumentor.java b/sandbox/src/main/java/org/robolectric/internal/bytecode/ClassInstrumentor.java
index e30b1b7..1ba7356 100644
--- a/sandbox/src/main/java/org/robolectric/internal/bytecode/ClassInstrumentor.java
+++ b/sandbox/src/main/java/org/robolectric/internal/bytecode/ClassInstrumentor.java
@@ -303,57 +303,8 @@
}
/**
- * Constructors are instrumented as follows:
- *
- * <ul>
- * <li>Code other than a call to the superclass constructor is moved to a new method named
- * {@code __constructor__} with the same signature.
- * <li>The constructor is modified to call {@link ClassHandler#initializing(Object)} (or {@link
- * ClassHandler#getShadowCreator(Class)} for {@code invokedynamic} JVMs).
- * <li>The constructor is modified to then call {@link ClassHandler#methodInvoked(String,
- * boolean, Class)} (or {@link ClassHandler#findShadowMethodHandle(Class, String,
- * MethodType, boolean)} for {@code invokedynamic} JVMs) with the method name {@code
- * __constructor__} and the same parameter types.
- * </ul>
- *
- * Note that most code in the constructor will not be executed unless the {@link ClassHandler}
- * arranges for it to happen.
- *
- * <p>Given a constructor like this:
- *
- * <pre>
- * public ThisClass(String name, int size) {
- * super(name, someStaticMethod());
- * this.size = size;
- * }
- * </pre>
- *
- * ... generates code like this:
- *
- * <pre>
- * private $$robo$$__constructor__(String name, int size) {
- * this.size = size;
- * }
- *
- * private __constructor__(String name, int size) {
- * Plan plan = RobolectricInternals.methodInvoked(
- * "pkg/ThisClass/__constructor__(Ljava/lang/String;I)V", true, ThisClass.class);
- * if (plan != null) {
- * try {
- * plan.run(this, new Object[] {name, size});
- * } catch (Throwable t) {
- * throw RobolectricInternals.cleanStackTrace(t);
- * }
- * } else {
- * $$robo$$__constructor__(name, size);
- * }
- * }
- *
- * public ThisClass(String name, int size) {
- * super(name, someStaticMethod());
- * $$robo$init();
- * }
- * </pre>
+ * Constructors are instrumented as follows: TODO(slliu): Fill in constructor instrumentation
+ * directions
*
* @param method the constructor to instrument
*/
diff --git a/sandbox/src/main/java/org/robolectric/internal/bytecode/RobolectricInternals.java b/sandbox/src/main/java/org/robolectric/internal/bytecode/RobolectricInternals.java
index fc1e9ae..673f5c1 100644
--- a/sandbox/src/main/java/org/robolectric/internal/bytecode/RobolectricInternals.java
+++ b/sandbox/src/main/java/org/robolectric/internal/bytecode/RobolectricInternals.java
@@ -26,17 +26,13 @@
return classHandler.initializing(instance);
}
- @SuppressWarnings("UnusedDeclaration")
- public static ClassHandler.Plan methodInvoked(String signature, boolean isStatic, Class<?> theClass) {
- return classHandler.methodInvoked(signature, isStatic, theClass);
- }
-
public static MethodHandle getShadowCreator(Class<?> caller) {
return classHandler.getShadowCreator(caller);
}
- public static MethodHandle findShadowMethodHandle(Class<?> theClass, String name,
- MethodType methodType, boolean isStatic) throws IllegalAccessException {
+ public static MethodHandle findShadowMethodHandle(
+ Class<?> theClass, String name, MethodType methodType, boolean isStatic)
+ throws IllegalAccessException {
return classHandler.findShadowMethodHandle(theClass, name, methodType, isStatic);
}
@@ -45,7 +41,8 @@
return classHandler.stripStackTrace(exception);
}
- public static Object intercept(String signature, Object instance, Object[] params, Class theClass) throws Throwable {
+ public static Object intercept(String signature, Object instance, Object[] params, Class theClass)
+ throws Throwable {
try {
return classHandler.intercept(signature, instance, params, theClass);
} catch (java.lang.LinkageError e) {
diff --git a/sandbox/src/main/java/org/robolectric/internal/bytecode/ShadowWrangler.java b/sandbox/src/main/java/org/robolectric/internal/bytecode/ShadowWrangler.java
index f8a92e5..51953fd 100644
--- a/sandbox/src/main/java/org/robolectric/internal/bytecode/ShadowWrangler.java
+++ b/sandbox/src/main/java/org/robolectric/internal/bytecode/ShadowWrangler.java
@@ -18,10 +18,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
-import java.util.Collections;
-import java.util.LinkedHashMap;
import java.util.List;
-import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Priority;
import org.robolectric.annotation.RealObject;
@@ -55,19 +52,6 @@
return null;
}
};
- public static final Plan DO_NOTHING_PLAN =
- new Plan() {
- @Override
- public Object run(Object instance, Object[] params) throws Exception {
- return null;
- }
-
- @Override
- public String describe() {
- return "do nothing";
- }
- };
- public static final Plan CALL_REAL_CODE_PLAN = null;
public static final Method CALL_REAL_CODE = null;
public static final MethodHandle DO_NOTHING =
constant(Void.class, null).asType(methodType(void.class));
@@ -109,14 +93,6 @@
private final Interceptors interceptors;
private final ShadowMatcher shadowMatcher;
private final MethodHandle reflectorHandle;
- private final Map<String, Plan> planCache =
- Collections.synchronizedMap(
- new LinkedHashMap<String, Plan>() {
- @Override
- protected boolean removeEldestEntry(Map.Entry<String, Plan> eldest) {
- return size() > 500;
- }
- });
/** key is instrumented class */
private final ClassValueMap<ShadowInfo> cachedShadowInfos =
@@ -216,44 +192,6 @@
return createShadowFor(instance);
}
- @Override
- public Plan methodInvoked(String signature, boolean isStatic, Class<?> theClass) {
- Plan plan;
- if (planCache.containsKey(signature)) {
- plan = planCache.get(signature);
- } else {
- plan = calculatePlan(signature, isStatic, theClass);
- planCache.put(signature, plan);
- }
- return plan;
- }
-
- @SuppressWarnings("ReferenceEquality")
- private Plan calculatePlan(String signature, boolean isStatic, Class<?> definingClass) {
- return PerfStatsCollector.getInstance()
- .measure(
- "find shadow method",
- () -> {
- final ClassLoader classLoader = definingClass.getClassLoader();
- final InvocationProfile invocationProfile =
- new InvocationProfile(signature, isStatic, classLoader);
- try {
- Class<?>[] types = invocationProfile.getParamClasses(classLoader);
- Method shadowMethod =
- pickShadowMethod(definingClass, invocationProfile.methodName, types);
- if (shadowMethod == CALL_REAL_CODE) {
- return CALL_REAL_CODE_PLAN;
- } else if (shadowMethod == DO_NOTHING_METHOD) {
- return DO_NOTHING_PLAN;
- } else {
- return new ShadowMethodPlan(shadowMethod);
- }
- } catch (ClassNotFoundException e) {
- throw new RuntimeException(e);
- }
- });
- }
-
@SuppressWarnings({"ReferenceEquality"})
@Override
public MethodHandle findShadowMethodHandle(
@@ -445,10 +383,6 @@
continue;
}
- if (className.equals(ShadowMethodPlan.class.getName())) {
- continue;
- }
-
if (methodName.startsWith(ShadowConstants.ROBO_PREFIX)) {
methodName =
methodName.substring(
@@ -575,57 +509,6 @@
}
}
- private static class ShadowMethodPlan implements Plan {
- private final Method shadowMethod;
-
- public ShadowMethodPlan(Method shadowMethod) {
- this.shadowMethod = shadowMethod;
- }
-
- @Override
- public Object run(Object instance, Object[] params) throws Throwable {
- ShadowedObject shadowedObject = (ShadowedObject) instance;
- Object shadow = shadowedObject == null ? null : shadowedObject.$$robo$getData();
- try {
- return shadowMethod.invoke(shadow, params);
- } catch (IllegalArgumentException e) {
- assert shadow
- != null; // because IllegalArgumentException could only be thrown if shadow is non-null
- Method tryAgainMethod =
- shadow
- .getClass()
- .getDeclaredMethod(shadowMethod.getName(), shadowMethod.getParameterTypes());
- if (!tryAgainMethod.equals(shadowMethod)) {
- tryAgainMethod.setAccessible(true);
- try {
- return tryAgainMethod.invoke(shadow, params);
- } catch (InvocationTargetException e1) {
- throw e1.getCause();
- }
- } else {
- throw new IllegalArgumentException(
- "attempted to invoke "
- + shadowMethod
- + (shadow == null
- ? ""
- : " on instance of "
- + shadow.getClass()
- + ", but "
- + shadow.getClass().getSimpleName()
- + " doesn't extend "
- + shadowMethod.getDeclaringClass().getSimpleName()));
- }
- } catch (InvocationTargetException e) {
- throw e.getCause();
- }
- }
-
- @Override
- public String describe() {
- return shadowMethod.toString();
- }
- }
-
private static class ShadowMetadata {
final Constructor<?> constructor;
final List<Field> realObjectFields = new ArrayList<>();
diff --git a/sandbox/src/test/java/org/robolectric/internal/bytecode/SandboxClassLoaderTest.java b/sandbox/src/test/java/org/robolectric/internal/bytecode/SandboxClassLoaderTest.java
index 40cef3f..016e883 100644
--- a/sandbox/src/test/java/org/robolectric/internal/bytecode/SandboxClassLoaderTest.java
+++ b/sandbox/src/test/java/org/robolectric/internal/bytecode/SandboxClassLoaderTest.java
@@ -609,32 +609,6 @@
}
@Override
- public Plan methodInvoked(String signature, boolean isStatic, Class<?> theClass) {
- final InvocationProfile invocationProfile =
- new InvocationProfile(signature, isStatic, getClass().getClassLoader());
- return new Plan() {
- @Override
- public Object run(Object instance, Object[] params) throws Exception {
- try {
- return methodInvoked(
- invocationProfile.clazz,
- invocationProfile.methodName,
- instance,
- invocationProfile.paramTypes,
- params);
- } catch (Throwable throwable) {
- throw new RuntimeException(throwable);
- }
- }
-
- @Override
- public String describe() {
- return invocationProfile.methodName;
- }
- };
- }
-
- @Override
public MethodHandle getShadowCreator(Class<?> theClass) {
return dropArguments(constant(String.class, "a shadow!"), 0, theClass);
}