1 package org.robolectric.internal.bytecode;
2 
3 import java.lang.invoke.SwitchPoint;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.Map;
7 
8 public class ShadowInvalidator {
9   private static final SwitchPoint DUMMY = new SwitchPoint();
10 
11   static {
SwitchPoint.invalidateAll(new SwitchPoint[] { DUMMY })12     SwitchPoint.invalidateAll(new SwitchPoint[] { DUMMY });
13   }
14 
15   private Map<String, SwitchPoint> switchPoints;
16 
ShadowInvalidator()17   public ShadowInvalidator() {
18     this.switchPoints = new HashMap<>();
19   }
20 
getSwitchPoint(Class<?> caller)21   public SwitchPoint getSwitchPoint(Class<?> caller) {
22     return getSwitchPoint(caller.getName());
23   }
24 
getSwitchPoint(String className)25   public synchronized SwitchPoint getSwitchPoint(String className) {
26     SwitchPoint switchPoint = switchPoints.get(className);
27     if (switchPoint == null) switchPoints.put(className, switchPoint = new SwitchPoint());
28     return switchPoint;
29   }
30 
invalidateClasses(Collection<String> classNames)31   public synchronized void invalidateClasses(Collection<String> classNames) {
32     if (classNames.isEmpty()) return;
33     SwitchPoint[] points = new SwitchPoint[classNames.size()];
34     int i = 0;
35     for (String className : classNames) {
36       SwitchPoint switchPoint = switchPoints.put(className, null);
37       if (switchPoint == null) switchPoint = DUMMY;
38       points[i++] = switchPoint;
39     }
40 
41     SwitchPoint.invalidateAll(points);
42   }
43 }
44