blob: b36682d98bfd16f9b7567ca9647602aeb724a15c [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 Lesinskica5638f2015-10-21 14:42:43 -070028 if (comment.find(sDeprecated) != std::string::npos && !mDeprecated) {
29 mDeprecated = true;
30 if (!mAnnotations.empty()) {
31 mAnnotations += "\n";
32 }
33 mAnnotations += mPrefix;
34 mAnnotations += "@Deprecated";
35 }
36
37 if (comment.find(sSystemApi) != std::string::npos && !mSystemApi) {
38 mSystemApi = true;
39 if (!mAnnotations.empty()) {
40 mAnnotations += "\n";
41 }
42 mAnnotations += mPrefix;
43 mAnnotations += "@android.annotations.SystemApi";
44 }
45
46 if (mComment.empty()) {
47 mComment += mPrefix;
48 mComment += "/**";
49 }
50
51 mComment += "\n";
52 mComment += mPrefix;
53 mComment += " * ";
54 mComment += std::move(comment);
55}
56
57void AnnotationProcessor::appendComment(const StringPiece16& comment) {
58 // We need to process line by line to clean-up whitespace and append prefixes.
59 for (StringPiece16 line : util::tokenize(comment, u'\n')) {
Adam Lesinski3b4cd942015-10-30 16:31:42 -070060 line = util::trimWhitespace(line);
61 if (!line.empty()) {
62 appendCommentLine(util::utf16ToUtf8(line));
63 }
64 }
65}
66
67void AnnotationProcessor::appendComment(const StringPiece& comment) {
68 for (StringPiece line : util::tokenize(comment, '\n')) {
69 line = util::trimWhitespace(line);
70 if (!line.empty()) {
71 appendCommentLine(line.toString());
72 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070073 }
74}
75
76std::string AnnotationProcessor::buildComment() {
Adam Lesinski3b4cd942015-10-30 16:31:42 -070077 if (!mComment.empty()) {
78 mComment += "\n";
79 mComment += mPrefix;
80 mComment += " */";
81 }
Adam Lesinskica5638f2015-10-21 14:42:43 -070082 return std::move(mComment);
83}
84
85std::string AnnotationProcessor::buildAnnotations() {
86 return std::move(mAnnotations);
87}
88
89} // namespace aapt