• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(const StringPiece & comment)33 StringPiece AnnotationProcessor::ExtractFirstSentence(const 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   };
53 
54   StringPiece doc_str;
55   uint32_t bit_mask;
56   StringPiece annotation;
57 };
58 
59 static std::array<AnnotationRule, 2> sAnnotationRules = {{
60     {"@SystemApi", AnnotationRule::kSystemApi, "@android.annotation.SystemApi"},
61     {"@TestApi", AnnotationRule::kTestApi, "@android.annotation.TestApi"},
62 }};
63 
AppendCommentLine(std::string comment)64 void AnnotationProcessor::AppendCommentLine(std::string comment) {
65   static const std::string sDeprecated = "@deprecated";
66 
67   // Treat deprecated specially, since we don't remove it from the source comment.
68   if (comment.find(sDeprecated) != std::string::npos) {
69     annotation_parameter_map_[AnnotationRule::kDeprecated] = "";
70   }
71 
72   for (const AnnotationRule& rule : sAnnotationRules) {
73     std::string::size_type idx = comment.find(rule.doc_str.data());
74     if (idx != std::string::npos) {
75       // Captures all parameters associated with the specified annotation rule
76       // by matching the first pair of parantheses after the rule.
77       std::regex re(rule.doc_str.to_string() + "\\s*\\((.+)\\)");
78       std::smatch match_result;
79       const bool is_match = std::regex_search(comment, match_result, re);
80       // We currently only capture and preserve parameters for SystemApi.
81       if (is_match && rule.bit_mask == AnnotationRule::kSystemApi) {
82         annotation_parameter_map_[rule.bit_mask] = match_result[1].str();
83         comment.erase(comment.begin() + match_result.position(),
84                       comment.begin() + match_result.position() + match_result.length());
85       } else {
86         annotation_parameter_map_[rule.bit_mask] = "";
87         comment.erase(comment.begin() + idx, comment.begin() + idx + rule.doc_str.size());
88       }
89     }
90   }
91 
92   // Check if after removal of annotations the line is empty.
93   const StringPiece trimmed = util::TrimWhitespace(comment);
94   if (trimmed.empty()) {
95     return;
96   }
97 
98   // If there was trimming to do, copy the string.
99   if (trimmed.size() != comment.size()) {
100     comment = trimmed.to_string();
101   }
102 
103   if (!has_comments_) {
104     has_comments_ = true;
105     comment_ << "/**";
106   }
107   comment_ << "\n * " << std::move(comment);
108 }
109 
AppendComment(const StringPiece & comment)110 void AnnotationProcessor::AppendComment(const StringPiece& comment) {
111   // We need to process line by line to clean-up whitespace and append prefixes.
112   for (StringPiece line : util::Tokenize(comment, '\n')) {
113     line = util::TrimWhitespace(line);
114     if (!line.empty()) {
115       AppendCommentLine(line.to_string());
116     }
117   }
118 }
119 
AppendNewLine()120 void AnnotationProcessor::AppendNewLine() {
121   if (has_comments_) {
122     comment_ << "\n *";
123   }
124 }
125 
Print(Printer * printer,bool strip_api_annotations) const126 void AnnotationProcessor::Print(Printer* printer, bool strip_api_annotations) const {
127   if (has_comments_) {
128     std::string result = comment_.str();
129     for (const StringPiece& line : util::Tokenize(result, '\n')) {
130       printer->Println(line);
131     }
132     printer->Println(" */");
133   }
134 
135   if (annotation_parameter_map_.find(AnnotationRule::kDeprecated) !=
136         annotation_parameter_map_.end()) {
137     printer->Println("@Deprecated");
138   }
139 
140   if (strip_api_annotations) {
141     return;
142   }
143   for (const AnnotationRule& rule : sAnnotationRules) {
144     const auto& it = annotation_parameter_map_.find(rule.bit_mask);
145     if (it != annotation_parameter_map_.end()) {
146       printer->Print(rule.annotation);
147       if (!it->second.empty()) {
148         printer->Print("(").Print(it->second).Print(")");
149       }
150       printer->Print("\n");
151     }
152   }
153 }
154 
155 }  // namespace aapt
156