Search in sources :

Example 1 with FieldInitializer

use of com.google.devtools.build.android.resources.FieldInitializer in project bazel by bazelbuild.

the class AndroidResourceClassWriter method getResourceInitializers.

private List<FieldInitializer> getResourceInitializers(ResourceType type, int typeId, Collection<String> sortedFields) {
    ImmutableList.Builder<FieldInitializer> initList = ImmutableList.builder();
    Map<String, Integer> publicNameToId = new HashMap<>();
    Set<Integer> assignedIds = ImmutableSet.of();
    if (publicIds.containsKey(type)) {
        assignedIds = assignPublicIds(publicNameToId, publicIds.get(type), typeId);
    }
    int resourceIds = nextFreeId(getInitialIdForTypeId(typeId), assignedIds);
    for (String field : sortedFields) {
        Integer fieldValue = publicNameToId.get(field);
        if (fieldValue == null) {
            fieldValue = resourceIds;
            resourceIds = nextFreeId(resourceIds + 1, assignedIds);
        }
        initList.add(new IntFieldInitializer(field, fieldValue));
    }
    return initList.build();
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IntFieldInitializer(com.google.devtools.build.android.resources.IntFieldInitializer) ImmutableList(com.google.common.collect.ImmutableList) FieldInitializer(com.google.devtools.build.android.resources.FieldInitializer) IntArrayFieldInitializer(com.google.devtools.build.android.resources.IntArrayFieldInitializer) IntFieldInitializer(com.google.devtools.build.android.resources.IntFieldInitializer)

Example 2 with FieldInitializer

use of com.google.devtools.build.android.resources.FieldInitializer in project bazel by bazelbuild.

the class AndroidResourceClassWriter method getStyleableInitializers.

private List<FieldInitializer> getStyleableInitializers(Map<String, Integer> attrAssignments, Collection<String> styleableFields) throws AttrLookupException {
    ImmutableList.Builder<FieldInitializer> initList = ImmutableList.builder();
    for (String field : styleableFields) {
        Set<String> attrs = styleableAttrs.get(field).keySet();
        ImmutableMap.Builder<String, Integer> arrayInitValues = ImmutableMap.builder();
        for (String attr : attrs) {
            Integer attrId = attrAssignments.get(attr);
            if (attrId == null) {
                // It should be a framework resource, otherwise we don't know about the resource.
                if (!attr.startsWith(NORMALIZED_ANDROID_PREFIX)) {
                    throw new AttrLookupException("App attribute not found: " + attr);
                }
                String attrWithoutPrefix = attr.substring(NORMALIZED_ANDROID_PREFIX.length());
                attrId = androidIdProvider.getAttrId(attrWithoutPrefix);
            }
            arrayInitValues.put(attr, attrId);
        }
        // The styleable array should be sorted by ID value.
        // Make sure that if we have android: framework attributes, their IDs are listed first.
        ImmutableMap<String, Integer> arrayInitMap = arrayInitValues.orderEntriesByValue(Ordering.<Integer>natural()).build();
        initList.add(new IntArrayFieldInitializer(field, arrayInitMap.values()));
        int index = 0;
        for (String attr : arrayInitMap.keySet()) {
            initList.add(new IntFieldInitializer(field + "_" + attr, index));
            ++index;
        }
    }
    return initList.build();
}
Also used : IntArrayFieldInitializer(com.google.devtools.build.android.resources.IntArrayFieldInitializer) ImmutableList(com.google.common.collect.ImmutableList) FieldInitializer(com.google.devtools.build.android.resources.FieldInitializer) IntArrayFieldInitializer(com.google.devtools.build.android.resources.IntArrayFieldInitializer) IntFieldInitializer(com.google.devtools.build.android.resources.IntFieldInitializer) ImmutableMap(com.google.common.collect.ImmutableMap) AttrLookupException(com.google.devtools.build.android.AndroidFrameworkAttrIdProvider.AttrLookupException) IntFieldInitializer(com.google.devtools.build.android.resources.IntFieldInitializer)

Example 3 with FieldInitializer

use of com.google.devtools.build.android.resources.FieldInitializer in project bazel by bazelbuild.

the class AndroidResourceClassWriter method fillInitializers.

private void fillInitializers(Map<ResourceType, List<FieldInitializer>> initializers) throws AttrLookupException {
    Map<ResourceType, Integer> typeIdMap = chooseTypeIds();
    Map<String, Integer> attrAssignments = assignAttrIds(typeIdMap.get(ResourceType.ATTR));
    for (Map.Entry<ResourceType, Set<String>> fieldEntries : innerClasses.entrySet()) {
        ResourceType type = fieldEntries.getKey();
        ImmutableList<String> sortedFields = Ordering.natural().immutableSortedCopy(fieldEntries.getValue());
        List<FieldInitializer> fields;
        if (type == ResourceType.STYLEABLE) {
            fields = getStyleableInitializers(attrAssignments, sortedFields);
        } else if (type == ResourceType.ATTR) {
            fields = getAttrInitializers(attrAssignments, sortedFields);
        } else {
            int typeId = typeIdMap.get(type);
            fields = getResourceInitializers(type, typeId, sortedFields);
        }
        // The maximum number of Java fields is 2^16.
        // See the JVM reference "4.11. Limitations of the Java Virtual Machine."
        Preconditions.checkArgument(fields.size() < (1 << 16));
        initializers.put(type, fields);
    }
}
Also used : TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ResourceType(com.android.resources.ResourceType) FieldInitializer(com.google.devtools.build.android.resources.FieldInitializer) IntArrayFieldInitializer(com.google.devtools.build.android.resources.IntArrayFieldInitializer) IntFieldInitializer(com.google.devtools.build.android.resources.IntFieldInitializer) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) EnumMap(java.util.EnumMap) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 4 with FieldInitializer

use of com.google.devtools.build.android.resources.FieldInitializer in project bazel by bazelbuild.

the class AndroidResourceClassWriter method writeAsJava.

private void writeAsJava(Map<ResourceType, List<FieldInitializer>> initializers) throws IOException {
    String packageDir = packageName.replace('.', '/');
    Path packagePath = outputBasePath.resolve(packageDir);
    Path rJavaPath = packagePath.resolve(SdkConstants.FN_RESOURCE_CLASS);
    Files.createDirectories(rJavaPath.getParent());
    try (BufferedWriter writer = Files.newBufferedWriter(rJavaPath, UTF_8)) {
        writer.write("/* AUTO-GENERATED FILE.  DO NOT MODIFY.\n");
        writer.write(" *\n");
        writer.write(" * This class was automatically generated by the\n");
        writer.write(" * bazel tool from the resource data it found.  It\n");
        writer.write(" * should not be modified by hand.\n");
        writer.write(" */\n");
        writer.write(String.format("package %s;\n", packageName));
        writer.write("public final class R {\n");
        for (Map.Entry<ResourceType, Set<String>> fieldEntries : innerClasses.entrySet()) {
            ResourceType type = fieldEntries.getKey();
            writer.write(String.format("    public static final class %s {\n", type.getName()));
            for (FieldInitializer field : initializers.get(type)) {
                field.writeInitSource(writer);
            }
            writer.write("    }\n");
        }
        writer.write("}");
    }
}
Also used : Path(java.nio.file.Path) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ResourceType(com.android.resources.ResourceType) FieldInitializer(com.google.devtools.build.android.resources.FieldInitializer) IntArrayFieldInitializer(com.google.devtools.build.android.resources.IntArrayFieldInitializer) IntFieldInitializer(com.google.devtools.build.android.resources.IntFieldInitializer) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) EnumMap(java.util.EnumMap) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) BufferedWriter(java.io.BufferedWriter)

Example 5 with FieldInitializer

use of com.google.devtools.build.android.resources.FieldInitializer in project bazel by bazelbuild.

the class AndroidResourceClassWriter method getAttrInitializers.

private List<FieldInitializer> getAttrInitializers(Map<String, Integer> attrAssignments, Collection<String> sortedFields) {
    ImmutableList.Builder<FieldInitializer> initList = ImmutableList.builder();
    for (String field : sortedFields) {
        int attrId = attrAssignments.get(field);
        initList.add(new IntFieldInitializer(field, attrId));
    }
    return initList.build();
}
Also used : IntFieldInitializer(com.google.devtools.build.android.resources.IntFieldInitializer) ImmutableList(com.google.common.collect.ImmutableList) FieldInitializer(com.google.devtools.build.android.resources.FieldInitializer) IntArrayFieldInitializer(com.google.devtools.build.android.resources.IntArrayFieldInitializer) IntFieldInitializer(com.google.devtools.build.android.resources.IntFieldInitializer)

Aggregations

FieldInitializer (com.google.devtools.build.android.resources.FieldInitializer)5 IntArrayFieldInitializer (com.google.devtools.build.android.resources.IntArrayFieldInitializer)5 IntFieldInitializer (com.google.devtools.build.android.resources.IntFieldInitializer)5 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 ResourceType (com.android.resources.ResourceType)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 EnumMap (java.util.EnumMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 SortedMap (java.util.SortedMap)2 TreeMap (java.util.TreeMap)2 TreeSet (java.util.TreeSet)2 AttrLookupException (com.google.devtools.build.android.AndroidFrameworkAttrIdProvider.AttrLookupException)1 BufferedWriter (java.io.BufferedWriter)1 Path (java.nio.file.Path)1