| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 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" |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
| 20 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 21 | #include "util/Util.h" |
| 22 | |
| Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 23 | using android::StringPiece; |
| 24 | |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 25 | namespace aapt { |
| 26 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 27 | void AnnotationProcessor::AppendCommentLine(std::string& comment) { |
| 28 | static const std::string sDeprecated = "@deprecated"; |
| 29 | static const std::string sSystemApi = "@SystemApi"; |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 30 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 31 | if (comment.find(sDeprecated) != std::string::npos) { |
| 32 | annotation_bit_mask_ |= kDeprecated; |
| 33 | } |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 34 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 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 | } |
| Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 41 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 42 | if (util::TrimWhitespace(comment).empty()) { |
| 43 | return; |
| 44 | } |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 45 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 46 | if (!has_comments_) { |
| 47 | has_comments_ = true; |
| 48 | comment_ << "/**"; |
| 49 | } |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 50 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 51 | comment_ << "\n * " << std::move(comment); |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 54 | void 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 Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 59 | std::string lineCopy = line.to_string(); |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 60 | AppendCommentLine(lineCopy); |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 61 | } |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | } |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | void AnnotationProcessor::AppendNewLine() { comment_ << "\n *"; } |
| 66 | |
| 67 | void 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 Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | } // namespace aapt |