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 | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame^] | 21 | #include "java/AnnotationProcessor.h" |
| 22 | #include "java/JavaClassGenerator.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 23 | #include "util/StringPiece.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 24 | |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 25 | #include <algorithm> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 26 | #include <ostream> |
| 27 | #include <set> |
| 28 | #include <sstream> |
| 29 | #include <tuple> |
| 30 | |
| 31 | namespace aapt { |
| 32 | |
| 33 | // The number of attributes to emit per line in a Styleable array. |
| 34 | constexpr size_t kAttribsPerLine = 4; |
| 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 | */ |
| 71 | static std::u16string transform(const StringPiece16& symbol) { |
| 72 | std::u16string output = symbol.toString(); |
| 73 | for (char16_t& c : output) { |
| 74 | if (c == u'.' || c == u'-') { |
| 75 | c = u'_'; |
| 76 | } |
| 77 | } |
| 78 | return output; |
| 79 | } |
| 80 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 81 | bool JavaClassGenerator::skipSymbol(SymbolState state) { |
| 82 | switch (mOptions.types) { |
| 83 | case JavaClassGeneratorOptions::SymbolTypes::kAll: |
| 84 | return false; |
| 85 | case JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate: |
| 86 | return state == SymbolState::kUndefined; |
| 87 | case JavaClassGeneratorOptions::SymbolTypes::kPublic: |
| 88 | return state != SymbolState::kPublic; |
| 89 | } |
| 90 | return true; |
| 91 | } |
| 92 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 93 | void JavaClassGenerator::generateStyleable(const StringPiece16& packageNameToGenerate, |
| 94 | const std::u16string& entryName, |
| 95 | const Styleable* styleable, |
| 96 | std::ostream* out) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 97 | const StringPiece finalModifier = mOptions.useFinal ? " final" : ""; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 98 | |
| 99 | // This must be sorted by resource ID. |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 100 | std::vector<std::pair<ResourceId, ResourceNameRef>> sortedAttributes; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 101 | sortedAttributes.reserve(styleable->entries.size()); |
| 102 | for (const auto& attr : styleable->entries) { |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 103 | // If we are not encoding final attributes, the styleable entry may have no ID |
| 104 | // if we are building a static library. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 105 | assert((!mOptions.useFinal || attr.id) && "no ID set for Styleable entry"); |
| 106 | assert(attr.name && "no name set for Styleable entry"); |
| 107 | sortedAttributes.emplace_back(attr.id ? attr.id.value() : ResourceId(0), attr.name.value()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 108 | } |
| 109 | std::sort(sortedAttributes.begin(), sortedAttributes.end()); |
| 110 | |
| 111 | // First we emit the array containing the IDs of each attribute. |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 112 | *out << " " |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 113 | << "public static final int[] " << transform(entryName) << " = {"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 114 | |
| 115 | const size_t attrCount = sortedAttributes.size(); |
| 116 | for (size_t i = 0; i < attrCount; i++) { |
| 117 | if (i % kAttribsPerLine == 0) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 118 | *out << "\n "; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 121 | *out << sortedAttributes[i].first; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 122 | if (i != attrCount - 1) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 123 | *out << ", "; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 126 | *out << "\n };\n"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 127 | |
| 128 | // Now we emit the indices into the array. |
| 129 | for (size_t i = 0; i < attrCount; i++) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 130 | *out << " " |
| 131 | << "public static" << finalModifier |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 132 | << " int " << transform(entryName); |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 133 | |
| 134 | // We may reference IDs from other packages, so prefix the entry name with |
| 135 | // the package. |
| 136 | const ResourceNameRef& itemName = sortedAttributes[i].second; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 137 | if (!itemName.package.empty() && packageNameToGenerate != itemName.package) { |
Adam Lesinski | 838a687 | 2015-05-01 13:14:05 -0700 | [diff] [blame] | 138 | *out << "_" << transform(itemName.package); |
| 139 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 140 | *out << "_" << transform(itemName.entry) << " = " << i << ";\n"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 144 | bool JavaClassGenerator::generateType(const StringPiece16& packageNameToGenerate, |
| 145 | const ResourceTablePackage* package, |
| 146 | const ResourceTableType* type, |
| 147 | std::ostream* out) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 148 | const StringPiece finalModifier = mOptions.useFinal ? " final" : ""; |
| 149 | |
| 150 | std::u16string unmangledPackage; |
| 151 | std::u16string unmangledName; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 152 | for (const auto& entry : type->entries) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 153 | if (skipSymbol(entry->symbolStatus.state)) { |
| 154 | continue; |
| 155 | } |
| 156 | |
| 157 | ResourceId id(package->id.value(), type->id.value(), entry->id.value()); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 158 | assert(id.isValid()); |
| 159 | |
| 160 | unmangledName = entry->name; |
| 161 | if (NameMangler::unmangle(&unmangledName, &unmangledPackage)) { |
| 162 | // The entry name was mangled, and we successfully unmangled it. |
| 163 | // Check that we want to emit this symbol. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 164 | if (package->name != unmangledPackage) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 165 | // Skip the entry if it doesn't belong to the package we're writing. |
| 166 | continue; |
| 167 | } |
| 168 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 169 | if (packageNameToGenerate != package->name) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 170 | // We are processing a mangled package name, |
| 171 | // but this is a non-mangled resource. |
| 172 | continue; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if (!isValidSymbol(unmangledName)) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 177 | ResourceNameRef resourceName(packageNameToGenerate, type->type, unmangledName); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 178 | std::stringstream err; |
| 179 | err << "invalid symbol name '" << resourceName << "'"; |
| 180 | mError = err.str(); |
| 181 | return false; |
| 182 | } |
| 183 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 184 | if (type->type == ResourceType::kStyleable) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 185 | assert(!entry->values.empty()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 186 | generateStyleable(packageNameToGenerate, unmangledName, static_cast<const Styleable*>( |
| 187 | entry->values.front().value.get()), out); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 188 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 189 | *out << " " << "public static" << finalModifier |
| 190 | << " int " << transform(unmangledName) << " = " << id << ";\n"; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | return true; |
| 194 | } |
| 195 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 196 | bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, std::ostream* out) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 197 | return generate(packageNameToGenerate, packageNameToGenerate, out); |
| 198 | } |
| 199 | |
| 200 | bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, |
| 201 | const StringPiece16& outPackageName, std::ostream* out) { |
| 202 | generateHeader(outPackageName, out); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 203 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 204 | *out << "public final class R {\n"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 205 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 206 | for (const auto& package : mTable->packages) { |
| 207 | for (const auto& type : package->types) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 208 | StringPiece16 typeStr; |
| 209 | if (type->type == ResourceType::kAttrPrivate) { |
| 210 | typeStr = toString(ResourceType::kAttr); |
| 211 | } else { |
| 212 | typeStr = toString(type->type); |
| 213 | } |
| 214 | *out << " public static final class " << typeStr << " {\n"; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 215 | if (!generateType(packageNameToGenerate, package.get(), type.get(), out)) { |
| 216 | return false; |
| 217 | } |
| 218 | *out << " }\n"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 219 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 222 | *out << "}\n"; |
| 223 | out->flush(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 224 | return true; |
| 225 | } |
| 226 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 227 | |
| 228 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 229 | } // namespace aapt |