| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 17 | #include "NameMangler.h" |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 18 | #include "Resource.h" |
| 19 | #include "ResourceTable.h" |
| 20 | #include "ResourceValues.h" |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 21 | #include "ValueVisitor.h" |
| 22 | |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 23 | #include "java/AnnotationProcessor.h" |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 24 | #include "java/ClassDefinitionWriter.h" |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 25 | #include "java/JavaClassGenerator.h" |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 26 | #include "util/StringPiece.h" |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 27 | |
| Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 28 | #include <algorithm> |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 29 | #include <ostream> |
| 30 | #include <set> |
| 31 | #include <sstream> |
| 32 | #include <tuple> |
| 33 | |
| 34 | namespace aapt { |
| 35 | |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 36 | JavaClassGenerator::JavaClassGenerator(ResourceTable* table, JavaClassGeneratorOptions options) : |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 37 | mTable(table), mOptions(options) { |
| 38 | } |
| 39 | |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 40 | static 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static 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 | |
| 63 | static 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 Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame^] | 71 | static 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | return output; |
| 79 | } |
| 80 | |
| Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame^] | 81 | /** |
| 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 | */ |
| 92 | static 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 Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 106 | bool 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 Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame^] | 118 | struct StyleableAttr { |
| 119 | const Reference* attrRef; |
| 120 | std::string fieldName; |
| 121 | }; |
| 122 | |
| 123 | static 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 Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 135 | void JavaClassGenerator::writeStyleableEntryForClass(ClassDefinitionWriter* outClassDef, |
| 136 | AnnotationProcessor* processor, |
| 137 | const StringPiece16& packageNameToGenerate, |
| 138 | const std::u16string& entryName, |
| 139 | const Styleable* styleable) { |
| Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame^] | 140 | const std::string className = transform(entryName); |
| 141 | |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 142 | // This must be sorted by resource ID. |
| Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame^] | 143 | std::vector<StyleableAttr> sortedAttributes; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 144 | sortedAttributes.reserve(styleable->entries.size()); |
| 145 | for (const auto& attr : styleable->entries) { |
| Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 146 | // If we are not encoding final attributes, the styleable entry may have no ID |
| 147 | // if we are building a static library. |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 148 | assert((!mOptions.useFinal || attr.id) && "no ID set for Styleable entry"); |
| 149 | assert(attr.name && "no name set for Styleable entry"); |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 150 | |
| Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame^] | 151 | 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 Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 182 | }; |
| 183 | |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 184 | // First we emit the array containing the IDs of each attribute. |
| Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame^] | 185 | outClassDef->addArrayMember(className, processor, |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 186 | sortedAttributes.begin(), |
| 187 | sortedAttributes.end(), |
| 188 | accessorFunc); |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 189 | |
| 190 | // Now we emit the indices into the array. |
| 191 | for (size_t i = 0; i < attrCount; i++) { |
| Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame^] | 192 | const ResourceName& attrName = sortedAttributes[i].attrRef->name.value(); |
| Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 193 | |
| Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame^] | 194 | AnnotationProcessor attrProcessor; |
| 195 | std::stringstream doclavaComments; |
| 196 | doclavaComments << "@attr name "; |
| 197 | if (!attrName.package.empty()) { |
| 198 | doclavaComments << attrName.package << ":"; |
| Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 199 | } |
| Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame^] | 200 | doclavaComments << attrName.entry; |
| 201 | attrProcessor.appendComment(doclavaComments.str()); |
| 202 | outClassDef->addIntMember(sortedAttributes[i].fieldName, &attrProcessor, i); |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 206 | static 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 Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 218 | "<p>May be a string value, using '\\\\;' to escape characters such as\n" |
| 219 | "'\\\\n' or '\\\\uxxxx' for a unicode character;"); |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 220 | } |
| 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 Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 285 | bool JavaClassGenerator::writeEntriesForClass(ClassDefinitionWriter* outClassDef, |
| 286 | const StringPiece16& packageNameToGenerate, |
| 287 | const ResourceTablePackage* package, |
| 288 | const ResourceTableType* type) { |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 289 | for (const auto& entry : type->entries) { |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 290 | if (skipSymbol(entry->symbolStatus.state)) { |
| 291 | continue; |
| 292 | } |
| 293 | |
| Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 294 | ResourceId id; |
| 295 | if (package->id && type->id && entry->id) { |
| 296 | id = ResourceId(package->id.value(), type->id.value(), entry->id.value()); |
| 297 | } |
| Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 298 | |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 299 | std::u16string unmangledPackage; |
| 300 | std::u16string unmangledName = entry->name; |
| Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 301 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 304 | if (package->name != unmangledPackage) { |
| Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 305 | // Skip the entry if it doesn't belong to the package we're writing. |
| 306 | continue; |
| 307 | } |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 308 | } else if (packageNameToGenerate != package->name) { |
| 309 | // We are processing a mangled package name, |
| 310 | // but this is a non-mangled resource. |
| 311 | continue; |
| Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | if (!isValidSymbol(unmangledName)) { |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 315 | ResourceNameRef resourceName(packageNameToGenerate, type->type, unmangledName); |
| Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 316 | std::stringstream err; |
| 317 | err << "invalid symbol name '" << resourceName << "'"; |
| 318 | mError = err.str(); |
| 319 | return false; |
| 320 | } |
| 321 | |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 322 | // 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 Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 330 | processor.appendComment(configValue->value->getComment()); |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | // If this is an Attribute, append the format Javadoc. |
| 334 | if (!entry->values.empty()) { |
| Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 335 | if (Attribute* attr = valueCast<Attribute>(entry->values.front()->value.get())) { |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 336 | // We list out the available values for the given attribute. |
| 337 | addAttributeFormatDoc(&processor, attr); |
| 338 | } |
| 339 | } |
| 340 | |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 341 | if (type->type == ResourceType::kStyleable) { |
| Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 342 | assert(!entry->values.empty()); |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 343 | const Styleable* styleable = static_cast<const Styleable*>( |
| Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 344 | entry->values.front()->value.get()); |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 345 | writeStyleableEntryForClass(outClassDef, &processor, packageNameToGenerate, |
| 346 | unmangledName, styleable); |
| Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 347 | } else { |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 348 | outClassDef->addResourceMember(transform(unmangledName), &processor, id); |
| Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | return true; |
| 352 | } |
| 353 | |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 354 | bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, std::ostream* out) { |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 355 | return generate(packageNameToGenerate, packageNameToGenerate, out); |
| 356 | } |
| 357 | |
| 358 | bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, |
| 359 | const StringPiece16& outPackageName, std::ostream* out) { |
| 360 | generateHeader(outPackageName, out); |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 361 | |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 362 | *out << "public final class R {\n"; |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 363 | |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 364 | for (const auto& package : mTable->packages) { |
| 365 | for (const auto& type : package->types) { |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 366 | if (type->type == ResourceType::kAttrPrivate) { |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 367 | continue; |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 368 | } |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 369 | |
| 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 378 | return false; |
| 379 | } |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 380 | |
| 381 | if (type->type == ResourceType::kAttr) { |
| 382 | // Also include private attributes in this same class. |
| Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 383 | ResourceTableType* privType = package->findType(ResourceType::kAttrPrivate); |
| 384 | if (privType) { |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 385 | result = writeEntriesForClass(&classDef, packageNameToGenerate, |
| Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 386 | package.get(), privType); |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 387 | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 401 | } |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 402 | } |
| 403 | |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 404 | *out << "}\n"; |
| 405 | out->flush(); |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 406 | return true; |
| 407 | } |
| 408 | |
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 409 | |
| 410 | |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 411 | } // namespace aapt |