blob: 092bab241bba811afbe699289f4a251e87b6b691 [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 Lesinski6cbfb1d2016-03-31 13:33:02 -070024#include "java/ClassDefinition.h"
Adam Lesinskica5638f2015-10-21 14:42:43 -070025#include "java/JavaClassGenerator.h"
Adam Lesinski76565542016-03-10 21:55:04 -080026#include "process/SymbolTable.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027#include "util/StringPiece.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028
Adam Lesinskica2fc352015-04-03 12:08:26 -070029#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080030#include <ostream>
31#include <set>
32#include <sstream>
33#include <tuple>
34
35namespace aapt {
36
Adam Lesinski76565542016-03-10 21:55:04 -080037JavaClassGenerator::JavaClassGenerator(IAaptContext* context, ResourceTable* table,
38 const JavaClassGeneratorOptions& options) :
39 mContext(context), mTable(table), mOptions(options) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040}
41
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042static const std::set<StringPiece16> sJavaIdentifiers = {
43 u"abstract", u"assert", u"boolean", u"break", u"byte",
44 u"case", u"catch", u"char", u"class", u"const", u"continue",
45 u"default", u"do", u"double", u"else", u"enum", u"extends",
46 u"final", u"finally", u"float", u"for", u"goto", u"if",
47 u"implements", u"import", u"instanceof", u"int", u"interface",
48 u"long", u"native", u"new", u"package", u"private", u"protected",
49 u"public", u"return", u"short", u"static", u"strictfp", u"super",
50 u"switch", u"synchronized", u"this", u"throw", u"throws",
51 u"transient", u"try", u"void", u"volatile", u"while", u"true",
52 u"false", u"null"
53};
54
55static bool isValidSymbol(const StringPiece16& symbol) {
56 return sJavaIdentifiers.find(symbol) == sJavaIdentifiers.end();
57}
58
59/*
60 * Java symbols can not contain . or -, but those are valid in a resource name.
61 * Replace those with '_'.
62 */
Adam Lesinski74605cd2016-03-03 15:39:50 -080063static std::string transform(const StringPiece16& symbol) {
64 std::string output = util::utf16ToUtf8(symbol);
65 for (char& c : output) {
66 if (c == '.' || c == '-') {
67 c = '_';
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068 }
69 }
70 return output;
71}
72
Adam Lesinski74605cd2016-03-03 15:39:50 -080073/**
74 * Transforms an attribute in a styleable to the Java field name:
75 *
76 * <declare-styleable name="Foo">
77 * <attr name="android:bar" />
78 * <attr name="bar" />
79 * </declare-styleable>
80 *
81 * Foo_android_bar
82 * Foo_bar
83 */
84static std::string transformNestedAttr(const ResourceNameRef& attrName,
85 const std::string& styleableClassName,
86 const StringPiece16& packageNameToGenerate) {
87 std::string output = styleableClassName;
88
89 // We may reference IDs from other packages, so prefix the entry name with
90 // the package.
91 if (!attrName.package.empty() && packageNameToGenerate != attrName.package) {
92 output += "_" + transform(attrName.package);
93 }
94 output += "_" + transform(attrName.entry);
95 return output;
96}
97
Adam Lesinski76565542016-03-10 21:55:04 -080098static void addAttributeFormatDoc(AnnotationProcessor* processor, Attribute* attr) {
99 const uint32_t typeMask = attr->typeMask;
100 if (typeMask & android::ResTable_map::TYPE_REFERENCE) {
101 processor->appendComment(
102 "<p>May be a reference to another resource, in the form\n"
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700103 "\"<code>@[+][<i>package</i>:]<i>type</i>/<i>name</i></code>\" or a theme\n"
104 "attribute in the form\n"
105 "\"<code>?[<i>package</i>:]<i>type</i>/<i>name</i></code>\".");
Adam Lesinski76565542016-03-10 21:55:04 -0800106 }
107
108 if (typeMask & android::ResTable_map::TYPE_STRING) {
109 processor->appendComment(
110 "<p>May be a string value, using '\\\\;' to escape characters such as\n"
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700111 "'\\\\n' or '\\\\uxxxx' for a unicode character;");
Adam Lesinski76565542016-03-10 21:55:04 -0800112 }
113
114 if (typeMask & android::ResTable_map::TYPE_INTEGER) {
115 processor->appendComment("<p>May be an integer value, such as \"<code>100</code>\".");
116 }
117
118 if (typeMask & android::ResTable_map::TYPE_BOOLEAN) {
119 processor->appendComment(
120 "<p>May be a boolean value, such as \"<code>true</code>\" or\n"
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700121 "\"<code>false</code>\".");
Adam Lesinski76565542016-03-10 21:55:04 -0800122 }
123
124 if (typeMask & android::ResTable_map::TYPE_COLOR) {
125 processor->appendComment(
126 "<p>May be a color value, in the form of \"<code>#<i>rgb</i></code>\",\n"
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700127 "\"<code>#<i>argb</i></code>\", \"<code>#<i>rrggbb</i></code\", or \n"
128 "\"<code>#<i>aarrggbb</i></code>\".");
Adam Lesinski76565542016-03-10 21:55:04 -0800129 }
130
131 if (typeMask & android::ResTable_map::TYPE_FLOAT) {
132 processor->appendComment(
133 "<p>May be a floating point value, such as \"<code>1.2</code>\".");
134 }
135
136 if (typeMask & android::ResTable_map::TYPE_DIMENSION) {
137 processor->appendComment(
138 "<p>May be a dimension value, which is a floating point number appended with a\n"
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700139 "unit such as \"<code>14.5sp</code>\".\n"
140 "Available units are: px (pixels), dp (density-independent pixels),\n"
141 "sp (scaled pixels based on preferred font size), in (inches), and\n"
142 "mm (millimeters).");
Adam Lesinski76565542016-03-10 21:55:04 -0800143 }
144
145 if (typeMask & android::ResTable_map::TYPE_FRACTION) {
146 processor->appendComment(
147 "<p>May be a fractional value, which is a floating point number appended with\n"
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700148 "either % or %p, such as \"<code>14.5%</code>\".\n"
149 "The % suffix always means a percentage of the base size;\n"
150 "the optional %p suffix provides a size relative to some parent container.");
Adam Lesinski76565542016-03-10 21:55:04 -0800151 }
152
153 if (typeMask & (android::ResTable_map::TYPE_FLAGS | android::ResTable_map::TYPE_ENUM)) {
154 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
155 processor->appendComment(
156 "<p>Must be one or more (separated by '|') of the following "
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700157 "constant values.</p>");
Adam Lesinski76565542016-03-10 21:55:04 -0800158 } else {
159 processor->appendComment("<p>Must be one of the following constant values.</p>");
160 }
161
162 processor->appendComment("<table>\n<colgroup align=\"left\" />\n"
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700163 "<colgroup align=\"left\" />\n"
164 "<colgroup align=\"left\" />\n"
165 "<tr><th>Constant</th><th>Value</th><th>Description</th></tr>\n");
Adam Lesinski76565542016-03-10 21:55:04 -0800166 for (const Attribute::Symbol& symbol : attr->symbols) {
167 std::stringstream line;
168 line << "<tr><td>" << symbol.symbol.name.value().entry << "</td>"
169 << "<td>" << std::hex << symbol.value << std::dec << "</td>"
170 << "<td>" << util::trimWhitespace(symbol.symbol.getComment()) << "</td></tr>";
171 processor->appendComment(line.str());
172 }
173 processor->appendComment("</table>");
174 }
175}
176
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700177bool JavaClassGenerator::skipSymbol(SymbolState state) {
178 switch (mOptions.types) {
179 case JavaClassGeneratorOptions::SymbolTypes::kAll:
180 return false;
181 case JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate:
182 return state == SymbolState::kUndefined;
183 case JavaClassGeneratorOptions::SymbolTypes::kPublic:
184 return state != SymbolState::kPublic;
185 }
186 return true;
187}
188
Adam Lesinski74605cd2016-03-03 15:39:50 -0800189struct StyleableAttr {
190 const Reference* attrRef;
Adam Lesinski76565542016-03-10 21:55:04 -0800191 std::shared_ptr<Attribute> attribute;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800192 std::string fieldName;
193};
194
195static bool lessStyleableAttr(const StyleableAttr& lhs, const StyleableAttr& rhs) {
196 const ResourceId lhsId = lhs.attrRef->id ? lhs.attrRef->id.value() : ResourceId(0);
197 const ResourceId rhsId = rhs.attrRef->id ? rhs.attrRef->id.value() : ResourceId(0);
198 if (lhsId < rhsId) {
199 return true;
200 } else if (lhsId > rhsId) {
201 return false;
202 } else {
203 return lhs.attrRef->name.value() < rhs.attrRef->name.value();
204 }
205}
206
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700207void JavaClassGenerator::addMembersToStyleableClass(const StringPiece16& packageNameToGenerate,
208 const std::u16string& entryName,
209 const Styleable* styleable,
210 ClassDefinition* outStyleableClassDef) {
Adam Lesinski74605cd2016-03-03 15:39:50 -0800211 const std::string className = transform(entryName);
212
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700213 std::unique_ptr<ResourceArrayMember> styleableArrayDef =
214 util::make_unique<ResourceArrayMember>(className);
215
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800216 // This must be sorted by resource ID.
Adam Lesinski74605cd2016-03-03 15:39:50 -0800217 std::vector<StyleableAttr> sortedAttributes;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700218 sortedAttributes.reserve(styleable->entries.size());
219 for (const auto& attr : styleable->entries) {
Adam Lesinski330edcd2015-05-04 17:40:56 -0700220 // If we are not encoding final attributes, the styleable entry may have no ID
221 // if we are building a static library.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700222 assert((!mOptions.useFinal || attr.id) && "no ID set for Styleable entry");
223 assert(attr.name && "no name set for Styleable entry");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800224
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700225 // We will need the unmangled, transformed name in the comments and the field,
226 // so create it once and cache it in this StyleableAttr data structure.
Adam Lesinski76565542016-03-10 21:55:04 -0800227 StyleableAttr styleableAttr = {};
228 styleableAttr.attrRef = &attr;
229 styleableAttr.fieldName = transformNestedAttr(attr.name.value(), className,
230 packageNameToGenerate);
231
232 Reference mangledReference;
233 mangledReference.id = attr.id;
234 mangledReference.name = attr.name;
235 if (mangledReference.name.value().package.empty()) {
236 mangledReference.name.value().package = mContext->getCompilationPackage();
237 }
238
239 if (Maybe<ResourceName> mangledName =
240 mContext->getNameMangler()->mangleName(mangledReference.name.value())) {
241 mangledReference.name = mangledName;
242 }
243
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700244 // Look up the symbol so that we can write out in the comments what are possible
245 // legal values for this attribute.
Adam Lesinski76565542016-03-10 21:55:04 -0800246 const SymbolTable::Symbol* symbol = mContext->getExternalSymbols()->findByReference(
247 mangledReference);
248 if (symbol) {
249 styleableAttr.attribute = symbol->attribute;
250 }
251 sortedAttributes.push_back(std::move(styleableAttr));
Adam Lesinski74605cd2016-03-03 15:39:50 -0800252 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700253
254 // Sort the attributes by ID.
Adam Lesinski74605cd2016-03-03 15:39:50 -0800255 std::sort(sortedAttributes.begin(), sortedAttributes.end(), lessStyleableAttr);
256
257 const size_t attrCount = sortedAttributes.size();
Adam Lesinski74605cd2016-03-03 15:39:50 -0800258 if (attrCount > 0) {
259 // Build the comment string for the Styleable. It includes details about the
260 // child attributes.
261 std::stringstream styleableComment;
Adam Lesinski76565542016-03-10 21:55:04 -0800262 if (!styleable->getComment().empty()) {
263 styleableComment << styleable->getComment() << "\n";
264 } else {
265 styleableComment << "Attributes that can be used with a " << className << ".\n";
266 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700267
Adam Lesinski76565542016-03-10 21:55:04 -0800268 styleableComment <<
269 "<p>Includes the following attributes:</p>\n"
270 "<table>\n"
Adam Lesinski74605cd2016-03-03 15:39:50 -0800271 "<colgroup align=\"left\" />\n"
Adam Lesinski76565542016-03-10 21:55:04 -0800272 "<colgroup align=\"left\" />\n"
Adam Lesinski74605cd2016-03-03 15:39:50 -0800273 "<tr><th>Attribute</th><th>Description</th></tr>\n";
Adam Lesinski76565542016-03-10 21:55:04 -0800274
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700275 for (const StyleableAttr& entry : sortedAttributes) {
Adam Lesinski74605cd2016-03-03 15:39:50 -0800276 const ResourceName& attrName = entry.attrRef->name.value();
Adam Lesinski76565542016-03-10 21:55:04 -0800277 styleableComment << "<tr><td>";
278 styleableComment << "<code>{@link #"
279 << entry.fieldName << " "
280 << (!attrName.package.empty()
281 ? attrName.package : mContext->getCompilationPackage())
282 << ":" << attrName.entry
283 << "}</code>";
284 styleableComment << "</td>";
285
286 styleableComment << "<td>";
287 if (entry.attribute) {
288 styleableComment << entry.attribute->getComment();
289 }
290 styleableComment << "</td></tr>\n";
Adam Lesinski74605cd2016-03-03 15:39:50 -0800291 }
292 styleableComment << "</table>\n";
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700293
294 for (const StyleableAttr& entry : sortedAttributes) {
Adam Lesinski74605cd2016-03-03 15:39:50 -0800295 styleableComment << "@see #" << entry.fieldName << "\n";
296 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700297
298 styleableArrayDef->getCommentBuilder()->appendComment(styleableComment.str());
Adam Lesinski74605cd2016-03-03 15:39:50 -0800299 }
300
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700301 // Add the ResourceIds to the array member.
302 for (const StyleableAttr& styleableAttr : sortedAttributes) {
303 styleableArrayDef->addElement(
304 styleableAttr.attrRef->id ? styleableAttr.attrRef->id.value() : ResourceId(0));
305 }
Adam Lesinskib274e352015-11-06 15:14:35 -0800306
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700307 // Add the Styleable array to the Styleable class.
308 outStyleableClassDef->addMember(std::move(styleableArrayDef));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800309
310 // Now we emit the indices into the array.
311 for (size_t i = 0; i < attrCount; i++) {
Adam Lesinski76565542016-03-10 21:55:04 -0800312 const StyleableAttr& styleableAttr = sortedAttributes[i];
313 const ResourceName& attrName = styleableAttr.attrRef->name.value();
314
315 StringPiece16 packageName = attrName.package;
316 if (packageName.empty()) {
317 packageName = mContext->getCompilationPackage();
318 }
Adam Lesinski838a6872015-05-01 13:14:05 -0700319
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700320 std::unique_ptr<IntMember> indexMember = util::make_unique<IntMember>(
321 sortedAttributes[i].fieldName, i);
322
323 AnnotationProcessor* attrProcessor = indexMember->getCommentBuilder();
Adam Lesinski76565542016-03-10 21:55:04 -0800324
325 StringPiece16 comment = styleableAttr.attrRef->getComment();
326 if (styleableAttr.attribute && comment.empty()) {
327 comment = styleableAttr.attribute->getComment();
Adam Lesinski838a6872015-05-01 13:14:05 -0700328 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800329
Adam Lesinski76565542016-03-10 21:55:04 -0800330 if (!comment.empty()) {
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700331 attrProcessor->appendComment("<p>\n@attr description");
332 attrProcessor->appendComment(comment);
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700333 } else {
Adam Lesinski76565542016-03-10 21:55:04 -0800334 std::stringstream defaultComment;
335 defaultComment
336 << "<p>This symbol is the offset where the "
337 << "{@link " << packageName << ".R.attr#" << transform(attrName.entry) << "}\n"
338 << "attribute's value can be found in the "
339 << "{@link #" << className << "} array.";
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700340 attrProcessor->appendComment(defaultComment.str());
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700341 }
342
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700343 attrProcessor->appendNewLine();
Adam Lesinski76565542016-03-10 21:55:04 -0800344
345 if (styleableAttr.attribute) {
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700346 addAttributeFormatDoc(attrProcessor, styleableAttr.attribute.get());
347 attrProcessor->appendNewLine();
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700348 }
Adam Lesinski76565542016-03-10 21:55:04 -0800349
350 std::stringstream doclavaName;
351 doclavaName << "@attr name " << packageName << ":" << attrName.entry;;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700352 attrProcessor->appendComment(doclavaName.str());
353
354 outStyleableClassDef->addMember(std::move(indexMember));
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700355 }
356}
357
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700358bool JavaClassGenerator::addMembersToTypeClass(const StringPiece16& packageNameToGenerate,
359 const ResourceTablePackage* package,
360 const ResourceTableType* type,
361 ClassDefinition* outTypeClassDef) {
362
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700363 for (const auto& entry : type->entries) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700364 if (skipSymbol(entry->symbolStatus.state)) {
365 continue;
366 }
367
Adam Lesinski64587af2016-02-18 18:33:06 -0800368 ResourceId id;
369 if (package->id && type->id && entry->id) {
370 id = ResourceId(package->id.value(), type->id.value(), entry->id.value());
371 }
Adam Lesinski769de982015-04-10 19:43:55 -0700372
Adam Lesinskib274e352015-11-06 15:14:35 -0800373 std::u16string unmangledPackage;
374 std::u16string unmangledName = entry->name;
Adam Lesinski769de982015-04-10 19:43:55 -0700375 if (NameMangler::unmangle(&unmangledName, &unmangledPackage)) {
376 // The entry name was mangled, and we successfully unmangled it.
377 // Check that we want to emit this symbol.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700378 if (package->name != unmangledPackage) {
Adam Lesinski769de982015-04-10 19:43:55 -0700379 // Skip the entry if it doesn't belong to the package we're writing.
380 continue;
381 }
Adam Lesinskib274e352015-11-06 15:14:35 -0800382 } else if (packageNameToGenerate != package->name) {
383 // We are processing a mangled package name,
384 // but this is a non-mangled resource.
385 continue;
Adam Lesinski769de982015-04-10 19:43:55 -0700386 }
387
388 if (!isValidSymbol(unmangledName)) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700389 ResourceNameRef resourceName(packageNameToGenerate, type->type, unmangledName);
Adam Lesinski769de982015-04-10 19:43:55 -0700390 std::stringstream err;
391 err << "invalid symbol name '" << resourceName << "'";
392 mError = err.str();
393 return false;
394 }
395
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700396 if (type->type == ResourceType::kStyleable) {
Adam Lesinski769de982015-04-10 19:43:55 -0700397 assert(!entry->values.empty());
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700398
Adam Lesinskib274e352015-11-06 15:14:35 -0800399 const Styleable* styleable = static_cast<const Styleable*>(
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800400 entry->values.front()->value.get());
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700401
402 // Comments are handled within this method.
403 addMembersToStyleableClass(packageNameToGenerate, unmangledName, styleable,
404 outTypeClassDef);
Adam Lesinski769de982015-04-10 19:43:55 -0700405 } else {
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700406 std::unique_ptr<ResourceMember> resourceMember =
407 util::make_unique<ResourceMember>(transform(unmangledName), id);
408
409 // Build the comments and annotations for this entry.
410 AnnotationProcessor* processor = resourceMember->getCommentBuilder();
411
412 // Add the comments from any <public> tags.
413 if (entry->symbolStatus.state != SymbolState::kUndefined) {
414 processor->appendComment(entry->symbolStatus.comment);
415 }
416
417 // Add the comments from all configurations of this entry.
418 for (const auto& configValue : entry->values) {
419 processor->appendComment(configValue->value->getComment());
420 }
421
422 // If this is an Attribute, append the format Javadoc.
423 if (!entry->values.empty()) {
424 if (Attribute* attr = valueCast<Attribute>(entry->values.front()->value.get())) {
425 // We list out the available values for the given attribute.
426 addAttributeFormatDoc(processor, attr);
427 }
428 }
429
430 outTypeClassDef->addMember(std::move(resourceMember));
Adam Lesinski769de982015-04-10 19:43:55 -0700431 }
432 }
433 return true;
434}
435
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700436bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, std::ostream* out) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700437 return generate(packageNameToGenerate, packageNameToGenerate, out);
438}
439
Adam Lesinski3524a232016-04-01 19:19:24 -0700440static void appendJavaDocAnnotations(const std::vector<std::string>& annotations,
441 AnnotationProcessor* processor) {
442 for (const std::string& annotation : annotations) {
443 std::string properAnnotation = "@";
444 properAnnotation += annotation;
445 processor->appendComment(properAnnotation);
446 }
447}
448
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700449bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate,
450 const StringPiece16& outPackageName, std::ostream* out) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800451
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700452 ClassDefinition rClass("R", ClassQualifier::None, true);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800453
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700454 for (const auto& package : mTable->packages) {
455 for (const auto& type : package->types) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700456 if (type->type == ResourceType::kAttrPrivate) {
Adam Lesinskib274e352015-11-06 15:14:35 -0800457 continue;
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700458 }
Adam Lesinskib274e352015-11-06 15:14:35 -0800459
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700460 const bool forceCreationIfEmpty =
Adam Lesinskib274e352015-11-06 15:14:35 -0800461 (mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700462
463 std::unique_ptr<ClassDefinition> classDef = util::make_unique<ClassDefinition>(
464 util::utf16ToUtf8(toString(type->type)), ClassQualifier::Static,
465 forceCreationIfEmpty);
466
467 bool result = addMembersToTypeClass(packageNameToGenerate, package.get(), type.get(),
468 classDef.get());
Adam Lesinskib274e352015-11-06 15:14:35 -0800469 if (!result) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700470 return false;
471 }
Adam Lesinskib274e352015-11-06 15:14:35 -0800472
473 if (type->type == ResourceType::kAttr) {
474 // Also include private attributes in this same class.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800475 ResourceTableType* privType = package->findType(ResourceType::kAttrPrivate);
476 if (privType) {
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700477 result = addMembersToTypeClass(packageNameToGenerate, package.get(), privType,
478 classDef.get());
Adam Lesinskib274e352015-11-06 15:14:35 -0800479 if (!result) {
480 return false;
481 }
482 }
483 }
484
Adam Lesinskib274e352015-11-06 15:14:35 -0800485 if (type->type == ResourceType::kStyleable &&
486 mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic) {
487 // When generating a public R class, we don't want Styleable to be part of the API.
488 // It is only emitted for documentation purposes.
Adam Lesinski3524a232016-04-01 19:19:24 -0700489 classDef->getCommentBuilder()->appendComment("@doconly");
Adam Lesinskib274e352015-11-06 15:14:35 -0800490 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700491
Adam Lesinski3524a232016-04-01 19:19:24 -0700492 appendJavaDocAnnotations(mOptions.javadocAnnotations, classDef->getCommentBuilder());
493
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700494 rClass.addMember(std::move(classDef));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800495 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800496 }
497
Adam Lesinski3524a232016-04-01 19:19:24 -0700498 appendJavaDocAnnotations(mOptions.javadocAnnotations, rClass.getCommentBuilder());
499
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700500 if (!ClassDefinition::writeJavaFile(&rClass, util::utf16ToUtf8(outPackageName),
501 mOptions.useFinal, out)) {
502 return false;
503 }
504
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700505 out->flush();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800506 return true;
507}
508
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800509} // namespace aapt