AnnotationProcessor.cpp revision ca5638fd85098c3d0a699492751043545f75553a
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#include "util/Util.h"
19
20#include <algorithm>
21
22namespace aapt {
23
24void AnnotationProcessor::appendCommentLine(const StringPiece16& line) {
25    static const std::string sDeprecated = "@deprecated";
26    static const std::string sSystemApi = "@SystemApi";
27
28    if (line.empty()) {
29        return;
30    }
31
32    std::string comment = util::utf16ToUtf8(line);
33
34    if (comment.find(sDeprecated) != std::string::npos && !mDeprecated) {
35        mDeprecated = true;
36        if (!mAnnotations.empty()) {
37            mAnnotations += "\n";
38        }
39        mAnnotations += mPrefix;
40        mAnnotations += "@Deprecated";
41    }
42
43    if (comment.find(sSystemApi) != std::string::npos && !mSystemApi) {
44        mSystemApi = true;
45        if (!mAnnotations.empty()) {
46            mAnnotations += "\n";
47        }
48        mAnnotations += mPrefix;
49        mAnnotations += "@android.annotations.SystemApi";
50    }
51
52    if (mComment.empty()) {
53        mComment += mPrefix;
54        mComment += "/**";
55    }
56
57    mComment += "\n";
58    mComment += mPrefix;
59    mComment += " * ";
60    mComment += std::move(comment);
61}
62
63void AnnotationProcessor::appendComment(const StringPiece16& comment) {
64    // We need to process line by line to clean-up whitespace and append prefixes.
65    for (StringPiece16 line : util::tokenize(comment, u'\n')) {
66        appendCommentLine(util::trimWhitespace(line));
67    }
68}
69
70std::string AnnotationProcessor::buildComment() {
71    mComment += "\n";
72    mComment += mPrefix;
73    mComment += " */";
74    return std::move(mComment);
75}
76
77std::string AnnotationProcessor::buildAnnotations() {
78    return std::move(mAnnotations);
79}
80
81} // namespace aapt
82