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
17 #include "java/AnnotationProcessor.h"
18
19 #include <algorithm>
20 #include <array>
21 #include <regex>
22
23 #include "text/Unicode.h"
24 #include "text/Utf8Iterator.h"
25 #include "util/Util.h"
26
27 using ::aapt::text::Printer;
28 using ::aapt::text::Utf8Iterator;
29 using ::android::StringPiece;
30
31 namespace aapt {
32
ExtractFirstSentence(StringPiece comment)33 StringPiece AnnotationProcessor::ExtractFirstSentence(StringPiece comment) {
34 Utf8Iterator iter(comment);
35 while (iter.HasNext()) {
36 const char32_t codepoint = iter.Next();
37 if (codepoint == U'.') {
38 const size_t current_position = iter.Position();
39 if (!iter.HasNext() || text::IsWhitespace(iter.Next())) {
40 return comment.substr(0, current_position);
41 }
42 }
43 }
44 return comment;
45 }
46
47 struct AnnotationRule {
48 enum : uint32_t {
49 kDeprecated = 0x01,
50 kSystemApi = 0x02,
51 kTestApi = 0x04,
52 kFlaggedApi = 0x08,
53 };
54
55 StringPiece doc_str;
56 uint32_t bit_mask;
57 StringPiece annotation;
58 bool preserve_params;
59 };
60
61 static std::array<AnnotationRule, 3> sAnnotationRules = {{
62 {"@SystemApi", AnnotationRule::kSystemApi, "@android.annotation.SystemApi", true},
63 {"@TestApi", AnnotationRule::kTestApi, "@android.annotation.TestApi", false},
64 {"@FlaggedApi", AnnotationRule::kFlaggedApi, "@android.annotation.FlaggedApi", true},
65 }};
66
AppendCommentLine(std::string comment,bool add_api_annotations)67 void AnnotationProcessor::AppendCommentLine(std::string comment, bool add_api_annotations) {
68 static constexpr std::string_view sDeprecated = "@deprecated";
69
70 if (add_api_annotations) {
71 // Treat deprecated specially, since we don't remove it from the source comment.
72 if (comment.find(sDeprecated) != std::string::npos) {
73 annotation_parameter_map_[AnnotationRule::kDeprecated] = "";
74 }
75
76 for (const AnnotationRule& rule : sAnnotationRules) {
77 std::string::size_type idx = comment.find(rule.doc_str.data());
78 if (idx != std::string::npos) {
79 // Captures all parameters associated with the specified annotation rule
80 // by matching the first pair of parentheses after the rule.
81 std::regex re(std::string(rule.doc_str).append(R"(\s*\((.+)\))"));
82 std::smatch match_result;
83 const bool is_match = std::regex_search(comment, match_result, re);
84 if (is_match && rule.preserve_params) {
85 annotation_parameter_map_[rule.bit_mask] = match_result[1].str();
86 comment.erase(comment.begin() + match_result.position(),
87 comment.begin() + match_result.position() + match_result.length());
88 } else {
89 annotation_parameter_map_[rule.bit_mask] = "";
90 comment.erase(comment.begin() + idx, comment.begin() + idx + rule.doc_str.size());
91 }
92 }
93 }
94 }
95
96 // Check if after removal of annotations the line is empty.
97 const StringPiece trimmed = util::TrimWhitespace(comment);
98 if (trimmed.empty()) {
99 return;
100 }
101
102 // If there was trimming to do, copy the string.
103 if (trimmed.size() != comment.size()) {
104 comment = std::string(trimmed);
105 }
106
107 if (!has_comments_) {
108 has_comments_ = true;
109 comment_ << "/**";
110 }
111 comment_ << "\n * " << std::move(comment);
112 }
113
AppendComment(StringPiece comment,bool add_api_annotations)114 void AnnotationProcessor::AppendComment(StringPiece comment, bool add_api_annotations) {
115 // We need to process line by line to clean-up whitespace and append prefixes.
116 for (StringPiece line : util::Tokenize(comment, '\n')) {
117 line = util::TrimWhitespace(line);
118 if (!line.empty()) {
119 AppendCommentLine(std::string(line), add_api_annotations);
120 }
121 }
122 }
123
AppendNewLine()124 void AnnotationProcessor::AppendNewLine() {
125 if (has_comments_) {
126 comment_ << "\n *";
127 }
128 }
129
Print(Printer * printer,bool strip_api_annotations) const130 void AnnotationProcessor::Print(Printer* printer, bool strip_api_annotations) const {
131 if (has_comments_) {
132 std::string result = comment_.str();
133 for (StringPiece line : util::Tokenize(result, '\n')) {
134 printer->Println(line);
135 }
136 printer->Println(" */");
137 }
138
139 if (annotation_parameter_map_.find(AnnotationRule::kDeprecated) !=
140 annotation_parameter_map_.end()) {
141 printer->Println("@Deprecated");
142 }
143
144 if (strip_api_annotations) {
145 return;
146 }
147 for (const AnnotationRule& rule : sAnnotationRules) {
148 const auto& it = annotation_parameter_map_.find(rule.bit_mask);
149 if (it != annotation_parameter_map_.end()) {
150 printer->Print(rule.annotation);
151 if (!it->second.empty()) {
152 printer->Print("(").Print(it->second).Print(")");
153 }
154 printer->Print("\n");
155 }
156 }
157 }
158
159 } // namespace aapt
160