1 package org.robolectric.internal.bytecode; 2 3 import static java.util.Arrays.asList; 4 5 import java.util.Collection; 6 import java.util.HashMap; 7 import java.util.Map; 8 import org.robolectric.util.Function; 9 10 public class Interceptors { 11 private final Map<MethodRef, Interceptor> interceptors = new HashMap<>(); 12 Interceptors(Interceptor... interceptors)13 public Interceptors(Interceptor... interceptors) { 14 this(asList(interceptors)); 15 } 16 Interceptors(Collection<Interceptor> interceptorList)17 public Interceptors(Collection<Interceptor> interceptorList) { 18 for (Interceptor interceptor : interceptorList) { 19 for (MethodRef methodRef : interceptor.getMethodRefs()) { 20 this.interceptors.put(methodRef, interceptor); 21 } 22 } 23 } 24 getAllMethodRefs()25 public Collection<MethodRef> getAllMethodRefs() { 26 return interceptors.keySet(); 27 } 28 getInterceptionHandler(final MethodSignature methodSignature)29 public Function<Object, Object> getInterceptionHandler(final MethodSignature methodSignature) { 30 Interceptor interceptor = findInterceptor(methodSignature.className, methodSignature.methodName); 31 if (interceptor != null) { 32 return interceptor.handle(methodSignature); 33 } 34 35 // nothing matched, return default 36 return Interceptor.returnDefaultValue(methodSignature); 37 } 38 findInterceptor(String className, String methodName)39 public Interceptor findInterceptor(String className, String methodName) { 40 Interceptor mh = interceptors.get(new MethodRef(className, methodName)); 41 if (mh == null) { 42 mh = interceptors.get(new MethodRef(className, "*")); 43 } 44 return mh; 45 } 46 } 47