blob: c93461a6689921b41c3b82401dcbe7e189ac1118 [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 Lesinskie967d3f2017-07-24 18:19:36 -070026using ::aapt::text::Utf8Iterator;
27using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080028
Adam Lesinskica5638f2015-10-21 14:42:43 -070029namespace aapt {
30
Adam Lesinskie967d3f2017-07-24 18:19:36 -070031StringPiece AnnotationProcessor::ExtractFirstSentence(const StringPiece& comment) {
32 Utf8Iterator iter(comment);
33 while (iter.HasNext()) {
34 const char32_t codepoint = iter.Next();
35 if (codepoint == U'.') {
36 const size_t current_position = iter.Position();
37 if (!iter.HasNext() || text::IsWhitespace(iter.Next())) {
38 return comment.substr(0, current_position);
39 }
40 }
41 }
42 return comment;
43}
44
Adam Lesinski09f4d702017-08-08 10:39:55 -070045struct AnnotationRule {
46 enum : uint32_t {
47 kDeprecated = 0x01,
48 kSystemApi = 0x02,
49 kTestApi = 0x04,
50 };
51
52 StringPiece doc_str;
53 uint32_t bit_mask;
54 StringPiece annotation;
55};
56
57static std::array<AnnotationRule, 2> sAnnotationRules = {{
58 {"@SystemApi", AnnotationRule::kSystemApi, "@android.annotation.SystemApi"},
59 {"@TestApi", AnnotationRule::kTestApi, "@android.annotation.TestApi"},
60}};
61
62void AnnotationProcessor::AppendCommentLine(std::string comment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 static const std::string sDeprecated = "@deprecated";
Adam Lesinskica5638f2015-10-21 14:42:43 -070064
Adam Lesinski09f4d702017-08-08 10:39:55 -070065 // Treat deprecated specially, since we don't remove it from the source comment.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 if (comment.find(sDeprecated) != std::string::npos) {
Adam Lesinski09f4d702017-08-08 10:39:55 -070067 annotation_bit_mask_ |= AnnotationRule::kDeprecated;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070069
Adam Lesinski09f4d702017-08-08 10:39:55 -070070 for (const AnnotationRule& rule : sAnnotationRules) {
71 std::string::size_type idx = comment.find(rule.doc_str.data());
72 if (idx != std::string::npos) {
73 annotation_bit_mask_ |= rule.bit_mask;
74 comment.erase(comment.begin() + idx, comment.begin() + idx + rule.doc_str.size());
75 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 }
Adam Lesinski626b3db2016-04-07 13:24:59 -070077
Adam Lesinski09f4d702017-08-08 10:39:55 -070078 // Check if after removal of annotations the line is empty.
79 const StringPiece trimmed = util::TrimWhitespace(comment);
80 if (trimmed.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 return;
82 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070083
Adam Lesinski09f4d702017-08-08 10:39:55 -070084 // If there was trimming to do, copy the string.
85 if (trimmed.size() != comment.size()) {
86 comment = trimmed.to_string();
87 }
88
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 if (!has_comments_) {
90 has_comments_ = true;
91 comment_ << "/**";
92 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 comment_ << "\n * " << std::move(comment);
Adam Lesinskica5638f2015-10-21 14:42:43 -070094}
95
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096void AnnotationProcessor::AppendComment(const StringPiece& comment) {
97 // We need to process line by line to clean-up whitespace and append prefixes.
98 for (StringPiece line : util::Tokenize(comment, '\n')) {
99 line = util::TrimWhitespace(line);
100 if (!line.empty()) {
Adam Lesinski09f4d702017-08-08 10:39:55 -0700101 AppendCommentLine(line.to_string());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700102 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700104}
105
Adam Lesinski09f4d702017-08-08 10:39:55 -0700106void AnnotationProcessor::AppendNewLine() {
107 if (has_comments_) {
108 comment_ << "\n *";
109 }
110}
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111
Adam Lesinski09f4d702017-08-08 10:39:55 -0700112void AnnotationProcessor::WriteToStream(const StringPiece& prefix, std::ostream* out) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 if (has_comments_) {
114 std::string result = comment_.str();
115 for (StringPiece line : util::Tokenize(result, '\n')) {
116 *out << prefix << line << "\n";
117 }
118 *out << prefix << " */"
119 << "\n";
120 }
121
Adam Lesinski09f4d702017-08-08 10:39:55 -0700122 if (annotation_bit_mask_ & AnnotationRule::kDeprecated) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 *out << prefix << "@Deprecated\n";
124 }
125
Adam Lesinski09f4d702017-08-08 10:39:55 -0700126 for (const AnnotationRule& rule : sAnnotationRules) {
127 if (annotation_bit_mask_ & rule.bit_mask) {
128 *out << prefix << rule.annotation << "\n";
129 }
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