From 1c1544ff96e7f87154c2eff62c1da3aee46b569f Mon Sep 17 00:00:00 2001 From: Narayan Kamath Date: Thu, 13 Feb 2020 14:33:47 +0000 Subject: aapt2: Preserve annotation parameters for SystemApi. SystemAPI can now be parameterized. Note that this change preserves the content of the annotation params without attempting to interpret them in any way. This means that references to types outside of java.lang must be fully qualified. Test: atest aapt2_tests Bug: 147581540 Change-Id: I0630fb1c7c7a7f5918ff2dca32fb8e078b367751 --- tools/aapt2/java/AnnotationProcessor.cpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'tools/aapt2/java/AnnotationProcessor.cpp') diff --git a/tools/aapt2/java/AnnotationProcessor.cpp b/tools/aapt2/java/AnnotationProcessor.cpp index a4610b2575b9..cec59e75831d 100644 --- a/tools/aapt2/java/AnnotationProcessor.cpp +++ b/tools/aapt2/java/AnnotationProcessor.cpp @@ -18,6 +18,7 @@ #include #include +#include #include "text/Unicode.h" #include "text/Utf8Iterator.h" @@ -65,14 +66,26 @@ void AnnotationProcessor::AppendCommentLine(std::string comment) { // Treat deprecated specially, since we don't remove it from the source comment. if (comment.find(sDeprecated) != std::string::npos) { - annotation_bit_mask_ |= AnnotationRule::kDeprecated; + annotation_parameter_map_[AnnotationRule::kDeprecated] = ""; } for (const AnnotationRule& rule : sAnnotationRules) { std::string::size_type idx = comment.find(rule.doc_str.data()); if (idx != std::string::npos) { - annotation_bit_mask_ |= rule.bit_mask; - comment.erase(comment.begin() + idx, comment.begin() + idx + rule.doc_str.size()); + // Captures all parameters associated with the specified annotation rule + // by matching the first pair of parantheses after the rule. + std::regex re(rule.doc_str.to_string() + "\\s*\\((.+)\\)"); + std::smatch match_result; + const bool is_match = std::regex_search(comment, match_result, re); + // We currently only capture and preserve parameters for SystemApi. + if (is_match && rule.bit_mask == AnnotationRule::kSystemApi) { + annotation_parameter_map_[rule.bit_mask] = match_result[1].str(); + comment.erase(comment.begin() + match_result.position(), + comment.begin() + match_result.position() + match_result.length()); + } else { + annotation_parameter_map_[rule.bit_mask] = ""; + comment.erase(comment.begin() + idx, comment.begin() + idx + rule.doc_str.size()); + } } } @@ -119,13 +132,19 @@ void AnnotationProcessor::Print(Printer* printer) const { printer->Println(" */"); } - if (annotation_bit_mask_ & AnnotationRule::kDeprecated) { + if (annotation_parameter_map_.find(AnnotationRule::kDeprecated) != + annotation_parameter_map_.end()) { printer->Println("@Deprecated"); } for (const AnnotationRule& rule : sAnnotationRules) { - if (annotation_bit_mask_ & rule.bit_mask) { - printer->Println(rule.annotation); + const auto& it = annotation_parameter_map_.find(rule.bit_mask); + if (it != annotation_parameter_map_.end()) { + printer->Print(rule.annotation); + if (!it->second.empty()) { + printer->Print("(").Print(it->second).Print(")"); + } + printer->Print("\n"); } } } -- cgit v1.2.3 From de6e6f2098042bb91f7dac965d2b047c74c920f2 Mon Sep 17 00:00:00 2001 From: Makoto Onuki Date: Mon, 22 Jun 2020 10:17:02 -0700 Subject: Don't add API annotations in the internal R.java I'm trying to enable a check for the following structure: ``` /** @hide */ public class Class1 { /** @hide */ @SystemApi // Invalid because the class is hidden. public void method1() { } } ``` The internal R.java file violates this, which this change is going to fix. Bug: 159162473 Test: build (treehugger) Test: atest aapt2_tests Change-Id: I613e8611ddaf5f8e4761d351d4cd0142d59c7cc9 --- tools/aapt2/java/AnnotationProcessor.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'tools/aapt2/java/AnnotationProcessor.cpp') diff --git a/tools/aapt2/java/AnnotationProcessor.cpp b/tools/aapt2/java/AnnotationProcessor.cpp index cec59e75831d..482d91aeb491 100644 --- a/tools/aapt2/java/AnnotationProcessor.cpp +++ b/tools/aapt2/java/AnnotationProcessor.cpp @@ -123,7 +123,7 @@ void AnnotationProcessor::AppendNewLine() { } } -void AnnotationProcessor::Print(Printer* printer) const { +void AnnotationProcessor::Print(Printer* printer, bool strip_api_annotations) const { if (has_comments_) { std::string result = comment_.str(); for (const StringPiece& line : util::Tokenize(result, '\n')) { @@ -137,6 +137,9 @@ void AnnotationProcessor::Print(Printer* printer) const { printer->Println("@Deprecated"); } + if (strip_api_annotations) { + return; + } for (const AnnotationRule& rule : sAnnotationRules) { const auto& it = annotation_parameter_map_.find(rule.bit_mask); if (it != annotation_parameter_map_.end()) { -- cgit v1.2.3