1/*
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"
18
19#include <algorithm>
20
21#include "util/Util.h"
22
23using android::StringPiece;
24
25namespace aapt {
26
27void AnnotationProcessor::AppendCommentLine(std::string& comment) {
28  static const std::string sDeprecated = "@deprecated";
29  static const std::string sSystemApi = "@SystemApi";
30
31  if (comment.find(sDeprecated) != std::string::npos) {
32    annotation_bit_mask_ |= kDeprecated;
33  }
34
35  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  }
41
42  if (util::TrimWhitespace(comment).empty()) {
43    return;
44  }
45
46  if (!has_comments_) {
47    has_comments_ = true;
48    comment_ << "/**";
49  }
50
51  comment_ << "\n * " << std::move(comment);
52}
53
54void 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()) {
59      std::string lineCopy = line.to_string();
60      AppendCommentLine(lineCopy);
61    }
62  }
63}
64
65void 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  }
85}
86
87}  // namespace aapt
88