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 | 18dc03a | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 21 | #include "text/Unicode.h" |
| 22 | #include "text/Utf8Iterator.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 23 | #include "util/Util.h" |
| 24 | |
Adam Lesinski | 18dc03a | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 25 | using ::aapt::text::Utf8Iterator; |
| 26 | using ::android::StringPiece; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 27 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 28 | namespace aapt { |
| 29 | |
Adam Lesinski | 18dc03a | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 30 | StringPiece AnnotationProcessor::ExtractFirstSentence(const StringPiece& comment) { |
| 31 | Utf8Iterator iter(comment); |
| 32 | while (iter.HasNext()) { |
| 33 | const char32_t codepoint = iter.Next(); |
| 34 | if (codepoint == U'.') { |
| 35 | const size_t current_position = iter.Position(); |
| 36 | if (!iter.HasNext() || text::IsWhitespace(iter.Next())) { |
| 37 | return comment.substr(0, current_position); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | return comment; |
| 42 | } |
| 43 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 44 | void AnnotationProcessor::AppendCommentLine(std::string& comment) { |
| 45 | static const std::string sDeprecated = "@deprecated"; |
| 46 | static const std::string sSystemApi = "@SystemApi"; |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 47 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | if (comment.find(sDeprecated) != std::string::npos) { |
| 49 | annotation_bit_mask_ |= kDeprecated; |
| 50 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 51 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 52 | std::string::size_type idx = comment.find(sSystemApi); |
| 53 | if (idx != std::string::npos) { |
| 54 | annotation_bit_mask_ |= kSystemApi; |
| 55 | comment.erase(comment.begin() + idx, |
| 56 | comment.begin() + idx + sSystemApi.size()); |
| 57 | } |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 58 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | if (util::TrimWhitespace(comment).empty()) { |
| 60 | return; |
| 61 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 62 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | if (!has_comments_) { |
| 64 | has_comments_ = true; |
| 65 | comment_ << "/**"; |
| 66 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 67 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 68 | comment_ << "\n * " << std::move(comment); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 71 | void AnnotationProcessor::AppendComment(const StringPiece& comment) { |
| 72 | // We need to process line by line to clean-up whitespace and append prefixes. |
| 73 | for (StringPiece line : util::Tokenize(comment, '\n')) { |
| 74 | line = util::TrimWhitespace(line); |
| 75 | if (!line.empty()) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 76 | std::string lineCopy = line.to_string(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 77 | AppendCommentLine(lineCopy); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 78 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 82 | void AnnotationProcessor::AppendNewLine() { comment_ << "\n *"; } |
| 83 | |
| 84 | void AnnotationProcessor::WriteToStream(std::ostream* out, |
| 85 | const StringPiece& prefix) const { |
| 86 | if (has_comments_) { |
| 87 | std::string result = comment_.str(); |
| 88 | for (StringPiece line : util::Tokenize(result, '\n')) { |
| 89 | *out << prefix << line << "\n"; |
| 90 | } |
| 91 | *out << prefix << " */" |
| 92 | << "\n"; |
| 93 | } |
| 94 | |
| 95 | if (annotation_bit_mask_ & kDeprecated) { |
| 96 | *out << prefix << "@Deprecated\n"; |
| 97 | } |
| 98 | |
| 99 | if (annotation_bit_mask_ & kSystemApi) { |
| 100 | *out << prefix << "@android.annotation.SystemApi\n"; |
| 101 | } |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 104 | } // namespace aapt |