package com.xtremelabs.robolectric.res;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

public class ResourceExtractor {
    private Map<String, Integer> resourceStringToId = new HashMap<String, Integer>();
    private Map<Integer, String> resourceIdToString = new HashMap<Integer, String>();

    public void addRClass(Class rClass) throws Exception {
        for (Class innerClass : rClass.getClasses()) {
            for (Field field : innerClass.getDeclaredFields()) {
                if (field.getType().equals(Integer.TYPE) && Modifier.isStatic(field.getModifiers())) {
                    String name = innerClass.getSimpleName() + "/" + field.getName();
                    int value = field.getInt(null);
                    resourceStringToId.put(name, value);
                    resourceIdToString.put(value, name);
                }
            }
        }
    }

    public int resourceIdOrZero(String resourceName) {
        Integer resId = getResourceId(resourceName);
        return (resId == null) ? 0 : resId;
    }

    public Integer getResourceId(String resourceName) {
        if (resourceName == null) {
            return null;
        }
        if (resourceName.startsWith("@+id")) {
            resourceName = resourceName.substring(2);
        } else if (resourceName.startsWith("@")) {
            resourceName = resourceName.substring(1);
        }
        return resourceStringToId.get(resourceName);
    }

    public String getResourceName(int resourceId) {
        return resourceIdToString.get(resourceId);
    }
}
@@ -1553,38 +1553,44 @@
 ceId) %7B%0A        
-return
+String ret =
  resourceIdToStr
@@ -1614,11 +1614,132 @@
 d);%0A
+        if (ret == null)%0A            throw new RuntimeException(%22Can't find resource %22+resourceId);%0A        return ret;%0A
     %7D%0A%7D
+%0A

Throw RuntimeException if getResourceName() fails