<commit_before>package pl.polidea.robospock.internal;

import android.app.Application;
import android.os.Build;
import org.robolectric.*;
import org.robolectric.annotation.*;
import org.robolectric.bytecode.ClassHandler;
import org.robolectric.bytecode.RobolectricInternals;
import org.robolectric.bytecode.ShadowMap;
import org.robolectric.bytecode.ShadowWrangler;
import org.robolectric.internal.ParallelUniverseInterface;
import org.robolectric.internal.dependency.CachedDependencyResolver;
import org.robolectric.internal.dependency.DependencyResolver;
import org.robolectric.internal.dependency.LocalDependencyResolver;
import org.robolectric.internal.dependency.MavenDependencyResolver;
import org.robolectric.res.ResourceLoader;
import org.robolectric.util.Logger;
import org.robolectric.util.ReflectionHelpers;
import org.spockframework.runtime.extension.AbstractMethodInterceptor;
import org.spockframework.runtime.extension.IMethodInvocation;
import org.spockframework.runtime.model.SpecInfo;

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class RoboSpockInterceptor extends AbstractMethodInterceptor {
    private DependencyResolver dependencyResolver;

    protected DependencyResolver getJarResolver() {
        if (dependencyResolver == null) {
            if (Boolean.getBoolean("robolectric.offline")) {
                String dependencyDir = System.getProperty("robolectric.dependency.dir", ".");
                dependencyResolver = new LocalDependencyResolver(new File(dependencyDir));
            } else {
                File cacheDir = new File(new File(System.getProperty("java.io.tmpdir")), "robolectric");
                cacheDir.mkdir();

                if (cacheDir.exists()) {
                    Logger.info("Dependency cache location: %s", cacheDir.getAbsolutePath());
                    dependencyResolver = new CachedDependencyResolver(new MavenDependencyResolver(), cacheDir, 60 * 60 * 24 * 1000);
                } else {
                    dependencyResolver = new MavenDependencyResolver();
                }
            }
        }

        return dependencyResolver;
    }

    private static ShadowMap mainShadowMap;
    private TestLifecycle<Application> testLifecycle;

    private SpecInfo specInfo;
    private final SdkEnvironment sdkEnvironment;
    private final Config config;
    private final AndroidManifest appManifest;

    public RoboSpockInterceptor(
            SpecInfo specInfo, SdkEnvironment sdkEnvironment, Config config, AndroidManifest appManifest) {

        this.sdkEnvironment = sdkEnvironment;
        this.config = config;
        this.appManifest = appManifest;
        this.specInfo = specInfo;

        this.specInfo.addInterceptor(this);
    }

    @Override
    public void interceptSpecExecution(IMethodInvocation invocation) throws Throwable {

        configureShadows(sdkEnvironment, config);

        ParallelUniverseInterface parallelUniverseInterface = getHooksInterface(sdkEnvironment);
        try {
            assureTestLifecycle(sdkEnvironment);

            parallelUniverseInterface.resetStaticState(config);
            parallelUniverseInterface.setSdkConfig(sdkEnvironment.getSdkConfig());

            int sdkVersion = pickSdkVersion(config, appManifest);
            Class<?> versionClass = sdkEnvironment.bootstrappedClass(Build.VERSION.class);
            ReflectionHelpers.setStaticField(versionClass, "SDK_INT", sdkVersion);

            ResourceLoader systemResourceLoader = sdkEnvironment.getSystemResourceLoader(getJarResolver(), null);
            setUpApplicationState(null, parallelUniverseInterface, systemResourceLoader, appManifest, config);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }


        Map<Field, Object> withConstantAnnos = getWithConstantAnnotations();

        setupConstants(withConstantAnnos);

        try {
            invocation.proceed();
        } finally {
            parallelUniverseInterface.resetStaticState(config);
        }

    }

    private void setupConstants(Map<Field, Object> constants) {
        for (Field field : constants.keySet()) {
            Object newValue = constants.get(field);
            Object oldValue = ReflectionHelpers.getStaticField(field);
            ReflectionHelpers.setStaticField(field, newValue);
            constants.put(field, oldValue);
        }
    }

    private Map<Field, Object> getWithConstantAnnotations() {
        Map<Field, Object> constants = new HashMap<Field, Object>();

        for (Annotation anno : specInfo.getReflection().getAnnotations()) {
            addConstantFromAnnotation(constants, anno);
        }

        return constants;
    }

    private void addConstantFromAnnotation(Map<Field, Object> constants, Annotation anno) {
        try {
            String name = anno.annotationType().getName();
            Object newValue = null;

            if (name.equals(WithConstantString.class.getName())) {
                newValue = anno.annotationType().getMethod("newValue").invoke(anno);
            } else if (name.equals(WithConstantInt.class.getName())) {
                newValue = anno.annotationType().getMethod("newValue").invoke(anno);
            } else {
                return;
            }

            @SuppressWarnings("rawtypes")
            Class classWithField = (Class) anno.annotationType().getMethod("classWithField").invoke(anno);
            String fieldName = (String) anno.annotationType().getMethod("fieldName").invoke(anno);
            Field field = classWithField.getDeclaredField(fieldName);
            constants.put(field, newValue);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    protected void setUpApplicationState(Method method, ParallelUniverseInterface parallelUniverseInterface, ResourceLoader systemResourceLoader, AndroidManifest appManifest, Config config) {
        parallelUniverseInterface.setUpApplicationState(method, testLifecycle, systemResourceLoader, appManifest, config);
    }
<commit_msg>With* annotations are not used/supported in TestRunner anymore<commit_after>package pl.polidea.robospock.internal;

import android.app.Application;
import android.os.Build;
import org.robolectric.*;
import org.robolectric.bytecode.ClassHandler;
import org.robolectric.bytecode.RobolectricInternals;
import org.robolectric.bytecode.ShadowMap;
import org.robolectric.bytecode.ShadowWrangler;
import org.robolectric.internal.ParallelUniverseInterface;
import org.robolectric.internal.dependency.CachedDependencyResolver;
import org.robolectric.internal.dependency.DependencyResolver;
import org.robolectric.internal.dependency.LocalDependencyResolver;
import org.robolectric.internal.dependency.MavenDependencyResolver;
import org.robolectric.res.ResourceLoader;
import org.robolectric.util.Logger;
import org.robolectric.util.ReflectionHelpers;
import org.spockframework.runtime.extension.AbstractMethodInterceptor;
import org.spockframework.runtime.extension.IMethodInvocation;
import org.spockframework.runtime.model.SpecInfo;

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class RoboSpockInterceptor extends AbstractMethodInterceptor {
    private DependencyResolver dependencyResolver;

    protected DependencyResolver getJarResolver() {
        if (dependencyResolver == null) {
            if (Boolean.getBoolean("robolectric.offline")) {
                String dependencyDir = System.getProperty("robolectric.dependency.dir", ".");
                dependencyResolver = new LocalDependencyResolver(new File(dependencyDir));
            } else {
                File cacheDir = new File(new File(System.getProperty("java.io.tmpdir")), "robolectric");
                cacheDir.mkdir();

                if (cacheDir.exists()) {
                    Logger.info("Dependency cache location: %s", cacheDir.getAbsolutePath());
                    dependencyResolver = new CachedDependencyResolver(new MavenDependencyResolver(), cacheDir, 60 * 60 * 24 * 1000);
                } else {
                    dependencyResolver = new MavenDependencyResolver();
                }
            }
        }

        return dependencyResolver;
    }

    private static ShadowMap mainShadowMap;
    private TestLifecycle<Application> testLifecycle;

    private SpecInfo specInfo;
    private final SdkEnvironment sdkEnvironment;
    private final Config config;
    private final AndroidManifest appManifest;

    public RoboSpockInterceptor(
            SpecInfo specInfo, SdkEnvironment sdkEnvironment, Config config, AndroidManifest appManifest) {

        this.sdkEnvironment = sdkEnvironment;
        this.config = config;
        this.appManifest = appManifest;
        this.specInfo = specInfo;

        this.specInfo.addInterceptor(this);
    }

    @Override
    public void interceptSpecExecution(IMethodInvocation invocation) throws Throwable {

        configureShadows(sdkEnvironment, config);

        ParallelUniverseInterface parallelUniverseInterface = getHooksInterface(sdkEnvironment);
        try {
            assureTestLifecycle(sdkEnvironment);

            parallelUniverseInterface.resetStaticState(config);
            parallelUniverseInterface.setSdkConfig(sdkEnvironment.getSdkConfig());

            int sdkVersion = pickSdkVersion(config, appManifest);
            Class<?> versionClass = sdkEnvironment.bootstrappedClass(Build.VERSION.class);
            ReflectionHelpers.setStaticField(versionClass, "SDK_INT", sdkVersion);

            ResourceLoader systemResourceLoader = sdkEnvironment.getSystemResourceLoader(getJarResolver(), null);
            setUpApplicationState(null, parallelUniverseInterface, systemResourceLoader, appManifest, config);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

        try {
            invocation.proceed();
        } finally {
            parallelUniverseInterface.resetStaticState(config);
        }
    }

    protected void setUpApplicationState(Method method, ParallelUniverseInterface parallelUniverseInterface, ResourceLoader systemResourceLoader, AndroidManifest appManifest, Config config) {
        parallelUniverseInterface.setUpApplicationState(method, testLifecycle, systemResourceLoader, appManifest, config);
    }

{'fraction_non_alphanumeric': 0.08433962264150943, 'fraction_numerical': 0.0018867924528301887, 'mean_word_length': 3.3769611890999176, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2018744', 'n_tokens_mistral': 2706, 'n_tokens_neox': 2599, 'n_words': 510}