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 | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 17 | #include "java/JavaClassGenerator.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 18 | |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 20 | #include <ostream> |
| 21 | #include <set> |
| 22 | #include <sstream> |
| 23 | #include <tuple> |
| 24 | |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 25 | #include "android-base/errors.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | #include "android-base/logging.h" |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 27 | #include "android-base/stringprintf.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 28 | #include "androidfw/StringPiece.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 29 | |
| 30 | #include "NameMangler.h" |
| 31 | #include "Resource.h" |
| 32 | #include "ResourceTable.h" |
| 33 | #include "ResourceValues.h" |
| 34 | #include "ValueVisitor.h" |
| 35 | #include "java/AnnotationProcessor.h" |
| 36 | #include "java/ClassDefinition.h" |
| 37 | #include "process/SymbolTable.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 38 | |
| 39 | using android::StringPiece; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 40 | using android::base::StringPrintf; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 42 | namespace aapt { |
| 43 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 44 | static const std::set<StringPiece> sJavaIdentifiers = { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 45 | "abstract", "assert", "boolean", "break", "byte", |
| 46 | "case", "catch", "char", "class", "const", |
| 47 | "continue", "default", "do", "double", "else", |
| 48 | "enum", "extends", "final", "finally", "float", |
| 49 | "for", "goto", "if", "implements", "import", |
| 50 | "instanceof", "int", "interface", "long", "native", |
| 51 | "new", "package", "private", "protected", "public", |
| 52 | "return", "short", "static", "strictfp", "super", |
| 53 | "switch", "synchronized", "this", "throw", "throws", |
| 54 | "transient", "try", "void", "volatile", "while", |
| 55 | "true", "false", "null"}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 56 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 57 | static bool IsValidSymbol(const StringPiece& symbol) { |
| 58 | return sJavaIdentifiers.find(symbol) == sJavaIdentifiers.end(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 61 | // Java symbols can not contain . or -, but those are valid in a resource name. |
| 62 | // Replace those with '_'. |
| 63 | static std::string TransformToFieldName(const StringPiece& symbol) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 64 | std::string output = symbol.to_string(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | for (char& c : output) { |
| 66 | if (c == '.' || c == '-') { |
| 67 | c = '_'; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 68 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 69 | } |
| 70 | return output; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 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 | static std::string TransformNestedAttr(const ResourceNameRef& attr_name, |
| 83 | const std::string& styleable_class_name, |
| 84 | const StringPiece& package_name_to_generate) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 85 | std::string output = styleable_class_name; |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 86 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | // We may reference IDs from other packages, so prefix the entry name with |
| 88 | // the package. |
| 89 | if (!attr_name.package.empty() && |
| 90 | package_name_to_generate != attr_name.package) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 91 | output += "_" + TransformToFieldName(attr_name.package); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 92 | } |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 93 | output += "_" + TransformToFieldName(attr_name.entry); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 94 | return output; |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 97 | static void AddAttributeFormatDoc(AnnotationProcessor* processor, Attribute* attr) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | const uint32_t type_mask = attr->type_mask; |
| 99 | if (type_mask & android::ResTable_map::TYPE_REFERENCE) { |
| 100 | processor->AppendComment( |
| 101 | "<p>May be a reference to another resource, in the form\n" |
| 102 | "\"<code>@[+][<i>package</i>:]<i>type</i>/<i>name</i></code>\" or a " |
| 103 | "theme\n" |
| 104 | "attribute in the form\n" |
| 105 | "\"<code>?[<i>package</i>:]<i>type</i>/<i>name</i></code>\"."); |
| 106 | } |
| 107 | |
| 108 | if (type_mask & android::ResTable_map::TYPE_STRING) { |
| 109 | processor->AppendComment( |
| 110 | "<p>May be a string value, using '\\\\;' to escape characters such as\n" |
| 111 | "'\\\\n' or '\\\\uxxxx' for a unicode character;"); |
| 112 | } |
| 113 | |
| 114 | if (type_mask & android::ResTable_map::TYPE_INTEGER) { |
| 115 | processor->AppendComment( |
| 116 | "<p>May be an integer value, such as \"<code>100</code>\"."); |
| 117 | } |
| 118 | |
| 119 | if (type_mask & android::ResTable_map::TYPE_BOOLEAN) { |
| 120 | processor->AppendComment( |
| 121 | "<p>May be a boolean value, such as \"<code>true</code>\" or\n" |
| 122 | "\"<code>false</code>\"."); |
| 123 | } |
| 124 | |
| 125 | if (type_mask & android::ResTable_map::TYPE_COLOR) { |
| 126 | processor->AppendComment( |
| 127 | "<p>May be a color value, in the form of " |
| 128 | "\"<code>#<i>rgb</i></code>\",\n" |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 129 | "\"<code>#<i>argb</i></code>\", \"<code>#<i>rrggbb</i></code>\", or \n" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 130 | "\"<code>#<i>aarrggbb</i></code>\"."); |
| 131 | } |
| 132 | |
| 133 | if (type_mask & android::ResTable_map::TYPE_FLOAT) { |
| 134 | processor->AppendComment( |
| 135 | "<p>May be a floating point value, such as \"<code>1.2</code>\"."); |
| 136 | } |
| 137 | |
| 138 | if (type_mask & android::ResTable_map::TYPE_DIMENSION) { |
| 139 | processor->AppendComment( |
| 140 | "<p>May be a dimension value, which is a floating point number " |
| 141 | "appended with a\n" |
| 142 | "unit such as \"<code>14.5sp</code>\".\n" |
| 143 | "Available units are: px (pixels), dp (density-independent pixels),\n" |
| 144 | "sp (scaled pixels based on preferred font size), in (inches), and\n" |
| 145 | "mm (millimeters)."); |
| 146 | } |
| 147 | |
| 148 | if (type_mask & android::ResTable_map::TYPE_FRACTION) { |
| 149 | processor->AppendComment( |
| 150 | "<p>May be a fractional value, which is a floating point number " |
| 151 | "appended with\n" |
| 152 | "either % or %p, such as \"<code>14.5%</code>\".\n" |
| 153 | "The % suffix always means a percentage of the base size;\n" |
| 154 | "the optional %p suffix provides a size relative to some parent " |
| 155 | "container."); |
| 156 | } |
| 157 | |
| 158 | if (type_mask & |
| 159 | (android::ResTable_map::TYPE_FLAGS | android::ResTable_map::TYPE_ENUM)) { |
| 160 | if (type_mask & android::ResTable_map::TYPE_FLAGS) { |
| 161 | processor->AppendComment( |
| 162 | "<p>Must be one or more (separated by '|') of the following " |
| 163 | "constant values.</p>"); |
| 164 | } else { |
| 165 | processor->AppendComment( |
| 166 | "<p>Must be one of the following constant values.</p>"); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 169 | processor->AppendComment( |
| 170 | "<table>\n<colgroup align=\"left\" />\n" |
| 171 | "<colgroup align=\"left\" />\n" |
| 172 | "<colgroup align=\"left\" />\n" |
| 173 | "<tr><th>Constant</th><th>Value</th><th>Description</th></tr>\n"); |
| 174 | for (const Attribute::Symbol& symbol : attr->symbols) { |
| 175 | std::stringstream line; |
| 176 | line << "<tr><td>" << symbol.symbol.name.value().entry << "</td>" |
| 177 | << "<td>" << std::hex << symbol.value << std::dec << "</td>" |
| 178 | << "<td>" << util::TrimWhitespace(symbol.symbol.GetComment()) |
| 179 | << "</td></tr>"; |
| 180 | processor->AppendComment(line.str()); |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 181 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 182 | processor->AppendComment("</table>"); |
| 183 | } |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 186 | JavaClassGenerator::JavaClassGenerator(IAaptContext* context, |
| 187 | ResourceTable* table, |
| 188 | const JavaClassGeneratorOptions& options) |
| 189 | : context_(context), table_(table), options_(options) {} |
| 190 | |
| 191 | bool JavaClassGenerator::SkipSymbol(SymbolState state) { |
| 192 | switch (options_.types) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 193 | case JavaClassGeneratorOptions::SymbolTypes::kAll: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 194 | return false; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 195 | case JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 196 | return state == SymbolState::kUndefined; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 197 | case JavaClassGeneratorOptions::SymbolTypes::kPublic: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 198 | return state != SymbolState::kPublic; |
| 199 | } |
| 200 | return true; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 203 | // Whether or not to skip writing this symbol. |
| 204 | bool JavaClassGenerator::SkipSymbol(const Maybe<SymbolTable::Symbol>& symbol) { |
| 205 | return !symbol || (options_.types == JavaClassGeneratorOptions::SymbolTypes::kPublic && |
| 206 | !symbol.value().is_public); |
| 207 | } |
| 208 | |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 209 | struct StyleableAttr { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 210 | const Reference* attr_ref = nullptr; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 211 | std::string field_name; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 212 | Maybe<SymbolTable::Symbol> symbol; |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 213 | }; |
| 214 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 215 | static bool operator<(const StyleableAttr& lhs, const StyleableAttr& rhs) { |
| 216 | const ResourceId lhs_id = lhs.attr_ref->id.value_or_default(ResourceId(0)); |
| 217 | const ResourceId rhs_id = rhs.attr_ref->id.value_or_default(ResourceId(0)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 218 | if (lhs_id < rhs_id) { |
| 219 | return true; |
| 220 | } else if (lhs_id > rhs_id) { |
| 221 | return false; |
| 222 | } else { |
| 223 | return lhs.attr_ref->name.value() < rhs.attr_ref->name.value(); |
| 224 | } |
| 225 | } |
| 226 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 227 | void JavaClassGenerator::ProcessStyleable(const ResourceNameRef& name, const ResourceId& id, |
| 228 | const Styleable& styleable, |
| 229 | const StringPiece& package_name_to_generate, |
| 230 | ClassDefinition* out_class_def, |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 231 | MethodDefinition* out_rewrite_method, |
| 232 | std::ostream* out_r_txt) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 233 | const std::string array_field_name = TransformToFieldName(name.entry); |
| 234 | std::unique_ptr<ResourceArrayMember> array_def = |
| 235 | util::make_unique<ResourceArrayMember>(array_field_name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 236 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 237 | // The array must be sorted by resource ID. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 238 | std::vector<StyleableAttr> sorted_attributes; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 239 | sorted_attributes.reserve(styleable.entries.size()); |
| 240 | for (const auto& attr : styleable.entries) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 241 | // If we are not encoding final attributes, the styleable entry may have no |
| 242 | // ID if we are building a static library. |
| 243 | CHECK(!options_.use_final || attr.id) << "no ID set for Styleable entry"; |
| 244 | CHECK(bool(attr.name)) << "no name set for Styleable entry"; |
| 245 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 246 | // We will need the unmangled, transformed name in the comments and the field, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 247 | // so create it once and cache it in this StyleableAttr data structure. |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 248 | StyleableAttr styleable_attr; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 249 | styleable_attr.attr_ref = &attr; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 250 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 251 | // The field name for this attribute is prefixed by the name of this styleable and |
| 252 | // the package it comes from. |
| 253 | styleable_attr.field_name = |
| 254 | TransformNestedAttr(attr.name.value(), array_field_name, package_name_to_generate); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 255 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 256 | // Look up the symbol so that we can write out in the comments what are possible legal values |
| 257 | // for this attribute. |
| 258 | const SymbolTable::Symbol* symbol = context_->GetExternalSymbols()->FindByReference(attr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 259 | if (symbol && symbol->attribute) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 260 | // Copy the symbol data structure because the returned instance can be destroyed. |
| 261 | styleable_attr.symbol = *symbol; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 262 | } |
| 263 | sorted_attributes.push_back(std::move(styleable_attr)); |
| 264 | } |
| 265 | |
| 266 | // Sort the attributes by ID. |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 267 | std::sort(sorted_attributes.begin(), sorted_attributes.end()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 268 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 269 | // Build the JavaDoc comment for the Styleable array. This has references to child attributes |
| 270 | // and what possible values can be used for them. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 271 | const size_t attr_count = sorted_attributes.size(); |
| 272 | if (attr_count > 0) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 273 | std::stringstream styleable_comment; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 274 | if (!styleable.GetComment().empty()) { |
| 275 | styleable_comment << styleable.GetComment() << "\n"; |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 276 | } else { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 277 | // Apply a default intro comment if the styleable has no comments of its own. |
| 278 | styleable_comment << "Attributes that can be used with a " << array_field_name << ".\n"; |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 279 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 280 | |
| 281 | styleable_comment << "<p>Includes the following attributes:</p>\n" |
| 282 | "<table>\n" |
| 283 | "<colgroup align=\"left\" />\n" |
| 284 | "<colgroup align=\"left\" />\n" |
| 285 | "<tr><th>Attribute</th><th>Description</th></tr>\n"; |
| 286 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 287 | // Build the table of attributes with their links and names. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 288 | for (const StyleableAttr& entry : sorted_attributes) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 289 | if (SkipSymbol(entry.symbol)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 290 | continue; |
| 291 | } |
| 292 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 293 | StringPiece attr_comment_line = entry.symbol.value().attribute->GetComment(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 294 | if (attr_comment_line.contains("@removed")) { |
| 295 | // Removed attributes are public but hidden from the documentation, so |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 296 | // don't emit them as part of the class documentation. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 297 | continue; |
| 298 | } |
| 299 | |
| 300 | const ResourceName& attr_name = entry.attr_ref->name.value(); |
| 301 | styleable_comment << "<tr><td>"; |
| 302 | styleable_comment << "<code>{@link #" << entry.field_name << " " |
| 303 | << (!attr_name.package.empty() |
| 304 | ? attr_name.package |
| 305 | : context_->GetCompilationPackage()) |
| 306 | << ":" << attr_name.entry << "}</code>"; |
| 307 | styleable_comment << "</td>"; |
| 308 | |
| 309 | styleable_comment << "<td>"; |
| 310 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 311 | // Only use the comment up until the first '.'. This is to stay compatible with |
| 312 | // the way old AAPT did it (presumably to keep it short and to avoid including |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 313 | // annotations like @hide which would affect this Styleable). |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 314 | auto iter = std::find(attr_comment_line.begin(), attr_comment_line.end(), '.'); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 315 | if (iter != attr_comment_line.end()) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 316 | attr_comment_line = attr_comment_line.substr(0, (iter - attr_comment_line.begin()) + 1); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 317 | } |
| 318 | styleable_comment << attr_comment_line << "</td></tr>\n"; |
| 319 | } |
| 320 | styleable_comment << "</table>\n"; |
| 321 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 322 | // Generate the @see lines for each attribute. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 323 | for (const StyleableAttr& entry : sorted_attributes) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 324 | if (SkipSymbol(entry.symbol)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 325 | continue; |
| 326 | } |
| 327 | styleable_comment << "@see #" << entry.field_name << "\n"; |
| 328 | } |
| 329 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 330 | array_def->GetCommentBuilder()->AppendComment(styleable_comment.str()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 333 | if (out_r_txt != nullptr) { |
| 334 | *out_r_txt << "int[] styleable " << array_field_name << " {"; |
| 335 | } |
| 336 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 337 | // Add the ResourceIds to the array member. |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 338 | for (size_t i = 0; i < attr_count; i++) { |
| 339 | const ResourceId id = sorted_attributes[i].attr_ref->id.value_or_default(ResourceId(0)); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 340 | array_def->AddElement(id); |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 341 | |
| 342 | if (out_r_txt != nullptr) { |
| 343 | if (i != 0) { |
| 344 | *out_r_txt << ","; |
| 345 | } |
| 346 | *out_r_txt << " " << id; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | if (out_r_txt != nullptr) { |
| 351 | *out_r_txt << " }\n"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | // Add the Styleable array to the Styleable class. |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 355 | out_class_def->AddMember(std::move(array_def)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 356 | |
| 357 | // Now we emit the indices into the array. |
| 358 | for (size_t i = 0; i < attr_count; i++) { |
| 359 | const StyleableAttr& styleable_attr = sorted_attributes[i]; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 360 | if (SkipSymbol(styleable_attr.symbol)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 361 | continue; |
| 362 | } |
| 363 | |
| 364 | StringPiece comment = styleable_attr.attr_ref->GetComment(); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 365 | if (styleable_attr.symbol.value().attribute && comment.empty()) { |
| 366 | comment = styleable_attr.symbol.value().attribute->GetComment(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | if (comment.contains("@removed")) { |
| 370 | // Removed attributes are public but hidden from the documentation, so |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 371 | // don't emit them as part of the class documentation. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 372 | continue; |
| 373 | } |
| 374 | |
| 375 | const ResourceName& attr_name = styleable_attr.attr_ref->name.value(); |
| 376 | |
| 377 | StringPiece package_name = attr_name.package; |
| 378 | if (package_name.empty()) { |
| 379 | package_name = context_->GetCompilationPackage(); |
| 380 | } |
| 381 | |
| 382 | std::unique_ptr<IntMember> index_member = util::make_unique<IntMember>( |
| 383 | sorted_attributes[i].field_name, static_cast<uint32_t>(i)); |
| 384 | |
| 385 | AnnotationProcessor* attr_processor = index_member->GetCommentBuilder(); |
| 386 | |
| 387 | if (!comment.empty()) { |
| 388 | attr_processor->AppendComment("<p>\n@attr description"); |
| 389 | attr_processor->AppendComment(comment); |
| 390 | } else { |
| 391 | std::stringstream default_comment; |
| 392 | default_comment << "<p>This symbol is the offset where the " |
| 393 | << "{@link " << package_name << ".R.attr#" |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 394 | << TransformToFieldName(attr_name.entry) << "}\n" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 395 | << "attribute's value can be found in the " |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 396 | << "{@link #" << array_field_name << "} array."; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 397 | attr_processor->AppendComment(default_comment.str()); |
| 398 | } |
| 399 | |
| 400 | attr_processor->AppendNewLine(); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 401 | AddAttributeFormatDoc(attr_processor, styleable_attr.symbol.value().attribute.get()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 402 | attr_processor->AppendNewLine(); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 403 | attr_processor->AppendComment( |
| 404 | StringPrintf("@attr name %s:%s", package_name.data(), attr_name.entry.data())); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 405 | |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 406 | if (out_r_txt != nullptr) { |
| 407 | *out_r_txt << StringPrintf("int styleable %s %d\n", sorted_attributes[i].field_name.data(), |
| 408 | (int)i); |
| 409 | } |
| 410 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 411 | out_class_def->AddMember(std::move(index_member)); |
| 412 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 413 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 414 | // If there is a rewrite method to generate, add the statements that rewrite package IDs |
| 415 | // for this styleable. |
| 416 | if (out_rewrite_method != nullptr) { |
| 417 | out_rewrite_method->AppendStatement( |
| 418 | StringPrintf("for (int i = 0; i < styleable.%s.length; i++) {", array_field_name.data())); |
| 419 | out_rewrite_method->AppendStatement( |
| 420 | StringPrintf(" if ((styleable.%s[i] & 0xff000000) == 0) {", array_field_name.data())); |
| 421 | out_rewrite_method->AppendStatement( |
| 422 | StringPrintf(" styleable.%s[i] = (styleable.%s[i] & 0x00ffffff) | (p << 24);", |
| 423 | array_field_name.data(), array_field_name.data())); |
| 424 | out_rewrite_method->AppendStatement(" }"); |
| 425 | out_rewrite_method->AppendStatement("}"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 426 | } |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 429 | void JavaClassGenerator::ProcessResource(const ResourceNameRef& name, const ResourceId& id, |
| 430 | const ResourceEntry& entry, ClassDefinition* out_class_def, |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 431 | MethodDefinition* out_rewrite_method, |
| 432 | std::ostream* out_r_txt) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 433 | const std::string field_name = TransformToFieldName(name.entry); |
| 434 | std::unique_ptr<ResourceMember> resource_member = |
| 435 | util::make_unique<ResourceMember>(field_name, id); |
| 436 | |
| 437 | // Build the comments and annotations for this entry. |
| 438 | AnnotationProcessor* processor = resource_member->GetCommentBuilder(); |
| 439 | |
| 440 | // Add the comments from any <public> tags. |
| 441 | if (entry.symbol_status.state != SymbolState::kUndefined) { |
| 442 | processor->AppendComment(entry.symbol_status.comment); |
| 443 | } |
| 444 | |
| 445 | // Add the comments from all configurations of this entry. |
| 446 | for (const auto& config_value : entry.values) { |
| 447 | processor->AppendComment(config_value->value->GetComment()); |
| 448 | } |
| 449 | |
| 450 | // If this is an Attribute, append the format Javadoc. |
| 451 | if (!entry.values.empty()) { |
| 452 | if (Attribute* attr = ValueCast<Attribute>(entry.values.front()->value.get())) { |
| 453 | // We list out the available values for the given attribute. |
| 454 | AddAttributeFormatDoc(processor, attr); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | out_class_def->AddMember(std::move(resource_member)); |
| 459 | |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 460 | if (out_r_txt != nullptr) { |
| 461 | *out_r_txt << "int " << name.type << " " << field_name << " " << id << "\n"; |
| 462 | } |
| 463 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 464 | if (out_rewrite_method != nullptr) { |
| 465 | const StringPiece& type_str = ToString(name.type); |
| 466 | out_rewrite_method->AppendStatement(StringPrintf("%s.%s = (%s.%s & 0x00ffffff) | (p << 24);", |
| 467 | type_str.data(), field_name.data(), |
| 468 | type_str.data(), field_name.data())); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | Maybe<std::string> JavaClassGenerator::UnmangleResource(const StringPiece& package_name, |
| 473 | const StringPiece& package_name_to_generate, |
| 474 | const ResourceEntry& entry) { |
| 475 | if (SkipSymbol(entry.symbol_status.state)) { |
| 476 | return {}; |
| 477 | } |
| 478 | |
| 479 | std::string unmangled_package; |
| 480 | std::string unmangled_name = entry.name; |
| 481 | if (NameMangler::Unmangle(&unmangled_name, &unmangled_package)) { |
| 482 | // The entry name was mangled, and we successfully unmangled it. |
| 483 | // Check that we want to emit this symbol. |
| 484 | if (package_name != unmangled_package) { |
| 485 | // Skip the entry if it doesn't belong to the package we're writing. |
| 486 | return {}; |
| 487 | } |
| 488 | } else if (package_name_to_generate != package_name) { |
| 489 | // We are processing a mangled package name, |
| 490 | // but this is a non-mangled resource. |
| 491 | return {}; |
| 492 | } |
| 493 | return {std::move(unmangled_name)}; |
| 494 | } |
| 495 | |
| 496 | bool JavaClassGenerator::ProcessType(const StringPiece& package_name_to_generate, |
| 497 | const ResourceTablePackage& package, |
| 498 | const ResourceTableType& type, |
| 499 | ClassDefinition* out_type_class_def, |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 500 | MethodDefinition* out_rewrite_method_def, |
| 501 | std::ostream* out_r_txt) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 502 | for (const auto& entry : type.entries) { |
| 503 | const Maybe<std::string> unmangled_name = |
| 504 | UnmangleResource(package.name, package_name_to_generate, *entry); |
| 505 | if (!unmangled_name) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 506 | continue; |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 507 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 508 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 509 | // Create an ID if there is one (static libraries don't need one). |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 510 | ResourceId id; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 511 | if (package.id && type.id && entry->id) { |
| 512 | id = ResourceId(package.id.value(), type.id.value(), entry->id.value()); |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 513 | } |
| 514 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 515 | // We need to make sure we hide the fact that we are generating kAttrPrivate attributes. |
| 516 | const ResourceNameRef resource_name( |
| 517 | package_name_to_generate, |
| 518 | type.type == ResourceType::kAttrPrivate ? ResourceType::kAttr : type.type, |
| 519 | unmangled_name.value()); |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 520 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 521 | // Check to see if the unmangled name is a valid Java name (not a keyword). |
| 522 | if (!IsValidSymbol(unmangled_name.value())) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 523 | std::stringstream err; |
| 524 | err << "invalid symbol name '" << resource_name << "'"; |
| 525 | error_ = err.str(); |
| 526 | return false; |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 527 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 528 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 529 | if (resource_name.type == ResourceType::kStyleable) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 530 | CHECK(!entry->values.empty()); |
| 531 | |
| 532 | const Styleable* styleable = |
| 533 | static_cast<const Styleable*>(entry->values.front()->value.get()); |
| 534 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 535 | ProcessStyleable(resource_name, id, *styleable, package_name_to_generate, out_type_class_def, |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 536 | out_rewrite_method_def, out_r_txt); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 537 | } else { |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 538 | ProcessResource(resource_name, id, *entry, out_type_class_def, out_rewrite_method_def, |
| 539 | out_r_txt); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | return true; |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 545 | bool JavaClassGenerator::Generate(const StringPiece& package_name_to_generate, std::ostream* out, |
| 546 | std::ostream* out_r_txt) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 547 | return Generate(package_name_to_generate, package_name_to_generate, out); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 550 | static void AppendJavaDocAnnotations(const std::vector<std::string>& annotations, |
| 551 | AnnotationProcessor* processor) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 552 | for (const std::string& annotation : annotations) { |
| 553 | std::string proper_annotation = "@"; |
| 554 | proper_annotation += annotation; |
| 555 | processor->AppendComment(proper_annotation); |
| 556 | } |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 559 | bool JavaClassGenerator::Generate(const StringPiece& package_name_to_generate, |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 560 | const StringPiece& out_package_name, std::ostream* out, |
| 561 | std::ostream* out_r_txt) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 562 | ClassDefinition r_class("R", ClassQualifier::kNone, true); |
| 563 | std::unique_ptr<MethodDefinition> rewrite_method; |
| 564 | |
| 565 | // Generate an onResourcesLoaded() callback if requested. |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 566 | if (options_.rewrite_callback_options) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 567 | rewrite_method = |
| 568 | util::make_unique<MethodDefinition>("public static void onResourcesLoaded(int p)"); |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 569 | for (const std::string& package_to_callback : |
| 570 | options_.rewrite_callback_options.value().packages_to_callback) { |
| 571 | rewrite_method->AppendStatement( |
| 572 | StringPrintf("%s.R.onResourcesLoaded(p);", package_to_callback.data())); |
| 573 | } |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 574 | } |
Adam Lesinski | 3524a23 | 2016-04-01 19:19:24 -0700 | [diff] [blame] | 575 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 576 | for (const auto& package : table_->packages) { |
| 577 | for (const auto& type : package->types) { |
| 578 | if (type->type == ResourceType::kAttrPrivate) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 579 | // We generate these as part of the kAttr type, so skip them here. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 580 | continue; |
| 581 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 582 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 583 | // Stay consistent with AAPT and generate an empty type class if the R class |
| 584 | // is public. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 585 | const bool force_creation_if_empty = |
| 586 | (options_.types == JavaClassGeneratorOptions::SymbolTypes::kPublic); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 587 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 588 | std::unique_ptr<ClassDefinition> class_def = util::make_unique<ClassDefinition>( |
| 589 | ToString(type->type), ClassQualifier::kStatic, force_creation_if_empty); |
| 590 | if (!ProcessType(package_name_to_generate, *package, *type, class_def.get(), |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 591 | rewrite_method.get(), out_r_txt)) { |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 592 | return false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 593 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 594 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 595 | if (type->type == ResourceType::kAttr) { |
| 596 | // Also include private attributes in this same class. |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 597 | const ResourceTableType* priv_type = package->FindType(ResourceType::kAttrPrivate); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 598 | if (priv_type) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 599 | if (!ProcessType(package_name_to_generate, *package, *priv_type, class_def.get(), |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 600 | rewrite_method.get(), out_r_txt)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 601 | return false; |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | if (type->type == ResourceType::kStyleable && |
| 607 | options_.types == JavaClassGeneratorOptions::SymbolTypes::kPublic) { |
| 608 | // When generating a public R class, we don't want Styleable to be part |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 609 | // of the API. It is only emitted for documentation purposes. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 610 | class_def->GetCommentBuilder()->AppendComment("@doconly"); |
| 611 | } |
| 612 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 613 | AppendJavaDocAnnotations(options_.javadoc_annotations, class_def->GetCommentBuilder()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 614 | |
| 615 | r_class.AddMember(std::move(class_def)); |
| 616 | } |
| 617 | } |
| 618 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 619 | if (rewrite_method != nullptr) { |
| 620 | r_class.AddMember(std::move(rewrite_method)); |
| 621 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 622 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 623 | AppendJavaDocAnnotations(options_.javadoc_annotations, r_class.GetCommentBuilder()); |
| 624 | |
| 625 | if (!ClassDefinition::WriteJavaFile(&r_class, out_package_name, options_.use_final, out)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 626 | return false; |
| 627 | } |
| 628 | |
| 629 | out->flush(); |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 630 | |
| 631 | if (out_r_txt != nullptr) { |
| 632 | out_r_txt->flush(); |
| 633 | |
| 634 | if (!*out_r_txt) { |
| 635 | error_ = android::base::SystemErrorCodeToString(errno); |
| 636 | return false; |
| 637 | } |
| 638 | } |
| 639 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 640 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 641 | } |
| 642 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 643 | } // namespace aapt |