blob: a4610b2575b9c5782ef6c48d3710b825a96eeaf1 [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>
Adam Lesinskica5638f2015-10-21 14:42:43 -070021
Adam Lesinskie967d3f2017-07-24 18:19:36 -070022#include "text/Unicode.h"
23#include "text/Utf8Iterator.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024#include "util/Util.h"
25
Adam Lesinskia693c4a2017-11-09 11:29:39 -080026using ::aapt::text::Printer;
Adam Lesinskie967d3f2017-07-24 18:19:36 -070027using ::aapt::text::Utf8Iterator;
28using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080029
Adam Lesinskica5638f2015-10-21 14:42:43 -070030namespace aapt {
31
Adam Lesinskie967d3f2017-07-24 18:19:36 -070032StringPiece AnnotationProcessor::ExtractFirstSentence(const StringPiece& comment) {
33 Utf8Iterator iter(comment);
34 while (iter.HasNext()) {
35 const char32_t codepoint = iter.Next();
36 if (codepoint == U'.') {
37 const size_t current_position = iter.Position();
38 if (!iter.HasNext() || text::IsWhitespace(iter.Next())) {
39 return comment.substr(0, current_position);
40 }
41 }
42 }
43 return comment;
44}
45
Adam Lesinski09f4d702017-08-08 10:39:55 -070046struct AnnotationRule {
47 enum : uint32_t {
48 kDeprecated = 0x01,
49 kSystemApi = 0x02,
50 kTestApi = 0x04,
51 };
52
53 StringPiece doc_str;
54 uint32_t bit_mask;
55 StringPiece annotation;
56};
57
58static std::array<AnnotationRule, 2> sAnnotationRules = {{
59 {"@SystemApi", AnnotationRule::kSystemApi, "@android.annotation.SystemApi"},
60 {"@TestApi", AnnotationRule::kTestApi, "@android.annotation.TestApi"},
61}};
62
63void AnnotationProcessor::AppendCommentLine(std::string comment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 static const std::string sDeprecated = "@deprecated";
Adam Lesinskica5638f2015-10-21 14:42:43 -070065
Adam Lesinski09f4d702017-08-08 10:39:55 -070066 // Treat deprecated specially, since we don't remove it from the source comment.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 if (comment.find(sDeprecated) != std::string::npos) {
Adam Lesinski09f4d702017-08-08 10:39:55 -070068 annotation_bit_mask_ |= AnnotationRule::kDeprecated;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070070
Adam Lesinski09f4d702017-08-08 10:39:55 -070071 for (const AnnotationRule& rule : sAnnotationRules) {
72 std::string::size_type idx = comment.find(rule.doc_str.data());
73 if (idx != std::string::npos) {
74 annotation_bit_mask_ |= rule.bit_mask;
75 comment.erase(comment.begin() + idx, comment.begin() + idx + rule.doc_str.size());
76 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 }
Adam Lesinski626b3db2016-04-07 13:24:59 -070078
Adam Lesinski09f4d702017-08-08 10:39:55 -070079 // Check if after removal of annotations the line is empty.
80 const StringPiece trimmed = util::TrimWhitespace(comment);
81 if (trimmed.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 return;
83 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070084
Adam Lesinski09f4d702017-08-08 10:39:55 -070085 // If there was trimming to do, copy the string.
86 if (trimmed.size() != comment.size()) {
87 comment = trimmed.to_string();
88 }
89
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 if (!has_comments_) {
91 has_comments_ = true;
92 comment_ << "/**";
93 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 comment_ << "\n * " << std::move(comment);
Adam Lesinskica5638f2015-10-21 14:42:43 -070095}
96
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097void AnnotationProcessor::AppendComment(const StringPiece& comment) {
98 // We need to process line by line to clean-up whitespace and append prefixes.
99 for (StringPiece line : util::Tokenize(comment, '\n')) {
100 line = util::TrimWhitespace(line);
101 if (!line.empty()) {
Adam Lesinski09f4d702017-08-08 10:39:55 -0700102 AppendCommentLine(line.to_string());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700103 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700105}
106
Adam Lesinski09f4d702017-08-08 10:39:55 -0700107void AnnotationProcessor::AppendNewLine() {
108 if (has_comments_) {
109 comment_ << "\n *";
110 }
111}
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800113void AnnotationProcessor::Print(Printer* printer) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 if (has_comments_) {
115 std::string result = comment_.str();
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800116 for (const StringPiece& line : util::Tokenize(result, '\n')) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800117 printer->Println(line);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 }
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800119 printer->Println(" */");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 }
121
Adam Lesinski09f4d702017-08-08 10:39:55 -0700122 if (annotation_bit_mask_ & AnnotationRule::kDeprecated) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800123 printer->Println("@Deprecated");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 }
125
Adam Lesinski09f4d702017-08-08 10:39:55 -0700126 for (const AnnotationRule& rule : sAnnotationRules) {
127 if (annotation_bit_mask_ & rule.bit_mask) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800128 printer->Println(rule.annotation);
Adam Lesinski09f4d702017-08-08 10:39:55 -0700129 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 }
Adam Lesinski76565542016-03-10 21:55:04 -0800131}
132
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133} // namespace aapt