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();
}
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();
}
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);
}
}
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("}");
}
}
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();
}
Aggregations