blob: 1076ffe1b01d9cc6c8029ee555d6521549043128 [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 Lesinski3b4cd942015-10-30 16:31:42 -070021#include "ValueVisitor.h"
22
Adam Lesinskica5638f2015-10-21 14:42:43 -070023#include "java/AnnotationProcessor.h"
Adam Lesinskib274e352015-11-06 15:14:35 -080024#include "java/ClassDefinitionWriter.h"
Adam Lesinskica5638f2015-10-21 14:42:43 -070025#include "java/JavaClassGenerator.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "util/StringPiece.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027
Adam Lesinskica2fc352015-04-03 12:08:26 -070028#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080029#include <ostream>
30#include <set>
31#include <sstream>
32#include <tuple>
33
34namespace aapt {
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 */
Adam Lesinski74605cd2016-03-03 15:39:50 -080071static std::string transform(const StringPiece16& symbol) {
72 std::string output = util::utf16ToUtf8(symbol);
73 for (char& c : output) {
74 if (c == '.' || c == '-') {
75 c = '_';
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076 }
77 }
78 return output;
79}
80
Adam Lesinski74605cd2016-03-03 15:39:50 -080081/**
82 * Transforms an attribute in a styleable to the Java field name:
83 *
84 * <declare-styleable name="Foo">
85 * <attr name="android:bar" />
86 * <attr name="bar" />
87 * </declare-styleable>
88 *
89 * Foo_android_bar
90 * Foo_bar
91 */
92static std::string transformNestedAttr(const ResourceNameRef& attrName,
93 const std::string& styleableClassName,
94 const StringPiece16& packageNameToGenerate) {
95 std::string output = styleableClassName;
96
97 // We may reference IDs from other packages, so prefix the entry name with
98 // the package.
99 if (!attrName.package.empty() && packageNameToGenerate != attrName.package) {
100 output += "_" + transform(attrName.package);
101 }
102 output += "_" + transform(attrName.entry);
103 return output;
104}
105
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700106bool JavaClassGenerator::skipSymbol(SymbolState state) {
107 switch (mOptions.types) {
108 case JavaClassGeneratorOptions::SymbolTypes::kAll:
109 return false;
110 case JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate:
111 return state == SymbolState::kUndefined;
112 case JavaClassGeneratorOptions::SymbolTypes::kPublic:
113 return state != SymbolState::kPublic;
114 }
115 return true;
116}
117
Adam Lesinski74605cd2016-03-03 15:39:50 -0800118struct StyleableAttr {
119 const Reference* attrRef;
120 std::string fieldName;
121};
122
123static bool lessStyleableAttr(const StyleableAttr& lhs, const StyleableAttr& rhs) {
124 const ResourceId lhsId = lhs.attrRef->id ? lhs.attrRef->id.value() : ResourceId(0);
125 const ResourceId rhsId = rhs.attrRef->id ? rhs.attrRef->id.value() : ResourceId(0);
126 if (lhsId < rhsId) {
127 return true;
128 } else if (lhsId > rhsId) {
129 return false;
130 } else {
131 return lhs.attrRef->name.value() < rhs.attrRef->name.value();
132 }
133}
134
Adam Lesinskib274e352015-11-06 15:14:35 -0800135void JavaClassGenerator::writeStyleableEntryForClass(ClassDefinitionWriter* outClassDef,
136 AnnotationProcessor* processor,
137 const StringPiece16& packageNameToGenerate,
138 const std::u16string& entryName,
139 const Styleable* styleable) {
Adam Lesinski74605cd2016-03-03 15:39:50 -0800140 const std::string className = transform(entryName);
141
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800142 // This must be sorted by resource ID.
Adam Lesinski74605cd2016-03-03 15:39:50 -0800143 std::vector<StyleableAttr> sortedAttributes;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700144 sortedAttributes.reserve(styleable->entries.size());
145 for (const auto& attr : styleable->entries) {
Adam Lesinski330edcd2015-05-04 17:40:56 -0700146 // If we are not encoding final attributes, the styleable entry may have no ID
147 // if we are building a static library.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700148 assert((!mOptions.useFinal || attr.id) && "no ID set for Styleable entry");
149 assert(attr.name && "no name set for Styleable entry");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150
Adam Lesinski74605cd2016-03-03 15:39:50 -0800151 sortedAttributes.emplace_back(StyleableAttr{
152 &attr, transformNestedAttr(attr.name.value(), className, packageNameToGenerate) });
153 }
154 std::sort(sortedAttributes.begin(), sortedAttributes.end(), lessStyleableAttr);
155
156 const size_t attrCount = sortedAttributes.size();
157
158 if (attrCount > 0) {
159 // Build the comment string for the Styleable. It includes details about the
160 // child attributes.
161 std::stringstream styleableComment;
162 styleableComment << "Attributes that can be used with a " << className << ".\n";
163 styleableComment << "<table>\n"
164 "<colgroup align=\"left\" />\n"
165 "<colgroup align=\"left\">\n"
166 "<tr><th>Attribute</th><th>Description</th></tr>\n";
167 for (const auto& entry : sortedAttributes) {
168 const ResourceName& attrName = entry.attrRef->name.value();
169 styleableComment << "<tr><td><code>{@link #" << entry.fieldName << " "
170 << attrName.package << ":" << attrName.entry
171 << "}</code></td><td></td></tr>\n";
172 }
173 styleableComment << "</table>\n";
174 for (const auto& entry : sortedAttributes) {
175 styleableComment << "@see #" << entry.fieldName << "\n";
176 }
177 processor->appendComment(styleableComment.str());
178 }
179
180 auto accessorFunc = [](const StyleableAttr& a) -> ResourceId {
181 return a.attrRef->id ? a.attrRef->id.value() : ResourceId(0);
Adam Lesinskib274e352015-11-06 15:14:35 -0800182 };
183
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800184 // First we emit the array containing the IDs of each attribute.
Adam Lesinski74605cd2016-03-03 15:39:50 -0800185 outClassDef->addArrayMember(className, processor,
Adam Lesinskib274e352015-11-06 15:14:35 -0800186 sortedAttributes.begin(),
187 sortedAttributes.end(),
188 accessorFunc);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800189
190 // Now we emit the indices into the array.
191 for (size_t i = 0; i < attrCount; i++) {
Adam Lesinski74605cd2016-03-03 15:39:50 -0800192 const ResourceName& attrName = sortedAttributes[i].attrRef->name.value();
Adam Lesinski838a6872015-05-01 13:14:05 -0700193
Adam Lesinski74605cd2016-03-03 15:39:50 -0800194 AnnotationProcessor attrProcessor;
195 std::stringstream doclavaComments;
196 doclavaComments << "@attr name ";
197 if (!attrName.package.empty()) {
198 doclavaComments << attrName.package << ":";
Adam Lesinski838a6872015-05-01 13:14:05 -0700199 }
Adam Lesinski74605cd2016-03-03 15:39:50 -0800200 doclavaComments << attrName.entry;
201 attrProcessor.appendComment(doclavaComments.str());
202 outClassDef->addIntMember(sortedAttributes[i].fieldName, &attrProcessor, i);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800203 }
204}
205
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700206static void addAttributeFormatDoc(AnnotationProcessor* processor, Attribute* attr) {
207 const uint32_t typeMask = attr->typeMask;
208 if (typeMask & android::ResTable_map::TYPE_REFERENCE) {
209 processor->appendComment(
210 "<p>May be a reference to another resource, in the form\n"
211 "\"<code>@[+][<i>package</i>:]<i>type</i>/<i>name</i></code>\" or a theme\n"
212 "attribute in the form\n"
213 "\"<code>?[<i>package</i>:]<i>type</i>/<i>name</i></code>\".");
214 }
215
216 if (typeMask & android::ResTable_map::TYPE_STRING) {
217 processor->appendComment(
Adam Lesinskib274e352015-11-06 15:14:35 -0800218 "<p>May be a string value, using '\\\\;' to escape characters such as\n"
219 "'\\\\n' or '\\\\uxxxx' for a unicode character;");
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700220 }
221
222 if (typeMask & android::ResTable_map::TYPE_INTEGER) {
223 processor->appendComment("<p>May be an integer value, such as \"<code>100</code>\".");
224 }
225
226 if (typeMask & android::ResTable_map::TYPE_BOOLEAN) {
227 processor->appendComment(
228 "<p>May be a boolean value, such as \"<code>true</code>\" or\n"
229 "\"<code>false</code>\".");
230 }
231
232 if (typeMask & android::ResTable_map::TYPE_COLOR) {
233 processor->appendComment(
234 "<p>May be a color value, in the form of \"<code>#<i>rgb</i></code>\",\n"
235 "\"<code>#<i>argb</i></code>\", \"<code>#<i>rrggbb</i></code\", or \n"
236 "\"<code>#<i>aarrggbb</i></code>\".");
237 }
238
239 if (typeMask & android::ResTable_map::TYPE_FLOAT) {
240 processor->appendComment(
241 "<p>May be a floating point value, such as \"<code>1.2</code>\".");
242 }
243
244 if (typeMask & android::ResTable_map::TYPE_DIMENSION) {
245 processor->appendComment(
246 "<p>May be a dimension value, which is a floating point number appended with a\n"
247 "unit such as \"<code>14.5sp</code>\".\n"
248 "Available units are: px (pixels), dp (density-independent pixels),\n"
249 "sp (scaled pixels based on preferred font size), in (inches), and\n"
250 "mm (millimeters).");
251 }
252
253 if (typeMask & android::ResTable_map::TYPE_FRACTION) {
254 processor->appendComment(
255 "<p>May be a fractional value, which is a floating point number appended with\n"
256 "either % or %p, such as \"<code>14.5%</code>\".\n"
257 "The % suffix always means a percentage of the base size;\n"
258 "the optional %p suffix provides a size relative to some parent container.");
259 }
260
261 if (typeMask & (android::ResTable_map::TYPE_FLAGS | android::ResTable_map::TYPE_ENUM)) {
262 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
263 processor->appendComment(
264 "<p>Must be one or more (separated by '|') of the following "
265 "constant values.</p>");
266 } else {
267 processor->appendComment("<p>Must be one of the following constant values.</p>");
268 }
269
270 processor->appendComment("<table>\n<colgroup align=\"left\" />\n"
271 "<colgroup align=\"left\" />\n"
272 "<colgroup align=\"left\" />\n"
273 "<tr><th>Constant</th><th>Value</th><th>Description</th></tr>\n");
274 for (const Attribute::Symbol& symbol : attr->symbols) {
275 std::stringstream line;
276 line << "<tr><td>" << symbol.symbol.name.value().entry << "</td>"
277 << "<td>" << std::hex << symbol.value << std::dec << "</td>"
278 << "<td>" << util::trimWhitespace(symbol.symbol.getComment()) << "</td></tr>";
279 processor->appendComment(line.str());
280 }
281 processor->appendComment("</table>");
282 }
283}
284
Adam Lesinskib274e352015-11-06 15:14:35 -0800285bool JavaClassGenerator::writeEntriesForClass(ClassDefinitionWriter* outClassDef,
286 const StringPiece16& packageNameToGenerate,
287 const ResourceTablePackage* package,
288 const ResourceTableType* type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700289 for (const auto& entry : type->entries) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700290 if (skipSymbol(entry->symbolStatus.state)) {
291 continue;
292 }
293
Adam Lesinski64587af2016-02-18 18:33:06 -0800294 ResourceId id;
295 if (package->id && type->id && entry->id) {
296 id = ResourceId(package->id.value(), type->id.value(), entry->id.value());
297 }
Adam Lesinski769de982015-04-10 19:43:55 -0700298
Adam Lesinskib274e352015-11-06 15:14:35 -0800299 std::u16string unmangledPackage;
300 std::u16string unmangledName = entry->name;
Adam Lesinski769de982015-04-10 19:43:55 -0700301 if (NameMangler::unmangle(&unmangledName, &unmangledPackage)) {
302 // The entry name was mangled, and we successfully unmangled it.
303 // Check that we want to emit this symbol.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700304 if (package->name != unmangledPackage) {
Adam Lesinski769de982015-04-10 19:43:55 -0700305 // Skip the entry if it doesn't belong to the package we're writing.
306 continue;
307 }
Adam Lesinskib274e352015-11-06 15:14:35 -0800308 } else if (packageNameToGenerate != package->name) {
309 // We are processing a mangled package name,
310 // but this is a non-mangled resource.
311 continue;
Adam Lesinski769de982015-04-10 19:43:55 -0700312 }
313
314 if (!isValidSymbol(unmangledName)) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700315 ResourceNameRef resourceName(packageNameToGenerate, type->type, unmangledName);
Adam Lesinski769de982015-04-10 19:43:55 -0700316 std::stringstream err;
317 err << "invalid symbol name '" << resourceName << "'";
318 mError = err.str();
319 return false;
320 }
321
Adam Lesinskib274e352015-11-06 15:14:35 -0800322 // Build the comments and annotations for this entry.
323
324 AnnotationProcessor processor;
325 if (entry->symbolStatus.state != SymbolState::kUndefined) {
326 processor.appendComment(entry->symbolStatus.comment);
327 }
328
329 for (const auto& configValue : entry->values) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800330 processor.appendComment(configValue->value->getComment());
Adam Lesinskib274e352015-11-06 15:14:35 -0800331 }
332
333 // If this is an Attribute, append the format Javadoc.
334 if (!entry->values.empty()) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800335 if (Attribute* attr = valueCast<Attribute>(entry->values.front()->value.get())) {
Adam Lesinskib274e352015-11-06 15:14:35 -0800336 // We list out the available values for the given attribute.
337 addAttributeFormatDoc(&processor, attr);
338 }
339 }
340
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700341 if (type->type == ResourceType::kStyleable) {
Adam Lesinski769de982015-04-10 19:43:55 -0700342 assert(!entry->values.empty());
Adam Lesinskib274e352015-11-06 15:14:35 -0800343 const Styleable* styleable = static_cast<const Styleable*>(
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800344 entry->values.front()->value.get());
Adam Lesinskib274e352015-11-06 15:14:35 -0800345 writeStyleableEntryForClass(outClassDef, &processor, packageNameToGenerate,
346 unmangledName, styleable);
Adam Lesinski769de982015-04-10 19:43:55 -0700347 } else {
Adam Lesinskib274e352015-11-06 15:14:35 -0800348 outClassDef->addResourceMember(transform(unmangledName), &processor, id);
Adam Lesinski769de982015-04-10 19:43:55 -0700349 }
350 }
351 return true;
352}
353
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700354bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, std::ostream* out) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700355 return generate(packageNameToGenerate, packageNameToGenerate, out);
356}
357
358bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate,
359 const StringPiece16& outPackageName, std::ostream* out) {
360 generateHeader(outPackageName, out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800361
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700362 *out << "public final class R {\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800363
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700364 for (const auto& package : mTable->packages) {
365 for (const auto& type : package->types) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700366 if (type->type == ResourceType::kAttrPrivate) {
Adam Lesinskib274e352015-11-06 15:14:35 -0800367 continue;
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700368 }
Adam Lesinskib274e352015-11-06 15:14:35 -0800369
370 ClassDefinitionWriterOptions classOptions;
371 classOptions.useFinalQualifier = mOptions.useFinal;
372 classOptions.forceCreationIfEmpty =
373 (mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic);
374 ClassDefinitionWriter classDef(toString(type->type), classOptions);
375 bool result = writeEntriesForClass(&classDef, packageNameToGenerate,
376 package.get(), type.get());
377 if (!result) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700378 return false;
379 }
Adam Lesinskib274e352015-11-06 15:14:35 -0800380
381 if (type->type == ResourceType::kAttr) {
382 // Also include private attributes in this same class.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800383 ResourceTableType* privType = package->findType(ResourceType::kAttrPrivate);
384 if (privType) {
Adam Lesinskib274e352015-11-06 15:14:35 -0800385 result = writeEntriesForClass(&classDef, packageNameToGenerate,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800386 package.get(), privType);
Adam Lesinskib274e352015-11-06 15:14:35 -0800387 if (!result) {
388 return false;
389 }
390 }
391 }
392
393 AnnotationProcessor processor;
394 if (type->type == ResourceType::kStyleable &&
395 mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic) {
396 // When generating a public R class, we don't want Styleable to be part of the API.
397 // It is only emitted for documentation purposes.
398 processor.appendComment("@doconly");
399 }
400 classDef.writeToStream(out, " ", &processor);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800401 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800402 }
403
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700404 *out << "}\n";
405 out->flush();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800406 return true;
407}
408
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700409
410
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800411} // namespace aapt