blob: 87da09a7b05468bec90c2695123418aa033d90ec [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,
52 };
53
54 StringPiece doc_str;
55 uint32_t bit_mask;
56 StringPiece annotation;
57};
58
59static std::array<AnnotationRule, 2> sAnnotationRules = {{
60 {"@SystemApi", AnnotationRule::kSystemApi, "@android.annotation.SystemApi"},
61 {"@TestApi", AnnotationRule::kTestApi, "@android.annotation.TestApi"},
62}};
63
64void AnnotationProcessor::AppendCommentLine(std::string comment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070065 static constexpr std::string_view sDeprecated = "@deprecated";
Adam Lesinskica5638f2015-10-21 14:42:43 -070066
Adam Lesinski09f4d702017-08-08 10:39:55 -070067 // Treat deprecated specially, since we don't remove it from the source comment.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 if (comment.find(sDeprecated) != std::string::npos) {
Narayan Kamath1c1544f2020-02-13 14:33:47 +000069 annotation_parameter_map_[AnnotationRule::kDeprecated] = "";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070071
Adam Lesinski09f4d702017-08-08 10:39:55 -070072 for (const AnnotationRule& rule : sAnnotationRules) {
73 std::string::size_type idx = comment.find(rule.doc_str.data());
74 if (idx != std::string::npos) {
Narayan Kamath1c1544f2020-02-13 14:33:47 +000075 // Captures all parameters associated with the specified annotation rule
76 // by matching the first pair of parantheses after the rule.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070077 std::regex re(std::string(rule.doc_str) += "\\s*\\((.+)\\)");
Narayan Kamath1c1544f2020-02-13 14:33:47 +000078 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 Lesinski09f4d702017-08-08 10:39:55 -070089 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 }
Adam Lesinski626b3db2016-04-07 13:24:59 -070091
Adam Lesinski09f4d702017-08-08 10:39:55 -070092 // Check if after removal of annotations the line is empty.
93 const StringPiece trimmed = util::TrimWhitespace(comment);
94 if (trimmed.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 return;
96 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070097
Adam Lesinski09f4d702017-08-08 10:39:55 -070098 // If there was trimming to do, copy the string.
99 if (trimmed.size() != comment.size()) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700100 comment = std::string(trimmed);
Adam Lesinski09f4d702017-08-08 10:39:55 -0700101 }
102
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 if (!has_comments_) {
104 has_comments_ = true;
105 comment_ << "/**";
106 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 comment_ << "\n * " << std::move(comment);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700108}
109
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700110void AnnotationProcessor::AppendComment(StringPiece comment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 // 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 Zubrytskyia5775142022-11-02 17:49:49 -0700115 AppendCommentLine(std::string(line));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700116 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700118}
119
Adam Lesinski09f4d702017-08-08 10:39:55 -0700120void AnnotationProcessor::AppendNewLine() {
121 if (has_comments_) {
122 comment_ << "\n *";
123 }
124}
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125
Makoto Onukide6e6f22020-06-22 10:17:02 -0700126void AnnotationProcessor::Print(Printer* printer, bool strip_api_annotations) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 if (has_comments_) {
128 std::string result = comment_.str();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700129 for (StringPiece line : util::Tokenize(result, '\n')) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800130 printer->Println(line);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 }
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800132 printer->Println(" */");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 }
134
Narayan Kamath1c1544f2020-02-13 14:33:47 +0000135 if (annotation_parameter_map_.find(AnnotationRule::kDeprecated) !=
136 annotation_parameter_map_.end()) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800137 printer->Println("@Deprecated");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 }
139
Makoto Onukide6e6f22020-06-22 10:17:02 -0700140 if (strip_api_annotations) {
141 return;
142 }
Adam Lesinski09f4d702017-08-08 10:39:55 -0700143 for (const AnnotationRule& rule : sAnnotationRules) {
Narayan Kamath1c1544f2020-02-13 14:33:47 +0000144 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 Lesinski09f4d702017-08-08 10:39:55 -0700151 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 }
Adam Lesinski76565542016-03-10 21:55:04 -0800153}
154
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155} // namespace aapt