• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 =
31         findInterceptor(methodSignature.className, methodSignature.methodName);
32     if (interceptor != null) {
33       return interceptor.handle(methodSignature);
34     }
35 
36     // nothing matched, return default
37     return Interceptor.returnDefaultValue(methodSignature);
38   }
39 
findInterceptor(String className, String methodName)40   public Interceptor findInterceptor(String className, String methodName) {
41     Interceptor mh = interceptors.get(new MethodRef(className, methodName));
42     if (mh == null) {
43       mh = interceptors.get(new MethodRef(className, "*"));
44     }
45     return mh;
46   }
47 }
48