Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 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" |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 20 | #include <array> |
Narayan Kamath | 1c1544f | 2020-02-13 14:33:47 +0000 | [diff] [blame] | 21 | #include <regex> |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 22 | |
Adam Lesinski | e967d3f | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 23 | #include "text/Unicode.h" |
| 24 | #include "text/Utf8Iterator.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 25 | #include "util/Util.h" |
| 26 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 27 | using ::aapt::text::Printer; |
Adam Lesinski | e967d3f | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 28 | using ::aapt::text::Utf8Iterator; |
| 29 | using ::android::StringPiece; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 30 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 31 | namespace aapt { |
| 32 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 33 | StringPiece AnnotationProcessor::ExtractFirstSentence(StringPiece comment) { |
Adam Lesinski | e967d3f | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 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 | |
| 64 | void AnnotationProcessor::AppendCommentLine(std::string comment) { |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 65 | static constexpr std::string_view sDeprecated = "@deprecated"; |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 66 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 67 | // Treat deprecated specially, since we don't remove it from the source comment. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 68 | if (comment.find(sDeprecated) != std::string::npos) { |
Narayan Kamath | 1c1544f | 2020-02-13 14:33:47 +0000 | [diff] [blame] | 69 | annotation_parameter_map_[AnnotationRule::kDeprecated] = ""; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 71 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 72 | for (const AnnotationRule& rule : sAnnotationRules) { |
| 73 | std::string::size_type idx = comment.find(rule.doc_str.data()); |
| 74 | if (idx != std::string::npos) { |
Narayan Kamath | 1c1544f | 2020-02-13 14:33:47 +0000 | [diff] [blame] | 75 | // Captures all parameters associated with the specified annotation rule |
| 76 | // by matching the first pair of parantheses after the rule. |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 77 | std::regex re(std::string(rule.doc_str) += "\\s*\\((.+)\\)"); |
Narayan Kamath | 1c1544f | 2020-02-13 14:33:47 +0000 | [diff] [blame] | 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 | } |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 89 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | } |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 91 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 92 | // Check if after removal of annotations the line is empty. |
| 93 | const StringPiece trimmed = util::TrimWhitespace(comment); |
| 94 | if (trimmed.empty()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | return; |
| 96 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 97 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 98 | // If there was trimming to do, copy the string. |
| 99 | if (trimmed.size() != comment.size()) { |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 100 | comment = std::string(trimmed); |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 103 | if (!has_comments_) { |
| 104 | has_comments_ = true; |
| 105 | comment_ << "/**"; |
| 106 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 107 | comment_ << "\n * " << std::move(comment); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 110 | void AnnotationProcessor::AppendComment(StringPiece comment) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 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()) { |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 115 | AppendCommentLine(std::string(line)); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 116 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 117 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 120 | void AnnotationProcessor::AppendNewLine() { |
| 121 | if (has_comments_) { |
| 122 | comment_ << "\n *"; |
| 123 | } |
| 124 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 125 | |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame] | 126 | void AnnotationProcessor::Print(Printer* printer, bool strip_api_annotations) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 127 | if (has_comments_) { |
| 128 | std::string result = comment_.str(); |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 129 | for (StringPiece line : util::Tokenize(result, '\n')) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 130 | printer->Println(line); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 131 | } |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 132 | printer->Println(" */"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Narayan Kamath | 1c1544f | 2020-02-13 14:33:47 +0000 | [diff] [blame] | 135 | if (annotation_parameter_map_.find(AnnotationRule::kDeprecated) != |
| 136 | annotation_parameter_map_.end()) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 137 | printer->Println("@Deprecated"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame] | 140 | if (strip_api_annotations) { |
| 141 | return; |
| 142 | } |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 143 | for (const AnnotationRule& rule : sAnnotationRules) { |
Narayan Kamath | 1c1544f | 2020-02-13 14:33:47 +0000 | [diff] [blame] | 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"); |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 151 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 152 | } |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 155 | } // namespace aapt |