blob: 496e92e0bf93de2bf46243cb8a7f2527409ad9c4 [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"
18#include "util/Util.h"
19
20#include <algorithm>
21
22namespace aapt {
23
Adam Lesinski3b4cd942015-10-30 16:31:42 -070024void AnnotationProcessor::appendCommentLine(const std::string& comment) {
Adam Lesinskica5638f2015-10-21 14:42:43 -070025 static const std::string sDeprecated = "@deprecated";
26 static const std::string sSystemApi = "@SystemApi";
27
Adam Lesinskib274e352015-11-06 15:14:35 -080028 if (comment.find(sDeprecated) != std::string::npos) {
29 mAnnotationBitMask |= kDeprecated;
Adam Lesinskica5638f2015-10-21 14:42:43 -070030 }
31
Adam Lesinskib274e352015-11-06 15:14:35 -080032 if (comment.find(sSystemApi) != std::string::npos) {
33 mAnnotationBitMask |= kSystemApi;
Adam Lesinskica5638f2015-10-21 14:42:43 -070034 }
35
Adam Lesinskib274e352015-11-06 15:14:35 -080036 if (!mHasComments) {
37 mHasComments = true;
38 mComment << "/**";
Adam Lesinskica5638f2015-10-21 14:42:43 -070039 }
40
Adam Lesinski76565542016-03-10 21:55:04 -080041 mComment << "\n * " << std::move(comment);
Adam Lesinskica5638f2015-10-21 14:42:43 -070042}
43
44void AnnotationProcessor::appendComment(const StringPiece16& comment) {
45 // We need to process line by line to clean-up whitespace and append prefixes.
46 for (StringPiece16 line : util::tokenize(comment, u'\n')) {
Adam Lesinski3b4cd942015-10-30 16:31:42 -070047 line = util::trimWhitespace(line);
48 if (!line.empty()) {
49 appendCommentLine(util::utf16ToUtf8(line));
50 }
51 }
52}
53
54void AnnotationProcessor::appendComment(const StringPiece& comment) {
55 for (StringPiece line : util::tokenize(comment, '\n')) {
56 line = util::trimWhitespace(line);
57 if (!line.empty()) {
58 appendCommentLine(line.toString());
59 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070060 }
61}
62
Adam Lesinski76565542016-03-10 21:55:04 -080063void AnnotationProcessor::appendNewLine() {
64 mComment << "\n *";
65}
66
Adam Lesinskib274e352015-11-06 15:14:35 -080067void AnnotationProcessor::writeToStream(std::ostream* out, const StringPiece& prefix) {
68 if (mHasComments) {
69 std::string result = mComment.str();
70 for (StringPiece line : util::tokenize<char>(result, '\n')) {
71 *out << prefix << line << "\n";
72 }
73 *out << prefix << " */" << "\n";
Adam Lesinski3b4cd942015-10-30 16:31:42 -070074 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070075
Adam Lesinskib274e352015-11-06 15:14:35 -080076 if (mAnnotationBitMask & kDeprecated) {
77 *out << prefix << "@Deprecated\n";
78 }
79
80 if (mAnnotationBitMask & kSystemApi) {
81 *out << prefix << "@android.annotation.SystemApi\n";
82 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070083}
84
85} // namespace aapt