All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.robolectric.internal.bytecode.Sandbox Maven / Gradle / Ivy

There is a newer version: 4.13
Show newest version
package org.robolectric.internal.bytecode;

import static org.robolectric.util.ReflectionHelpers.newInstance;
import static org.robolectric.util.ReflectionHelpers.setStaticField;

import java.util.Set;
import org.robolectric.shadow.api.Shadow;

public class Sandbox {
  private final ClassLoader robolectricClassLoader;
  private ShadowInvalidator shadowInvalidator;
  public ClassHandler classHandler; // todo not public
  private ShadowMap shadowMap = ShadowMap.EMPTY;

  public Sandbox(ClassLoader robolectricClassLoader) {
    this.robolectricClassLoader = robolectricClassLoader;
  }

  public  Class bootstrappedClass(Class clazz) {
    try {
      return (Class) robolectricClassLoader.loadClass(clazz.getName());
    } catch (ClassNotFoundException e) {
      throw new RuntimeException(e);
    }
  }
  public ClassLoader getRobolectricClassLoader() {
    return robolectricClassLoader;
  }

  private ShadowInvalidator getShadowInvalidator() {
    if (shadowInvalidator == null) {
      this.shadowInvalidator = new ShadowInvalidator();
    }
    return shadowInvalidator;
  }

  public void replaceShadowMap(ShadowMap shadowMap) {
    if (InvokeDynamic.ENABLED) {
      ShadowMap oldShadowMap = this.shadowMap;
      this.shadowMap = shadowMap;
      Set invalidatedClasses = shadowMap.getInvalidatedClasses(oldShadowMap);
      getShadowInvalidator().invalidateClasses(invalidatedClasses);
    }
  }

  public void configure(ClassHandler classHandler, Interceptors interceptors) {
    this.classHandler = classHandler;

    ClassLoader robolectricClassLoader = getRobolectricClassLoader();
    Class robolectricInternalsClass = bootstrappedClass(RobolectricInternals.class);
    if (InvokeDynamic.ENABLED) {
      ShadowInvalidator invalidator = getShadowInvalidator();
      setStaticField(robolectricInternalsClass, "shadowInvalidator", invalidator);
    }

    setStaticField(robolectricInternalsClass, "classHandler", classHandler);
    setStaticField(robolectricInternalsClass, "classLoader", robolectricClassLoader);

    Class invokeDynamicSupportClass = bootstrappedClass(InvokeDynamicSupport.class);
    setStaticField(invokeDynamicSupportClass, "INTERCEPTORS", interceptors);

    Class shadowClass = bootstrappedClass(Shadow.class);
    setStaticField(shadowClass, "SHADOW_IMPL", newInstance(bootstrappedClass(ShadowImpl.class)));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy