2 * Copyright (C) 2015 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "JavaClassGenerator.h"
18 #include "NameMangler.h"
20 #include "ResourceTable.h"
21 #include "ResourceValues.h"
22 #include "StringPiece.h"
32 // The number of attributes to emit per line in a Styleable array.
33 constexpr size_t kAttribsPerLine = 4;
35 JavaClassGenerator::JavaClassGenerator(const std::shared_ptr<const ResourceTable>& table,
37 mTable(table), mOptions(options) {
40 static void generateHeader(std::ostream& out, const StringPiece16& package) {
41 out << "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
43 " * This class was automatically generated by the\n"
44 " * aapt tool from the resource data it found. It\n"
45 " * should not be modified by hand.\n"
47 out << "package " << package << ";"
52 static const std::set<StringPiece16> sJavaIdentifiers = {
53 u"abstract", u"assert", u"boolean", u"break", u"byte",
54 u"case", u"catch", u"char", u"class", u"const", u"continue",
55 u"default", u"do", u"double", u"else", u"enum", u"extends",
56 u"final", u"finally", u"float", u"for", u"goto", u"if",
57 u"implements", u"import", u"instanceof", u"int", u"interface",
58 u"long", u"native", u"new", u"package", u"private", u"protected",
59 u"public", u"return", u"short", u"static", u"strictfp", u"super",
60 u"switch", u"synchronized", u"this", u"throw", u"throws",
61 u"transient", u"try", u"void", u"volatile", u"while", u"true",
65 static bool isValidSymbol(const StringPiece16& symbol) {
66 return sJavaIdentifiers.find(symbol) == sJavaIdentifiers.end();
70 * Java symbols can not contain . or -, but those are valid in a resource name.
71 * Replace those with '_'.
73 static std::u16string transform(const StringPiece16& symbol) {
74 std::u16string output = symbol.toString();
75 for (char16_t& c : output) {
76 if (c == u'.' || c == u'-') {
83 struct GenArgs : ValueVisitorArgs {
84 GenArgs(std::ostream* o, std::u16string* e) : out(o), entryName(e) {
88 std::u16string* entryName;
91 void JavaClassGenerator::visit(const Styleable& styleable, ValueVisitorArgs& a) {
92 const StringPiece finalModifier = mOptions.useFinal ? " final" : "";
93 std::ostream* out = static_cast<GenArgs&>(a).out;
94 std::u16string* entryName = static_cast<GenArgs&>(a).entryName;
96 // This must be sorted by resource ID.
97 std::vector<std::pair<ResourceId, StringPiece16>> sortedAttributes;
98 sortedAttributes.reserve(styleable.entries.size());
99 for (const auto& attr : styleable.entries) {
100 assert(attr.id.isValid() && "no ID set for Styleable entry");
101 assert(attr.name.isValid() && "no name set for Styleable entry");
102 sortedAttributes.emplace_back(attr.id, attr.name.entry);
104 std::sort(sortedAttributes.begin(), sortedAttributes.end());
106 // First we emit the array containing the IDs of each attribute.
108 << "public static final int[] " << transform(*entryName) << " = {";
110 const size_t attrCount = sortedAttributes.size();
111 for (size_t i = 0; i < attrCount; i++) {
112 if (i % kAttribsPerLine == 0) {
113 *out << std::endl << " ";
116 *out << sortedAttributes[i].first;
117 if (i != attrCount - 1) {
121 *out << std::endl << " };" << std::endl;
123 // Now we emit the indices into the array.
124 for (size_t i = 0; i < attrCount; i++) {
126 << "public static" << finalModifier
127 << " int " << transform(*entryName) << "_" << transform(sortedAttributes[i].second)
128 << " = " << i << ";" << std::endl;
132 bool JavaClassGenerator::generateType(const std::u16string& package, size_t packageId,
133 const ResourceTableType& type, std::ostream& out) {
134 const StringPiece finalModifier = mOptions.useFinal ? " final" : "";
136 std::u16string unmangledPackage;
137 std::u16string unmangledName;
138 for (const auto& entry : type.entries) {
139 ResourceId id = { packageId, type.typeId, entry->entryId };
140 assert(id.isValid());
142 unmangledName = entry->name;
143 if (NameMangler::unmangle(&unmangledName, &unmangledPackage)) {
144 // The entry name was mangled, and we successfully unmangled it.
145 // Check that we want to emit this symbol.
146 if (package != unmangledPackage) {
147 // Skip the entry if it doesn't belong to the package we're writing.
151 if (package != mTable->getPackage()) {
152 // We are processing a mangled package name,
153 // but this is a non-mangled resource.
158 if (!isValidSymbol(unmangledName)) {
159 ResourceNameRef resourceName = { package, type.type, unmangledName };
160 std::stringstream err;
161 err << "invalid symbol name '" << resourceName << "'";
166 if (type.type == ResourceType::kStyleable) {
167 assert(!entry->values.empty());
168 entry->values.front().value->accept(*this, GenArgs{ &out, &unmangledName });
170 out << " " << "public static" << finalModifier
171 << " int " << transform(unmangledName) << " = " << id << ";" << std::endl;
177 bool JavaClassGenerator::generate(const std::u16string& package, std::ostream& out) {
178 const size_t packageId = mTable->getPackageId();
180 generateHeader(out, package);
182 out << "public final class R {" << std::endl;
184 for (const auto& type : *mTable) {
185 out << " public static final class " << type->type << " {" << std::endl;
186 if (!generateType(package, packageId, *type, out)) {
189 out << " }" << std::endl;
192 out << "}" << std::endl;