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

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

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

import static java.util.Arrays.asList;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.robolectric.util.Function;

public class Interceptors {
  private final Map interceptors = new HashMap<>();

  public Interceptors(Interceptor... interceptors) {
    this(asList(interceptors));
  }

  public Interceptors(Collection interceptorList) {
    for (Interceptor interceptor : interceptorList) {
      for (MethodRef methodRef : interceptor.getMethodRefs()) {
        this.interceptors.put(methodRef, interceptor);
      }
    }
  }

  public Collection getAllMethodRefs() {
    return interceptors.keySet();
  }

  public Function getInterceptionHandler(final MethodSignature methodSignature) {
    Interceptor interceptor = findInterceptor(methodSignature.className, methodSignature.methodName);
    if (interceptor != null) {
      return interceptor.handle(methodSignature);
    }

    // nothing matched, return default
    return Interceptor.returnDefaultValue(methodSignature);
  }

  public Interceptor findInterceptor(String className, String methodName) {
    Interceptor mh = interceptors.get(new MethodRef(className, methodName));
    if (mh == null) {
      mh = interceptors.get(new MethodRef(className, "*"));
    }
    return mh;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy