blob: 0175489cf6eae25ad5e43f90b3b9c23516f61348 [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
Adam Lesinski769de982015-04-10 19:43:55 -070017#include "NameMangler.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080018#include "Resource.h"
19#include "ResourceTable.h"
20#include "ResourceValues.h"
Adam Lesinskica5638f2015-10-21 14:42:43 -070021#include "java/AnnotationProcessor.h"
22#include "java/JavaClassGenerator.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023#include "util/StringPiece.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024
Adam Lesinskica2fc352015-04-03 12:08:26 -070025#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080026#include <ostream>
27#include <set>
28#include <sstream>
29#include <tuple>
30
31namespace aapt {
32
33// The number of attributes to emit per line in a Styleable array.
34constexpr size_t kAttribsPerLine = 4;
35
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036JavaClassGenerator::JavaClassGenerator(ResourceTable* table, JavaClassGeneratorOptions options) :
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037 mTable(table), mOptions(options) {
38}
39
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040static void generateHeader(const StringPiece16& packageNameToGenerate, std::ostream* out) {
41 *out << "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
42 " *\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"
46 " */\n\n"
47 "package " << packageNameToGenerate << ";\n\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048}
49
50static const std::set<StringPiece16> sJavaIdentifiers = {
51 u"abstract", u"assert", u"boolean", u"break", u"byte",
52 u"case", u"catch", u"char", u"class", u"const", u"continue",
53 u"default", u"do", u"double", u"else", u"enum", u"extends",
54 u"final", u"finally", u"float", u"for", u"goto", u"if",
55 u"implements", u"import", u"instanceof", u"int", u"interface",
56 u"long", u"native", u"new", u"package", u"private", u"protected",
57 u"public", u"return", u"short", u"static", u"strictfp", u"super",
58 u"switch", u"synchronized", u"this", u"throw", u"throws",
59 u"transient", u"try", u"void", u"volatile", u"while", u"true",
60 u"false", u"null"
61};
62
63static bool isValidSymbol(const StringPiece16& symbol) {
64 return sJavaIdentifiers.find(symbol) == sJavaIdentifiers.end();
65}
66
67/*
68 * Java symbols can not contain . or -, but those are valid in a resource name.
69 * Replace those with '_'.
70 */
71static std::u16string transform(const StringPiece16& symbol) {
72 std::u16string output = symbol.toString();
73 for (char16_t& c : output) {
74 if (c == u'.' || c == u'-') {
75 c = u'_';
76 }
77 }
78 return output;
79}
80
Adam Lesinski9e10ac72015-10-16 14:37:48 -070081bool JavaClassGenerator::skipSymbol(SymbolState state) {
82 switch (mOptions.types) {
83 case JavaClassGeneratorOptions::SymbolTypes::kAll:
84 return false;
85 case JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate:
86 return state == SymbolState::kUndefined;
87 case JavaClassGeneratorOptions::SymbolTypes::kPublic:
88 return state != SymbolState::kPublic;
89 }
90 return true;
91}
92
Adam Lesinski1ab598f2015-08-14 14:26:04 -070093void JavaClassGenerator::generateStyleable(const StringPiece16& packageNameToGenerate,
94 const std::u16string& entryName,
95 const Styleable* styleable,
96 std::ostream* out) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080097 const StringPiece finalModifier = mOptions.useFinal ? " final" : "";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098
99 // This must be sorted by resource ID.
Adam Lesinski838a6872015-05-01 13:14:05 -0700100 std::vector<std::pair<ResourceId, ResourceNameRef>> sortedAttributes;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700101 sortedAttributes.reserve(styleable->entries.size());
102 for (const auto& attr : styleable->entries) {
Adam Lesinski330edcd2015-05-04 17:40:56 -0700103 // If we are not encoding final attributes, the styleable entry may have no ID
104 // if we are building a static library.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700105 assert((!mOptions.useFinal || attr.id) && "no ID set for Styleable entry");
106 assert(attr.name && "no name set for Styleable entry");
107 sortedAttributes.emplace_back(attr.id ? attr.id.value() : ResourceId(0), attr.name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800108 }
109 std::sort(sortedAttributes.begin(), sortedAttributes.end());
110
111 // First we emit the array containing the IDs of each attribute.
Adam Lesinski769de982015-04-10 19:43:55 -0700112 *out << " "
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700113 << "public static final int[] " << transform(entryName) << " = {";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800114
115 const size_t attrCount = sortedAttributes.size();
116 for (size_t i = 0; i < attrCount; i++) {
117 if (i % kAttribsPerLine == 0) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700118 *out << "\n ";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119 }
120
Adam Lesinski769de982015-04-10 19:43:55 -0700121 *out << sortedAttributes[i].first;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800122 if (i != attrCount - 1) {
Adam Lesinski769de982015-04-10 19:43:55 -0700123 *out << ", ";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800124 }
125 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700126 *out << "\n };\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800127
128 // Now we emit the indices into the array.
129 for (size_t i = 0; i < attrCount; i++) {
Adam Lesinski769de982015-04-10 19:43:55 -0700130 *out << " "
131 << "public static" << finalModifier
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700132 << " int " << transform(entryName);
Adam Lesinski838a6872015-05-01 13:14:05 -0700133
134 // We may reference IDs from other packages, so prefix the entry name with
135 // the package.
136 const ResourceNameRef& itemName = sortedAttributes[i].second;
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700137 if (!itemName.package.empty() && packageNameToGenerate != itemName.package) {
Adam Lesinski838a6872015-05-01 13:14:05 -0700138 *out << "_" << transform(itemName.package);
139 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700140 *out << "_" << transform(itemName.entry) << " = " << i << ";\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800141 }
142}
143
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700144bool JavaClassGenerator::generateType(const StringPiece16& packageNameToGenerate,
145 const ResourceTablePackage* package,
146 const ResourceTableType* type,
147 std::ostream* out) {
Adam Lesinski769de982015-04-10 19:43:55 -0700148 const StringPiece finalModifier = mOptions.useFinal ? " final" : "";
149
150 std::u16string unmangledPackage;
151 std::u16string unmangledName;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700152 for (const auto& entry : type->entries) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700153 if (skipSymbol(entry->symbolStatus.state)) {
154 continue;
155 }
156
157 ResourceId id(package->id.value(), type->id.value(), entry->id.value());
Adam Lesinski769de982015-04-10 19:43:55 -0700158 assert(id.isValid());
159
160 unmangledName = entry->name;
161 if (NameMangler::unmangle(&unmangledName, &unmangledPackage)) {
162 // The entry name was mangled, and we successfully unmangled it.
163 // Check that we want to emit this symbol.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700164 if (package->name != unmangledPackage) {
Adam Lesinski769de982015-04-10 19:43:55 -0700165 // Skip the entry if it doesn't belong to the package we're writing.
166 continue;
167 }
168 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169 if (packageNameToGenerate != package->name) {
Adam Lesinski769de982015-04-10 19:43:55 -0700170 // We are processing a mangled package name,
171 // but this is a non-mangled resource.
172 continue;
173 }
174 }
175
176 if (!isValidSymbol(unmangledName)) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700177 ResourceNameRef resourceName(packageNameToGenerate, type->type, unmangledName);
Adam Lesinski769de982015-04-10 19:43:55 -0700178 std::stringstream err;
179 err << "invalid symbol name '" << resourceName << "'";
180 mError = err.str();
181 return false;
182 }
183
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700184 if (type->type == ResourceType::kStyleable) {
Adam Lesinski769de982015-04-10 19:43:55 -0700185 assert(!entry->values.empty());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700186 generateStyleable(packageNameToGenerate, unmangledName, static_cast<const Styleable*>(
187 entry->values.front().value.get()), out);
Adam Lesinski769de982015-04-10 19:43:55 -0700188 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700189 *out << " " << "public static" << finalModifier
190 << " int " << transform(unmangledName) << " = " << id << ";\n";
Adam Lesinski769de982015-04-10 19:43:55 -0700191 }
192 }
193 return true;
194}
195
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700196bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, std::ostream* out) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700197 return generate(packageNameToGenerate, packageNameToGenerate, out);
198}
199
200bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate,
201 const StringPiece16& outPackageName, std::ostream* out) {
202 generateHeader(outPackageName, out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800203
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700204 *out << "public final class R {\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800205
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700206 for (const auto& package : mTable->packages) {
207 for (const auto& type : package->types) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700208 StringPiece16 typeStr;
209 if (type->type == ResourceType::kAttrPrivate) {
210 typeStr = toString(ResourceType::kAttr);
211 } else {
212 typeStr = toString(type->type);
213 }
214 *out << " public static final class " << typeStr << " {\n";
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700215 if (!generateType(packageNameToGenerate, package.get(), type.get(), out)) {
216 return false;
217 }
218 *out << " }\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800219 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800220 }
221
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700222 *out << "}\n";
223 out->flush();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800224 return true;
225}
226
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700227
228
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800229} // namespace aapt