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" |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 22 | #include "java/AnnotationProcessor.h" |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 23 | #include "java/ClassDefinition.h" |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 24 | #include "java/JavaClassGenerator.h" |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 25 | #include "process/SymbolTable.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 | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 36 | JavaClassGenerator::JavaClassGenerator(IAaptContext* context, ResourceTable* table, |
| 37 | const JavaClassGeneratorOptions& options) : |
| 38 | mContext(context), mTable(table), mOptions(options) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 41 | static const std::set<StringPiece> sJavaIdentifiers = { |
| 42 | "abstract", "assert", "boolean", "break", "byte", |
| 43 | "case", "catch", "char", "class", "const", "continue", |
| 44 | "default", "do", "double", "else", "enum", "extends", |
| 45 | "final", "finally", "float", "for", "goto", "if", |
| 46 | "implements", "import", "instanceof", "int", "interface", |
| 47 | "long", "native", "new", "package", "private", "protected", |
| 48 | "public", "return", "short", "static", "strictfp", "super", |
| 49 | "switch", "synchronized", "this", "throw", "throws", |
| 50 | "transient", "try", "void", "volatile", "while", "true", |
| 51 | "false", "null" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 52 | }; |
| 53 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 54 | static bool isValidSymbol(const StringPiece& symbol) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 55 | return sJavaIdentifiers.find(symbol) == sJavaIdentifiers.end(); |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * Java symbols can not contain . or -, but those are valid in a resource name. |
| 60 | * Replace those with '_'. |
| 61 | */ |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 62 | static std::string transform(const StringPiece& symbol) { |
| 63 | std::string output = symbol.toString(); |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 64 | for (char& c : output) { |
| 65 | if (c == '.' || c == '-') { |
| 66 | c = '_'; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | return output; |
| 70 | } |
| 71 | |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 72 | /** |
| 73 | * Transforms an attribute in a styleable to the Java field name: |
| 74 | * |
| 75 | * <declare-styleable name="Foo"> |
| 76 | * <attr name="android:bar" /> |
| 77 | * <attr name="bar" /> |
| 78 | * </declare-styleable> |
| 79 | * |
| 80 | * Foo_android_bar |
| 81 | * Foo_bar |
| 82 | */ |
| 83 | static std::string transformNestedAttr(const ResourceNameRef& attrName, |
| 84 | const std::string& styleableClassName, |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 85 | const StringPiece& packageNameToGenerate) { |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 86 | std::string output = styleableClassName; |
| 87 | |
| 88 | // We may reference IDs from other packages, so prefix the entry name with |
| 89 | // the package. |
| 90 | if (!attrName.package.empty() && packageNameToGenerate != attrName.package) { |
| 91 | output += "_" + transform(attrName.package); |
| 92 | } |
| 93 | output += "_" + transform(attrName.entry); |
| 94 | return output; |
| 95 | } |
| 96 | |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 97 | static void addAttributeFormatDoc(AnnotationProcessor* processor, Attribute* attr) { |
| 98 | const uint32_t typeMask = attr->typeMask; |
| 99 | if (typeMask & android::ResTable_map::TYPE_REFERENCE) { |
| 100 | processor->appendComment( |
| 101 | "<p>May be a reference to another resource, in the form\n" |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 102 | "\"<code>@[+][<i>package</i>:]<i>type</i>/<i>name</i></code>\" or a theme\n" |
| 103 | "attribute in the form\n" |
| 104 | "\"<code>?[<i>package</i>:]<i>type</i>/<i>name</i></code>\"."); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | if (typeMask & android::ResTable_map::TYPE_STRING) { |
| 108 | processor->appendComment( |
| 109 | "<p>May be a string value, using '\\\\;' to escape characters such as\n" |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 110 | "'\\\\n' or '\\\\uxxxx' for a unicode character;"); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | if (typeMask & android::ResTable_map::TYPE_INTEGER) { |
| 114 | processor->appendComment("<p>May be an integer value, such as \"<code>100</code>\"."); |
| 115 | } |
| 116 | |
| 117 | if (typeMask & android::ResTable_map::TYPE_BOOLEAN) { |
| 118 | processor->appendComment( |
| 119 | "<p>May be a boolean value, such as \"<code>true</code>\" or\n" |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 120 | "\"<code>false</code>\"."); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | if (typeMask & android::ResTable_map::TYPE_COLOR) { |
| 124 | processor->appendComment( |
| 125 | "<p>May be a color value, in the form of \"<code>#<i>rgb</i></code>\",\n" |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 126 | "\"<code>#<i>argb</i></code>\", \"<code>#<i>rrggbb</i></code\", or \n" |
| 127 | "\"<code>#<i>aarrggbb</i></code>\"."); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | if (typeMask & android::ResTable_map::TYPE_FLOAT) { |
| 131 | processor->appendComment( |
| 132 | "<p>May be a floating point value, such as \"<code>1.2</code>\"."); |
| 133 | } |
| 134 | |
| 135 | if (typeMask & android::ResTable_map::TYPE_DIMENSION) { |
| 136 | processor->appendComment( |
| 137 | "<p>May be a dimension value, which is a floating point number appended with a\n" |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 138 | "unit such as \"<code>14.5sp</code>\".\n" |
| 139 | "Available units are: px (pixels), dp (density-independent pixels),\n" |
| 140 | "sp (scaled pixels based on preferred font size), in (inches), and\n" |
| 141 | "mm (millimeters)."); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if (typeMask & android::ResTable_map::TYPE_FRACTION) { |
| 145 | processor->appendComment( |
| 146 | "<p>May be a fractional value, which is a floating point number appended with\n" |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 147 | "either % or %p, such as \"<code>14.5%</code>\".\n" |
| 148 | "The % suffix always means a percentage of the base size;\n" |
| 149 | "the optional %p suffix provides a size relative to some parent container."); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if (typeMask & (android::ResTable_map::TYPE_FLAGS | android::ResTable_map::TYPE_ENUM)) { |
| 153 | if (typeMask & android::ResTable_map::TYPE_FLAGS) { |
| 154 | processor->appendComment( |
| 155 | "<p>Must be one or more (separated by '|') of the following " |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 156 | "constant values.</p>"); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 157 | } else { |
| 158 | processor->appendComment("<p>Must be one of the following constant values.</p>"); |
| 159 | } |
| 160 | |
| 161 | processor->appendComment("<table>\n<colgroup align=\"left\" />\n" |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 162 | "<colgroup align=\"left\" />\n" |
| 163 | "<colgroup align=\"left\" />\n" |
| 164 | "<tr><th>Constant</th><th>Value</th><th>Description</th></tr>\n"); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 165 | for (const Attribute::Symbol& symbol : attr->symbols) { |
| 166 | std::stringstream line; |
| 167 | line << "<tr><td>" << symbol.symbol.name.value().entry << "</td>" |
| 168 | << "<td>" << std::hex << symbol.value << std::dec << "</td>" |
| 169 | << "<td>" << util::trimWhitespace(symbol.symbol.getComment()) << "</td></tr>"; |
| 170 | processor->appendComment(line.str()); |
| 171 | } |
| 172 | processor->appendComment("</table>"); |
| 173 | } |
| 174 | } |
| 175 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 176 | bool JavaClassGenerator::skipSymbol(SymbolState state) { |
| 177 | switch (mOptions.types) { |
| 178 | case JavaClassGeneratorOptions::SymbolTypes::kAll: |
| 179 | return false; |
| 180 | case JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate: |
| 181 | return state == SymbolState::kUndefined; |
| 182 | case JavaClassGeneratorOptions::SymbolTypes::kPublic: |
| 183 | return state != SymbolState::kPublic; |
| 184 | } |
| 185 | return true; |
| 186 | } |
| 187 | |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 188 | struct StyleableAttr { |
| 189 | const Reference* attrRef; |
| 190 | std::string fieldName; |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 191 | std::unique_ptr<SymbolTable::Symbol> symbol; |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | static bool lessStyleableAttr(const StyleableAttr& lhs, const StyleableAttr& rhs) { |
| 195 | const ResourceId lhsId = lhs.attrRef->id ? lhs.attrRef->id.value() : ResourceId(0); |
| 196 | const ResourceId rhsId = rhs.attrRef->id ? rhs.attrRef->id.value() : ResourceId(0); |
| 197 | if (lhsId < rhsId) { |
| 198 | return true; |
| 199 | } else if (lhsId > rhsId) { |
| 200 | return false; |
| 201 | } else { |
| 202 | return lhs.attrRef->name.value() < rhs.attrRef->name.value(); |
| 203 | } |
| 204 | } |
| 205 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 206 | void JavaClassGenerator::addMembersToStyleableClass(const StringPiece& packageNameToGenerate, |
| 207 | const std::string& entryName, |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 208 | const Styleable* styleable, |
| 209 | ClassDefinition* outStyleableClassDef) { |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 210 | const std::string className = transform(entryName); |
| 211 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 212 | std::unique_ptr<ResourceArrayMember> styleableArrayDef = |
| 213 | util::make_unique<ResourceArrayMember>(className); |
| 214 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 215 | // This must be sorted by resource ID. |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 216 | std::vector<StyleableAttr> sortedAttributes; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 217 | sortedAttributes.reserve(styleable->entries.size()); |
| 218 | for (const auto& attr : styleable->entries) { |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 219 | // If we are not encoding final attributes, the styleable entry may have no ID |
| 220 | // if we are building a static library. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 221 | assert((!mOptions.useFinal || attr.id) && "no ID set for Styleable entry"); |
| 222 | assert(attr.name && "no name set for Styleable entry"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 223 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 224 | // We will need the unmangled, transformed name in the comments and the field, |
| 225 | // so create it once and cache it in this StyleableAttr data structure. |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 226 | StyleableAttr styleableAttr = {}; |
| 227 | styleableAttr.attrRef = &attr; |
| 228 | styleableAttr.fieldName = transformNestedAttr(attr.name.value(), className, |
| 229 | packageNameToGenerate); |
| 230 | |
| 231 | Reference mangledReference; |
| 232 | mangledReference.id = attr.id; |
| 233 | mangledReference.name = attr.name; |
| 234 | if (mangledReference.name.value().package.empty()) { |
| 235 | mangledReference.name.value().package = mContext->getCompilationPackage(); |
| 236 | } |
| 237 | |
| 238 | if (Maybe<ResourceName> mangledName = |
| 239 | mContext->getNameMangler()->mangleName(mangledReference.name.value())) { |
| 240 | mangledReference.name = mangledName; |
| 241 | } |
| 242 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 243 | // Look up the symbol so that we can write out in the comments what are possible |
| 244 | // legal values for this attribute. |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 245 | const SymbolTable::Symbol* symbol = mContext->getExternalSymbols()->findByReference( |
| 246 | mangledReference); |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 247 | if (symbol && symbol->attribute) { |
| 248 | // Copy the symbol data structure because the returned instance can be destroyed. |
| 249 | styleableAttr.symbol = util::make_unique<SymbolTable::Symbol>(*symbol); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 250 | } |
| 251 | sortedAttributes.push_back(std::move(styleableAttr)); |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 252 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 253 | |
| 254 | // Sort the attributes by ID. |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 255 | std::sort(sortedAttributes.begin(), sortedAttributes.end(), lessStyleableAttr); |
| 256 | |
| 257 | const size_t attrCount = sortedAttributes.size(); |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 258 | if (attrCount > 0) { |
| 259 | // Build the comment string for the Styleable. It includes details about the |
| 260 | // child attributes. |
| 261 | std::stringstream styleableComment; |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 262 | if (!styleable->getComment().empty()) { |
| 263 | styleableComment << styleable->getComment() << "\n"; |
| 264 | } else { |
| 265 | styleableComment << "Attributes that can be used with a " << className << ".\n"; |
| 266 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 267 | |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 268 | styleableComment << |
| 269 | "<p>Includes the following attributes:</p>\n" |
| 270 | "<table>\n" |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 271 | "<colgroup align=\"left\" />\n" |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 272 | "<colgroup align=\"left\" />\n" |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 273 | "<tr><th>Attribute</th><th>Description</th></tr>\n"; |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 274 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 275 | for (const StyleableAttr& entry : sortedAttributes) { |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 276 | if (!entry.symbol) { |
| 277 | continue; |
| 278 | } |
| 279 | |
| 280 | if (mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic && |
| 281 | !entry.symbol->isPublic) { |
| 282 | // Don't write entries for non-public attributes. |
| 283 | continue; |
| 284 | } |
| 285 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 286 | StringPiece attrCommentLine = entry.symbol->attribute->getComment(); |
| 287 | if (attrCommentLine.contains("@removed")) { |
Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 288 | // Removed attributes are public but hidden from the documentation, so don't emit |
| 289 | // them as part of the class documentation. |
| 290 | continue; |
| 291 | } |
| 292 | |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 293 | const ResourceName& attrName = entry.attrRef->name.value(); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 294 | styleableComment << "<tr><td>"; |
| 295 | styleableComment << "<code>{@link #" |
| 296 | << entry.fieldName << " " |
| 297 | << (!attrName.package.empty() |
| 298 | ? attrName.package : mContext->getCompilationPackage()) |
| 299 | << ":" << attrName.entry |
| 300 | << "}</code>"; |
| 301 | styleableComment << "</td>"; |
| 302 | |
| 303 | styleableComment << "<td>"; |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 304 | |
| 305 | // Only use the comment up until the first '.'. This is to stay compatible with |
| 306 | // the way old AAPT did it (presumably to keep it short and to avoid including |
| 307 | // annotations like @hide which would affect this Styleable). |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 308 | auto iter = std::find(attrCommentLine.begin(), attrCommentLine.end(), u'.'); |
| 309 | if (iter != attrCommentLine.end()) { |
| 310 | attrCommentLine = attrCommentLine.substr( |
| 311 | 0, (iter - attrCommentLine.begin()) + 1); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 312 | } |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 313 | styleableComment << attrCommentLine << "</td></tr>\n"; |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 314 | } |
| 315 | styleableComment << "</table>\n"; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 316 | |
| 317 | for (const StyleableAttr& entry : sortedAttributes) { |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 318 | if (!entry.symbol) { |
| 319 | continue; |
| 320 | } |
| 321 | |
| 322 | if (mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic && |
| 323 | !entry.symbol->isPublic) { |
| 324 | // Don't write entries for non-public attributes. |
| 325 | continue; |
| 326 | } |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 327 | styleableComment << "@see #" << entry.fieldName << "\n"; |
| 328 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 329 | |
| 330 | styleableArrayDef->getCommentBuilder()->appendComment(styleableComment.str()); |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 333 | // Add the ResourceIds to the array member. |
| 334 | for (const StyleableAttr& styleableAttr : sortedAttributes) { |
| 335 | styleableArrayDef->addElement( |
| 336 | styleableAttr.attrRef->id ? styleableAttr.attrRef->id.value() : ResourceId(0)); |
| 337 | } |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 338 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 339 | // Add the Styleable array to the Styleable class. |
| 340 | outStyleableClassDef->addMember(std::move(styleableArrayDef)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 341 | |
| 342 | // Now we emit the indices into the array. |
| 343 | for (size_t i = 0; i < attrCount; i++) { |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 344 | const StyleableAttr& styleableAttr = sortedAttributes[i]; |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 345 | |
| 346 | if (!styleableAttr.symbol) { |
| 347 | continue; |
| 348 | } |
| 349 | |
| 350 | if (mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic && |
| 351 | !styleableAttr.symbol->isPublic) { |
| 352 | // Don't write entries for non-public attributes. |
| 353 | continue; |
| 354 | } |
| 355 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 356 | StringPiece comment = styleableAttr.attrRef->getComment(); |
Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 357 | if (styleableAttr.symbol->attribute && comment.empty()) { |
| 358 | comment = styleableAttr.symbol->attribute->getComment(); |
| 359 | } |
| 360 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 361 | if (comment.contains("@removed")) { |
Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 362 | // Removed attributes are public but hidden from the documentation, so don't emit them |
| 363 | // as part of the class documentation. |
| 364 | continue; |
| 365 | } |
| 366 | |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 367 | const ResourceName& attrName = styleableAttr.attrRef->name.value(); |
| 368 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 369 | StringPiece packageName = attrName.package; |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 370 | if (packageName.empty()) { |
| 371 | packageName = mContext->getCompilationPackage(); |
| 372 | } |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 373 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 374 | std::unique_ptr<IntMember> indexMember = util::make_unique<IntMember>( |
Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 375 | sortedAttributes[i].fieldName, static_cast<uint32_t>(i)); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 376 | |
| 377 | AnnotationProcessor* attrProcessor = indexMember->getCommentBuilder(); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 378 | |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 379 | if (!comment.empty()) { |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 380 | attrProcessor->appendComment("<p>\n@attr description"); |
| 381 | attrProcessor->appendComment(comment); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 382 | } else { |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 383 | std::stringstream defaultComment; |
| 384 | defaultComment |
| 385 | << "<p>This symbol is the offset where the " |
| 386 | << "{@link " << packageName << ".R.attr#" << transform(attrName.entry) << "}\n" |
| 387 | << "attribute's value can be found in the " |
| 388 | << "{@link #" << className << "} array."; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 389 | attrProcessor->appendComment(defaultComment.str()); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 392 | attrProcessor->appendNewLine(); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 393 | |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 394 | addAttributeFormatDoc(attrProcessor, styleableAttr.symbol->attribute.get()); |
| 395 | attrProcessor->appendNewLine(); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 396 | |
| 397 | std::stringstream doclavaName; |
| 398 | doclavaName << "@attr name " << packageName << ":" << attrName.entry;; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 399 | attrProcessor->appendComment(doclavaName.str()); |
| 400 | |
| 401 | outStyleableClassDef->addMember(std::move(indexMember)); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 405 | bool JavaClassGenerator::addMembersToTypeClass(const StringPiece& packageNameToGenerate, |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 406 | const ResourceTablePackage* package, |
| 407 | const ResourceTableType* type, |
| 408 | ClassDefinition* outTypeClassDef) { |
| 409 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 410 | for (const auto& entry : type->entries) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 411 | if (skipSymbol(entry->symbolStatus.state)) { |
| 412 | continue; |
| 413 | } |
| 414 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 415 | ResourceId id; |
| 416 | if (package->id && type->id && entry->id) { |
| 417 | id = ResourceId(package->id.value(), type->id.value(), entry->id.value()); |
| 418 | } |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 419 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 420 | std::string unmangledPackage; |
| 421 | std::string unmangledName = entry->name; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 422 | if (NameMangler::unmangle(&unmangledName, &unmangledPackage)) { |
| 423 | // The entry name was mangled, and we successfully unmangled it. |
| 424 | // Check that we want to emit this symbol. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 425 | if (package->name != unmangledPackage) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 426 | // Skip the entry if it doesn't belong to the package we're writing. |
| 427 | continue; |
| 428 | } |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 429 | } else if (packageNameToGenerate != package->name) { |
| 430 | // We are processing a mangled package name, |
| 431 | // but this is a non-mangled resource. |
| 432 | continue; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | if (!isValidSymbol(unmangledName)) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 436 | ResourceNameRef resourceName(packageNameToGenerate, type->type, unmangledName); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 437 | std::stringstream err; |
| 438 | err << "invalid symbol name '" << resourceName << "'"; |
| 439 | mError = err.str(); |
| 440 | return false; |
| 441 | } |
| 442 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 443 | if (type->type == ResourceType::kStyleable) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 444 | assert(!entry->values.empty()); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 445 | |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 446 | const Styleable* styleable = static_cast<const Styleable*>( |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 447 | entry->values.front()->value.get()); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 448 | |
| 449 | // Comments are handled within this method. |
| 450 | addMembersToStyleableClass(packageNameToGenerate, unmangledName, styleable, |
| 451 | outTypeClassDef); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 452 | } else { |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 453 | std::unique_ptr<ResourceMember> resourceMember = |
| 454 | util::make_unique<ResourceMember>(transform(unmangledName), id); |
| 455 | |
| 456 | // Build the comments and annotations for this entry. |
| 457 | AnnotationProcessor* processor = resourceMember->getCommentBuilder(); |
| 458 | |
| 459 | // Add the comments from any <public> tags. |
| 460 | if (entry->symbolStatus.state != SymbolState::kUndefined) { |
| 461 | processor->appendComment(entry->symbolStatus.comment); |
| 462 | } |
| 463 | |
| 464 | // Add the comments from all configurations of this entry. |
| 465 | for (const auto& configValue : entry->values) { |
| 466 | processor->appendComment(configValue->value->getComment()); |
| 467 | } |
| 468 | |
| 469 | // If this is an Attribute, append the format Javadoc. |
| 470 | if (!entry->values.empty()) { |
| 471 | if (Attribute* attr = valueCast<Attribute>(entry->values.front()->value.get())) { |
| 472 | // We list out the available values for the given attribute. |
| 473 | addAttributeFormatDoc(processor, attr); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | outTypeClassDef->addMember(std::move(resourceMember)); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | return true; |
| 481 | } |
| 482 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 483 | bool JavaClassGenerator::generate(const StringPiece& packageNameToGenerate, std::ostream* out) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 484 | return generate(packageNameToGenerate, packageNameToGenerate, out); |
| 485 | } |
| 486 | |
Adam Lesinski | 3524a23 | 2016-04-01 19:19:24 -0700 | [diff] [blame] | 487 | static void appendJavaDocAnnotations(const std::vector<std::string>& annotations, |
| 488 | AnnotationProcessor* processor) { |
| 489 | for (const std::string& annotation : annotations) { |
| 490 | std::string properAnnotation = "@"; |
| 491 | properAnnotation += annotation; |
| 492 | processor->appendComment(properAnnotation); |
| 493 | } |
| 494 | } |
| 495 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 496 | bool JavaClassGenerator::generate(const StringPiece& packageNameToGenerate, |
| 497 | const StringPiece& outPackageName, std::ostream* out) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 498 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 499 | ClassDefinition rClass("R", ClassQualifier::None, true); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 500 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 501 | for (const auto& package : mTable->packages) { |
| 502 | for (const auto& type : package->types) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 503 | if (type->type == ResourceType::kAttrPrivate) { |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 504 | continue; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 505 | } |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 506 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 507 | const bool forceCreationIfEmpty = |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 508 | (mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 509 | |
| 510 | std::unique_ptr<ClassDefinition> classDef = util::make_unique<ClassDefinition>( |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 511 | toString(type->type), ClassQualifier::Static, forceCreationIfEmpty); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 512 | |
| 513 | bool result = addMembersToTypeClass(packageNameToGenerate, package.get(), type.get(), |
| 514 | classDef.get()); |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 515 | if (!result) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 516 | return false; |
| 517 | } |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 518 | |
| 519 | if (type->type == ResourceType::kAttr) { |
| 520 | // Also include private attributes in this same class. |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 521 | ResourceTableType* privType = package->findType(ResourceType::kAttrPrivate); |
| 522 | if (privType) { |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 523 | result = addMembersToTypeClass(packageNameToGenerate, package.get(), privType, |
| 524 | classDef.get()); |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 525 | if (!result) { |
| 526 | return false; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 531 | if (type->type == ResourceType::kStyleable && |
| 532 | mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic) { |
| 533 | // When generating a public R class, we don't want Styleable to be part of the API. |
| 534 | // It is only emitted for documentation purposes. |
Adam Lesinski | 3524a23 | 2016-04-01 19:19:24 -0700 | [diff] [blame] | 535 | classDef->getCommentBuilder()->appendComment("@doconly"); |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 536 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 537 | |
Adam Lesinski | 3524a23 | 2016-04-01 19:19:24 -0700 | [diff] [blame] | 538 | appendJavaDocAnnotations(mOptions.javadocAnnotations, classDef->getCommentBuilder()); |
| 539 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 540 | rClass.addMember(std::move(classDef)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 541 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 542 | } |
| 543 | |
Adam Lesinski | 3524a23 | 2016-04-01 19:19:24 -0700 | [diff] [blame] | 544 | appendJavaDocAnnotations(mOptions.javadocAnnotations, rClass.getCommentBuilder()); |
| 545 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 546 | if (!ClassDefinition::writeJavaFile(&rClass, outPackageName, mOptions.useFinal, out)) { |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 547 | return false; |
| 548 | } |
| 549 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 550 | out->flush(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 551 | return true; |
| 552 | } |
| 553 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 554 | } // namespace aapt |