blob: 1f83fa098d746b233647f2d9a66699a60d058f09 [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>
20
Adam Lesinski18dc03a2017-07-24 18:19:36 -070021#include "text/Unicode.h"
22#include "text/Utf8Iterator.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070023#include "util/Util.h"
24
Adam Lesinski18dc03a2017-07-24 18:19:36 -070025using ::aapt::text::Utf8Iterator;
26using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080027
Adam Lesinskica5638f2015-10-21 14:42:43 -070028namespace aapt {
29
Adam Lesinski18dc03a2017-07-24 18:19:36 -070030StringPiece AnnotationProcessor::ExtractFirstSentence(const StringPiece& comment) {
31 Utf8Iterator iter(comment);
32 while (iter.HasNext()) {
33 const char32_t codepoint = iter.Next();
34 if (codepoint == U'.') {
35 const size_t current_position = iter.Position();
36 if (!iter.HasNext() || text::IsWhitespace(iter.Next())) {
37 return comment.substr(0, current_position);
38 }
39 }
40 }
41 return comment;
42}
43
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044void AnnotationProcessor::AppendCommentLine(std::string& comment) {
45 static const std::string sDeprecated = "@deprecated";
46 static const std::string sSystemApi = "@SystemApi";
Adam Lesinskica5638f2015-10-21 14:42:43 -070047
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 if (comment.find(sDeprecated) != std::string::npos) {
49 annotation_bit_mask_ |= kDeprecated;
50 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070051
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 std::string::size_type idx = comment.find(sSystemApi);
53 if (idx != std::string::npos) {
54 annotation_bit_mask_ |= kSystemApi;
55 comment.erase(comment.begin() + idx,
56 comment.begin() + idx + sSystemApi.size());
57 }
Adam Lesinski626b3db2016-04-07 13:24:59 -070058
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 if (util::TrimWhitespace(comment).empty()) {
60 return;
61 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070062
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 if (!has_comments_) {
64 has_comments_ = true;
65 comment_ << "/**";
66 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070067
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 comment_ << "\n * " << std::move(comment);
Adam Lesinskica5638f2015-10-21 14:42:43 -070069}
70
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071void AnnotationProcessor::AppendComment(const StringPiece& comment) {
72 // We need to process line by line to clean-up whitespace and append prefixes.
73 for (StringPiece line : util::Tokenize(comment, '\n')) {
74 line = util::TrimWhitespace(line);
75 if (!line.empty()) {
Adam Lesinskid5083f62017-01-16 15:07:21 -080076 std::string lineCopy = line.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 AppendCommentLine(lineCopy);
Adam Lesinskica5638f2015-10-21 14:42:43 -070078 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070080}
81
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082void AnnotationProcessor::AppendNewLine() { comment_ << "\n *"; }
83
84void AnnotationProcessor::WriteToStream(std::ostream* out,
85 const StringPiece& prefix) const {
86 if (has_comments_) {
87 std::string result = comment_.str();
88 for (StringPiece line : util::Tokenize(result, '\n')) {
89 *out << prefix << line << "\n";
90 }
91 *out << prefix << " */"
92 << "\n";
93 }
94
95 if (annotation_bit_mask_ & kDeprecated) {
96 *out << prefix << "@Deprecated\n";
97 }
98
99 if (annotation_bit_mask_ & kSystemApi) {
100 *out << prefix << "@android.annotation.SystemApi\n";
101 }
Adam Lesinski76565542016-03-10 21:55:04 -0800102}
103
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104} // namespace aapt