diff options
Diffstat (limited to 'tools/aapt2/java/JavaClassGenerator.cpp')
-rw-r--r-- | tools/aapt2/java/JavaClassGenerator.cpp | 80 |
1 files changed, 63 insertions, 17 deletions
diff --git a/tools/aapt2/java/JavaClassGenerator.cpp b/tools/aapt2/java/JavaClassGenerator.cpp index 2d076c2d5a66..24347a1cdc1e 100644 --- a/tools/aapt2/java/JavaClassGenerator.cpp +++ b/tools/aapt2/java/JavaClassGenerator.cpp @@ -188,8 +188,8 @@ bool JavaClassGenerator::skipSymbol(SymbolState state) { struct StyleableAttr { const Reference* attrRef; - std::shared_ptr<Attribute> attribute; std::string fieldName; + std::unique_ptr<SymbolTable::Symbol> symbol; }; static bool lessStyleableAttr(const StyleableAttr& lhs, const StyleableAttr& rhs) { @@ -245,8 +245,9 @@ void JavaClassGenerator::addMembersToStyleableClass(const StringPiece16& package // legal values for this attribute. const SymbolTable::Symbol* symbol = mContext->getExternalSymbols()->findByReference( mangledReference); - if (symbol) { - styleableAttr.attribute = symbol->attribute; + if (symbol && symbol->attribute) { + // Copy the symbol data structure because the returned instance can be destroyed. + styleableAttr.symbol = util::make_unique<SymbolTable::Symbol>(*symbol); } sortedAttributes.push_back(std::move(styleableAttr)); } @@ -273,6 +274,16 @@ void JavaClassGenerator::addMembersToStyleableClass(const StringPiece16& package "<tr><th>Attribute</th><th>Description</th></tr>\n"; for (const StyleableAttr& entry : sortedAttributes) { + if (!entry.symbol) { + continue; + } + + if (mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic && + !entry.symbol->isPublic) { + // Don't write entries for non-public attributes. + continue; + } + const ResourceName& attrName = entry.attrRef->name.value(); styleableComment << "<tr><td>"; styleableComment << "<code>{@link #" @@ -284,14 +295,30 @@ void JavaClassGenerator::addMembersToStyleableClass(const StringPiece16& package styleableComment << "</td>"; styleableComment << "<td>"; - if (entry.attribute) { - styleableComment << entry.attribute->getComment(); + + // Only use the comment up until the first '.'. This is to stay compatible with + // the way old AAPT did it (presumably to keep it short and to avoid including + // annotations like @hide which would affect this Styleable). + StringPiece16 attrCommentLine = entry.symbol->attribute->getComment(); + auto iter = std::find(attrCommentLine.begin(), attrCommentLine.end(), u'.'); + if (iter != attrCommentLine.end()) { + attrCommentLine = attrCommentLine.substr( + 0, (iter - attrCommentLine.begin()) + 1); } - styleableComment << "</td></tr>\n"; + styleableComment << attrCommentLine << "</td></tr>\n"; } styleableComment << "</table>\n"; for (const StyleableAttr& entry : sortedAttributes) { + if (!entry.symbol) { + continue; + } + + if (mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic && + !entry.symbol->isPublic) { + // Don't write entries for non-public attributes. + continue; + } styleableComment << "@see #" << entry.fieldName << "\n"; } @@ -310,6 +337,17 @@ void JavaClassGenerator::addMembersToStyleableClass(const StringPiece16& package // Now we emit the indices into the array. for (size_t i = 0; i < attrCount; i++) { const StyleableAttr& styleableAttr = sortedAttributes[i]; + + if (!styleableAttr.symbol) { + continue; + } + + if (mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic && + !styleableAttr.symbol->isPublic) { + // Don't write entries for non-public attributes. + continue; + } + const ResourceName& attrName = styleableAttr.attrRef->name.value(); StringPiece16 packageName = attrName.package; @@ -318,13 +356,13 @@ void JavaClassGenerator::addMembersToStyleableClass(const StringPiece16& package } std::unique_ptr<IntMember> indexMember = util::make_unique<IntMember>( - sortedAttributes[i].fieldName, i); + sortedAttributes[i].fieldName, static_cast<uint32_t>(i)); AnnotationProcessor* attrProcessor = indexMember->getCommentBuilder(); StringPiece16 comment = styleableAttr.attrRef->getComment(); - if (styleableAttr.attribute && comment.empty()) { - comment = styleableAttr.attribute->getComment(); + if (styleableAttr.symbol->attribute && comment.empty()) { + comment = styleableAttr.symbol->attribute->getComment(); } if (!comment.empty()) { @@ -342,10 +380,8 @@ void JavaClassGenerator::addMembersToStyleableClass(const StringPiece16& package attrProcessor->appendNewLine(); - if (styleableAttr.attribute) { - addAttributeFormatDoc(attrProcessor, styleableAttr.attribute.get()); - attrProcessor->appendNewLine(); - } + addAttributeFormatDoc(attrProcessor, styleableAttr.symbol->attribute.get()); + attrProcessor->appendNewLine(); std::stringstream doclavaName; doclavaName << "@attr name " << packageName << ":" << attrName.entry;; @@ -437,6 +473,15 @@ bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, st return generate(packageNameToGenerate, packageNameToGenerate, out); } +static void appendJavaDocAnnotations(const std::vector<std::string>& annotations, + AnnotationProcessor* processor) { + for (const std::string& annotation : annotations) { + std::string properAnnotation = "@"; + properAnnotation += annotation; + processor->appendComment(properAnnotation); + } +} + bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, const StringPiece16& outPackageName, std::ostream* out) { @@ -477,14 +522,17 @@ bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic) { // When generating a public R class, we don't want Styleable to be part of the API. // It is only emitted for documentation purposes. - AnnotationProcessor* processor = classDef->getCommentBuilder(); - processor->appendComment("@doconly"); + classDef->getCommentBuilder()->appendComment("@doconly"); } + appendJavaDocAnnotations(mOptions.javadocAnnotations, classDef->getCommentBuilder()); + rClass.addMember(std::move(classDef)); } } + appendJavaDocAnnotations(mOptions.javadocAnnotations, rClass.getCommentBuilder()); + if (!ClassDefinition::writeJavaFile(&rClass, util::utf16ToUtf8(outPackageName), mOptions.useFinal, out)) { return false; @@ -494,6 +542,4 @@ bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, return true; } - - } // namespace aapt |