blob: cdf1b6ad600b3c8b518a86f300f76195117d1553 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include "JavaClassGenerator.h"
Adam Lesinski769de982015-04-10 19:43:55 -070018#include "NameMangler.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080019#include "Resource.h"
20#include "ResourceTable.h"
21#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070022#include "util/StringPiece.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023
Adam Lesinskica2fc352015-04-03 12:08:26 -070024#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080025#include <ostream>
26#include <set>
27#include <sstream>
28#include <tuple>
29
30namespace aapt {
31
32// The number of attributes to emit per line in a Styleable array.
33constexpr size_t kAttribsPerLine = 4;
34
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035JavaClassGenerator::JavaClassGenerator(ResourceTable* table, JavaClassGeneratorOptions options) :
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080036 mTable(table), mOptions(options) {
37}
38
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039static void generateHeader(const StringPiece16& packageNameToGenerate, std::ostream* out) {
40 *out << "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
41 " *\n"
42 " * This class was automatically generated by the\n"
43 " * aapt tool from the resource data it found. It\n"
44 " * should not be modified by hand.\n"
45 " */\n\n"
46 "package " << packageNameToGenerate << ";\n\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047}
48
49static const std::set<StringPiece16> sJavaIdentifiers = {
50 u"abstract", u"assert", u"boolean", u"break", u"byte",
51 u"case", u"catch", u"char", u"class", u"const", u"continue",
52 u"default", u"do", u"double", u"else", u"enum", u"extends",
53 u"final", u"finally", u"float", u"for", u"goto", u"if",
54 u"implements", u"import", u"instanceof", u"int", u"interface",
55 u"long", u"native", u"new", u"package", u"private", u"protected",
56 u"public", u"return", u"short", u"static", u"strictfp", u"super",
57 u"switch", u"synchronized", u"this", u"throw", u"throws",
58 u"transient", u"try", u"void", u"volatile", u"while", u"true",
59 u"false", u"null"
60};
61
62static bool isValidSymbol(const StringPiece16& symbol) {
63 return sJavaIdentifiers.find(symbol) == sJavaIdentifiers.end();
64}
65
66/*
67 * Java symbols can not contain . or -, but those are valid in a resource name.
68 * Replace those with '_'.
69 */
70static std::u16string transform(const StringPiece16& symbol) {
71 std::u16string output = symbol.toString();
72 for (char16_t& c : output) {
73 if (c == u'.' || c == u'-') {
74 c = u'_';
75 }
76 }
77 return output;
78}
79
Adam Lesinski9e10ac72015-10-16 14:37:48 -070080bool JavaClassGenerator::skipSymbol(SymbolState state) {
81 switch (mOptions.types) {
82 case JavaClassGeneratorOptions::SymbolTypes::kAll:
83 return false;
84 case JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate:
85 return state == SymbolState::kUndefined;
86 case JavaClassGeneratorOptions::SymbolTypes::kPublic:
87 return state != SymbolState::kPublic;
88 }
89 return true;
90}
91
Adam Lesinski1ab598f2015-08-14 14:26:04 -070092void JavaClassGenerator::generateStyleable(const StringPiece16& packageNameToGenerate,
93 const std::u16string& entryName,
94 const Styleable* styleable,
95 std::ostream* out) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096 const StringPiece finalModifier = mOptions.useFinal ? " final" : "";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080097
98 // This must be sorted by resource ID.
Adam Lesinski838a6872015-05-01 13:14:05 -070099 std::vector<std::pair<ResourceId, ResourceNameRef>> sortedAttributes;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700100 sortedAttributes.reserve(styleable->entries.size());
101 for (const auto& attr : styleable->entries) {
Adam Lesinski330edcd2015-05-04 17:40:56 -0700102 // If we are not encoding final attributes, the styleable entry may have no ID
103 // if we are building a static library.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700104 assert((!mOptions.useFinal || attr.id) && "no ID set for Styleable entry");
105 assert(attr.name && "no name set for Styleable entry");
106 sortedAttributes.emplace_back(attr.id ? attr.id.value() : ResourceId(0), attr.name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800107 }
108 std::sort(sortedAttributes.begin(), sortedAttributes.end());
109
110 // First we emit the array containing the IDs of each attribute.
Adam Lesinski769de982015-04-10 19:43:55 -0700111 *out << " "
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700112 << "public static final int[] " << transform(entryName) << " = {";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800113
114 const size_t attrCount = sortedAttributes.size();
115 for (size_t i = 0; i < attrCount; i++) {
116 if (i % kAttribsPerLine == 0) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700117 *out << "\n ";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800118 }
119
Adam Lesinski769de982015-04-10 19:43:55 -0700120 *out << sortedAttributes[i].first;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800121 if (i != attrCount - 1) {
Adam Lesinski769de982015-04-10 19:43:55 -0700122 *out << ", ";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800123 }
124 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700125 *out << "\n };\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800126
127 // Now we emit the indices into the array.
128 for (size_t i = 0; i < attrCount; i++) {
Adam Lesinski769de982015-04-10 19:43:55 -0700129 *out << " "
130 << "public static" << finalModifier
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131 << " int " << transform(entryName);
Adam Lesinski838a6872015-05-01 13:14:05 -0700132
133 // We may reference IDs from other packages, so prefix the entry name with
134 // the package.
135 const ResourceNameRef& itemName = sortedAttributes[i].second;
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700136 if (!itemName.package.empty() && packageNameToGenerate != itemName.package) {
Adam Lesinski838a6872015-05-01 13:14:05 -0700137 *out << "_" << transform(itemName.package);
138 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139 *out << "_" << transform(itemName.entry) << " = " << i << ";\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800140 }
141}
142
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700143bool JavaClassGenerator::generateType(const StringPiece16& packageNameToGenerate,
144 const ResourceTablePackage* package,
145 const ResourceTableType* type,
146 std::ostream* out) {
Adam Lesinski769de982015-04-10 19:43:55 -0700147 const StringPiece finalModifier = mOptions.useFinal ? " final" : "";
148
149 std::u16string unmangledPackage;
150 std::u16string unmangledName;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700151 for (const auto& entry : type->entries) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700152 if (skipSymbol(entry->symbolStatus.state)) {
153 continue;
154 }
155
156 ResourceId id(package->id.value(), type->id.value(), entry->id.value());
Adam Lesinski769de982015-04-10 19:43:55 -0700157 assert(id.isValid());
158
159 unmangledName = entry->name;
160 if (NameMangler::unmangle(&unmangledName, &unmangledPackage)) {
161 // The entry name was mangled, and we successfully unmangled it.
162 // Check that we want to emit this symbol.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700163 if (package->name != unmangledPackage) {
Adam Lesinski769de982015-04-10 19:43:55 -0700164 // Skip the entry if it doesn't belong to the package we're writing.
165 continue;
166 }
167 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700168 if (packageNameToGenerate != package->name) {
Adam Lesinski769de982015-04-10 19:43:55 -0700169 // We are processing a mangled package name,
170 // but this is a non-mangled resource.
171 continue;
172 }
173 }
174
175 if (!isValidSymbol(unmangledName)) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700176 ResourceNameRef resourceName(packageNameToGenerate, type->type, unmangledName);
Adam Lesinski769de982015-04-10 19:43:55 -0700177 std::stringstream err;
178 err << "invalid symbol name '" << resourceName << "'";
179 mError = err.str();
180 return false;
181 }
182
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700183 if (type->type == ResourceType::kStyleable) {
Adam Lesinski769de982015-04-10 19:43:55 -0700184 assert(!entry->values.empty());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700185 generateStyleable(packageNameToGenerate, unmangledName, static_cast<const Styleable*>(
186 entry->values.front().value.get()), out);
Adam Lesinski769de982015-04-10 19:43:55 -0700187 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700188 *out << " " << "public static" << finalModifier
189 << " int " << transform(unmangledName) << " = " << id << ";\n";
Adam Lesinski769de982015-04-10 19:43:55 -0700190 }
191 }
192 return true;
193}
194
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700195bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, std::ostream* out) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700196 return generate(packageNameToGenerate, packageNameToGenerate, out);
197}
198
199bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate,
200 const StringPiece16& outPackageName, std::ostream* out) {
201 generateHeader(outPackageName, out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800202
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700203 *out << "public final class R {\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800204
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700205 for (const auto& package : mTable->packages) {
206 for (const auto& type : package->types) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700207 StringPiece16 typeStr;
208 if (type->type == ResourceType::kAttrPrivate) {
209 typeStr = toString(ResourceType::kAttr);
210 } else {
211 typeStr = toString(type->type);
212 }
213 *out << " public static final class " << typeStr << " {\n";
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700214 if (!generateType(packageNameToGenerate, package.get(), type.get(), out)) {
215 return false;
216 }
217 *out << " }\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800218 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800219 }
220
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700221 *out << "}\n";
222 out->flush();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800223 return true;
224}
225
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700226
227
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228} // namespace aapt