blob: a0ef00b1ea1ff6edcfad1aff76026f393b6167cd [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 Lesinskice5e56e22016-10-21 17:56:45 -070021#include "util/Util.h"
22
Adam Lesinskid5083f62017-01-16 15:07:21 -080023using android::StringPiece;
24
Adam Lesinskica5638f2015-10-21 14:42:43 -070025namespace aapt {
26
Adam Lesinskice5e56e22016-10-21 17:56:45 -070027void AnnotationProcessor::AppendCommentLine(std::string& comment) {
28 static const std::string sDeprecated = "@deprecated";
29 static const std::string sSystemApi = "@SystemApi";
Adam Lesinskica5638f2015-10-21 14:42:43 -070030
Adam Lesinskice5e56e22016-10-21 17:56:45 -070031 if (comment.find(sDeprecated) != std::string::npos) {
32 annotation_bit_mask_ |= kDeprecated;
33 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070034
Adam Lesinskice5e56e22016-10-21 17:56:45 -070035 std::string::size_type idx = comment.find(sSystemApi);
36 if (idx != std::string::npos) {
37 annotation_bit_mask_ |= kSystemApi;
38 comment.erase(comment.begin() + idx,
39 comment.begin() + idx + sSystemApi.size());
40 }
Adam Lesinski626b3db2016-04-07 13:24:59 -070041
Adam Lesinskice5e56e22016-10-21 17:56:45 -070042 if (util::TrimWhitespace(comment).empty()) {
43 return;
44 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070045
Adam Lesinskice5e56e22016-10-21 17:56:45 -070046 if (!has_comments_) {
47 has_comments_ = true;
48 comment_ << "/**";
49 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070050
Adam Lesinskice5e56e22016-10-21 17:56:45 -070051 comment_ << "\n * " << std::move(comment);
Adam Lesinskica5638f2015-10-21 14:42:43 -070052}
53
Adam Lesinskice5e56e22016-10-21 17:56:45 -070054void AnnotationProcessor::AppendComment(const StringPiece& comment) {
55 // We need to process line by line to clean-up whitespace and append prefixes.
56 for (StringPiece line : util::Tokenize(comment, '\n')) {
57 line = util::TrimWhitespace(line);
58 if (!line.empty()) {
Adam Lesinskid5083f62017-01-16 15:07:21 -080059 std::string lineCopy = line.to_string();
Adam Lesinskice5e56e22016-10-21 17:56:45 -070060 AppendCommentLine(lineCopy);
Adam Lesinskica5638f2015-10-21 14:42:43 -070061 }
Adam Lesinskice5e56e22016-10-21 17:56:45 -070062 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070063}
64
Adam Lesinskice5e56e22016-10-21 17:56:45 -070065void AnnotationProcessor::AppendNewLine() { comment_ << "\n *"; }
66
67void AnnotationProcessor::WriteToStream(std::ostream* out,
68 const StringPiece& prefix) const {
69 if (has_comments_) {
70 std::string result = comment_.str();
71 for (StringPiece line : util::Tokenize(result, '\n')) {
72 *out << prefix << line << "\n";
73 }
74 *out << prefix << " */"
75 << "\n";
76 }
77
78 if (annotation_bit_mask_ & kDeprecated) {
79 *out << prefix << "@Deprecated\n";
80 }
81
82 if (annotation_bit_mask_ & kSystemApi) {
83 *out << prefix << "@android.annotation.SystemApi\n";
84 }
Adam Lesinski76565542016-03-10 21:55:04 -080085}
86
Adam Lesinskice5e56e22016-10-21 17:56:45 -070087} // namespace aapt