From 626b3dbf74f02ae630ae0089632f5962340694dc Mon Sep 17 00:00:00 2001 From: Adam Lesinski Date: Thu, 7 Apr 2016 13:24:59 -0700 Subject: AAPT2: Clean up R JavaDoc generation - Don't generate private attributes in public R.java - Strip out @SystemApi from comment when generating @android.annotation.SystemApi - Only emit a single line (up to the first period) of an attribute's comment within a styleable's attribute table. Change-Id: Id6316a6861540325934133958939a12074ad4428 --- tools/aapt2/java/JavaClassGenerator.cpp | 60 ++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 12 deletions(-) (limited to 'tools/aapt2/java/JavaClassGenerator.cpp') diff --git a/tools/aapt2/java/JavaClassGenerator.cpp b/tools/aapt2/java/JavaClassGenerator.cpp index 32b8600358b8..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; std::string fieldName; + std::unique_ptr 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(*symbol); } sortedAttributes.push_back(std::move(styleableAttr)); } @@ -273,6 +274,16 @@ void JavaClassGenerator::addMembersToStyleableClass(const StringPiece16& package "AttributeDescription\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 << ""; styleableComment << "{@link #" @@ -284,14 +295,30 @@ void JavaClassGenerator::addMembersToStyleableClass(const StringPiece16& package styleableComment << ""; styleableComment << ""; - 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 << "\n"; + styleableComment << attrCommentLine << "\n"; } styleableComment << "\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; @@ -323,8 +361,8 @@ void JavaClassGenerator::addMembersToStyleableClass(const StringPiece16& package 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;; -- cgit v1.2.3