blob: 8c644cf8333945b7e3e8022e12dd67dfc8de30d2 [file] [log] [blame]
Adam Lesinskica5638f2015-10-21 14:42:43 -07001/*
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 Lesinskica5638f2015-10-21 14:42:43 -070018
19#include <algorithm>
Adam Lesinski09f4d702017-08-08 10:39:55 -070020#include <array>
Narayan Kamath1c1544f2020-02-13 14:33:47 +000021#include <regex>
Adam Lesinskica5638f2015-10-21 14:42:43 -070022
Adam Lesinskie967d3f2017-07-24 18:19:36 -070023#include "text/Unicode.h"
24#include "text/Utf8Iterator.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025#include "util/Util.h"
26
Adam Lesinskia693c4a2017-11-09 11:29:39 -080027using ::aapt::text::Printer;
Adam Lesinskie967d3f2017-07-24 18:19:36 -070028using ::aapt::text::Utf8Iterator;
29using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080030
Adam Lesinskica5638f2015-10-21 14:42:43 -070031namespace aapt {
32
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070033StringPiece AnnotationProcessor::ExtractFirstSentence(StringPiece comment) {
Adam Lesinskie967d3f2017-07-24 18:19:36 -070034 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 Lesinski09f4d702017-08-08 10:39:55 -070047struct AnnotationRule {
48 enum : uint32_t {
49 kDeprecated = 0x01,
50 kSystemApi = 0x02,
51 kTestApi = 0x04,
Mark Punzalan6fa7a3c2023-10-06 11:01:32 -070052 kFlaggedApi = 0x08,
Adam Lesinski09f4d702017-08-08 10:39:55 -070053 };
54
55 StringPiece doc_str;
56 uint32_t bit_mask;
57 StringPiece annotation;
Mark Punzalan6fa7a3c2023-10-06 11:01:32 -070058 bool preserve_params;
Adam Lesinski09f4d702017-08-08 10:39:55 -070059};
60
Mark Punzalan6fa7a3c2023-10-06 11:01:32 -070061static 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},
Adam Lesinski09f4d702017-08-08 10:39:55 -070065}};
66
67void AnnotationProcessor::AppendCommentLine(std::string comment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070068 static constexpr std::string_view sDeprecated = "@deprecated";
Adam Lesinskica5638f2015-10-21 14:42:43 -070069
Adam Lesinski09f4d702017-08-08 10:39:55 -070070 // Treat deprecated specially, since we don't remove it from the source comment.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 if (comment.find(sDeprecated) != std::string::npos) {
Narayan Kamath1c1544f2020-02-13 14:33:47 +000072 annotation_parameter_map_[AnnotationRule::kDeprecated] = "";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070074
Adam Lesinski09f4d702017-08-08 10:39:55 -070075 for (const AnnotationRule& rule : sAnnotationRules) {
76 std::string::size_type idx = comment.find(rule.doc_str.data());
77 if (idx != std::string::npos) {
Narayan Kamath1c1544f2020-02-13 14:33:47 +000078 // Captures all parameters associated with the specified annotation rule
Mark Punzalan6fa7a3c2023-10-06 11:01:32 -070079 // by matching the first pair of parentheses after the rule.
80 std::regex re(std::string(rule.doc_str).append(R"(\s*\((.+)\))"));
Narayan Kamath1c1544f2020-02-13 14:33:47 +000081 std::smatch match_result;
82 const bool is_match = std::regex_search(comment, match_result, re);
Mark Punzalan6fa7a3c2023-10-06 11:01:32 -070083 if (is_match && rule.preserve_params) {
Narayan Kamath1c1544f2020-02-13 14:33:47 +000084 annotation_parameter_map_[rule.bit_mask] = match_result[1].str();
85 comment.erase(comment.begin() + match_result.position(),
86 comment.begin() + match_result.position() + match_result.length());
87 } else {
88 annotation_parameter_map_[rule.bit_mask] = "";
89 comment.erase(comment.begin() + idx, comment.begin() + idx + rule.doc_str.size());
90 }
Adam Lesinski09f4d702017-08-08 10:39:55 -070091 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 }
Adam Lesinski626b3db2016-04-07 13:24:59 -070093
Adam Lesinski09f4d702017-08-08 10:39:55 -070094 // Check if after removal of annotations the line is empty.
95 const StringPiece trimmed = util::TrimWhitespace(comment);
96 if (trimmed.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 return;
98 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070099
Adam Lesinski09f4d702017-08-08 10:39:55 -0700100 // If there was trimming to do, copy the string.
101 if (trimmed.size() != comment.size()) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700102 comment = std::string(trimmed);
Adam Lesinski09f4d702017-08-08 10:39:55 -0700103 }
104
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 if (!has_comments_) {
106 has_comments_ = true;
107 comment_ << "/**";
108 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 comment_ << "\n * " << std::move(comment);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700110}
111
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700112void AnnotationProcessor::AppendComment(StringPiece comment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 // We need to process line by line to clean-up whitespace and append prefixes.
114 for (StringPiece line : util::Tokenize(comment, '\n')) {
115 line = util::TrimWhitespace(line);
116 if (!line.empty()) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700117 AppendCommentLine(std::string(line));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700118 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700120}
121
Adam Lesinski09f4d702017-08-08 10:39:55 -0700122void AnnotationProcessor::AppendNewLine() {
123 if (has_comments_) {
124 comment_ << "\n *";
125 }
126}
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127
Makoto Onukide6e6f22020-06-22 10:17:02 -0700128void AnnotationProcessor::Print(Printer* printer, bool strip_api_annotations) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 if (has_comments_) {
130 std::string result = comment_.str();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700131 for (StringPiece line : util::Tokenize(result, '\n')) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800132 printer->Println(line);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 }
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800134 printer->Println(" */");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 }
136
Narayan Kamath1c1544f2020-02-13 14:33:47 +0000137 if (annotation_parameter_map_.find(AnnotationRule::kDeprecated) !=
138 annotation_parameter_map_.end()) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800139 printer->Println("@Deprecated");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 }
141
Makoto Onukide6e6f22020-06-22 10:17:02 -0700142 if (strip_api_annotations) {
143 return;
144 }
Adam Lesinski09f4d702017-08-08 10:39:55 -0700145 for (const AnnotationRule& rule : sAnnotationRules) {
Narayan Kamath1c1544f2020-02-13 14:33:47 +0000146 const auto& it = annotation_parameter_map_.find(rule.bit_mask);
147 if (it != annotation_parameter_map_.end()) {
148 printer->Print(rule.annotation);
149 if (!it->second.empty()) {
150 printer->Print("(").Print(it->second).Print(")");
151 }
152 printer->Print("\n");
Adam Lesinski09f4d702017-08-08 10:39:55 -0700153 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 }
Adam Lesinski76565542016-03-10 21:55:04 -0800155}
156
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157} // namespace aapt