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> |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 20 | #include <array> |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 21 | |
Adam Lesinski | e967d3f | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 22 | #include "text/Unicode.h" |
| 23 | #include "text/Utf8Iterator.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 24 | #include "util/Util.h" |
| 25 | |
Adam Lesinski | e967d3f | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 26 | using ::aapt::text::Utf8Iterator; |
| 27 | using ::android::StringPiece; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 28 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 29 | namespace aapt { |
| 30 | |
Adam Lesinski | e967d3f | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 31 | StringPiece AnnotationProcessor::ExtractFirstSentence(const StringPiece& comment) { |
| 32 | Utf8Iterator iter(comment); |
| 33 | while (iter.HasNext()) { |
| 34 | const char32_t codepoint = iter.Next(); |
| 35 | if (codepoint == U'.') { |
| 36 | const size_t current_position = iter.Position(); |
| 37 | if (!iter.HasNext() || text::IsWhitespace(iter.Next())) { |
| 38 | return comment.substr(0, current_position); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | return comment; |
| 43 | } |
| 44 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 45 | struct AnnotationRule { |
| 46 | enum : uint32_t { |
| 47 | kDeprecated = 0x01, |
| 48 | kSystemApi = 0x02, |
| 49 | kTestApi = 0x04, |
| 50 | }; |
| 51 | |
| 52 | StringPiece doc_str; |
| 53 | uint32_t bit_mask; |
| 54 | StringPiece annotation; |
| 55 | }; |
| 56 | |
| 57 | static std::array<AnnotationRule, 2> sAnnotationRules = {{ |
| 58 | {"@SystemApi", AnnotationRule::kSystemApi, "@android.annotation.SystemApi"}, |
| 59 | {"@TestApi", AnnotationRule::kTestApi, "@android.annotation.TestApi"}, |
| 60 | }}; |
| 61 | |
| 62 | void AnnotationProcessor::AppendCommentLine(std::string comment) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | static const std::string sDeprecated = "@deprecated"; |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 64 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 65 | // Treat deprecated specially, since we don't remove it from the source comment. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | if (comment.find(sDeprecated) != std::string::npos) { |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 67 | annotation_bit_mask_ |= AnnotationRule::kDeprecated; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 68 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 69 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 70 | for (const AnnotationRule& rule : sAnnotationRules) { |
| 71 | std::string::size_type idx = comment.find(rule.doc_str.data()); |
| 72 | if (idx != std::string::npos) { |
| 73 | annotation_bit_mask_ |= rule.bit_mask; |
| 74 | comment.erase(comment.begin() + idx, comment.begin() + idx + rule.doc_str.size()); |
| 75 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 76 | } |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 77 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 78 | // Check if after removal of annotations the line is empty. |
| 79 | const StringPiece trimmed = util::TrimWhitespace(comment); |
| 80 | if (trimmed.empty()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | return; |
| 82 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 83 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 84 | // If there was trimming to do, copy the string. |
| 85 | if (trimmed.size() != comment.size()) { |
| 86 | comment = trimmed.to_string(); |
| 87 | } |
| 88 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 89 | if (!has_comments_) { |
| 90 | has_comments_ = true; |
| 91 | comment_ << "/**"; |
| 92 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 93 | comment_ << "\n * " << std::move(comment); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 96 | void AnnotationProcessor::AppendComment(const StringPiece& comment) { |
| 97 | // We need to process line by line to clean-up whitespace and append prefixes. |
| 98 | for (StringPiece line : util::Tokenize(comment, '\n')) { |
| 99 | line = util::TrimWhitespace(line); |
| 100 | if (!line.empty()) { |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 101 | AppendCommentLine(line.to_string()); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 102 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 103 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 106 | void AnnotationProcessor::AppendNewLine() { |
| 107 | if (has_comments_) { |
| 108 | comment_ << "\n *"; |
| 109 | } |
| 110 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 111 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 112 | void AnnotationProcessor::WriteToStream(const StringPiece& prefix, std::ostream* out) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 113 | if (has_comments_) { |
| 114 | std::string result = comment_.str(); |
| 115 | for (StringPiece line : util::Tokenize(result, '\n')) { |
| 116 | *out << prefix << line << "\n"; |
| 117 | } |
| 118 | *out << prefix << " */" |
| 119 | << "\n"; |
| 120 | } |
| 121 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 122 | if (annotation_bit_mask_ & AnnotationRule::kDeprecated) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 123 | *out << prefix << "@Deprecated\n"; |
| 124 | } |
| 125 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 126 | for (const AnnotationRule& rule : sAnnotationRules) { |
| 127 | if (annotation_bit_mask_ & rule.bit_mask) { |
| 128 | *out << prefix << rule.annotation << "\n"; |
| 129 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 130 | } |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 133 | } // namespace aapt |