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" |
| 18 | #include "util/Util.h" |
| 19 | |
| 20 | #include <algorithm> |
| 21 | |
| 22 | namespace aapt { |
| 23 | |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame^] | 24 | void AnnotationProcessor::appendCommentLine(std::string& comment) { |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 25 | static const std::string sDeprecated = "@deprecated"; |
| 26 | static const std::string sSystemApi = "@SystemApi"; |
| 27 | |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 28 | if (comment.find(sDeprecated) != std::string::npos) { |
| 29 | mAnnotationBitMask |= kDeprecated; |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame^] | 32 | std::string::size_type idx = comment.find(sSystemApi); |
| 33 | if (idx != std::string::npos) { |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 34 | mAnnotationBitMask |= kSystemApi; |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame^] | 35 | comment.erase(comment.begin() + idx, comment.begin() + idx + sSystemApi.size()); |
| 36 | } |
| 37 | |
| 38 | if (util::trimWhitespace(comment).empty()) { |
| 39 | return; |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 42 | if (!mHasComments) { |
| 43 | mHasComments = true; |
| 44 | mComment << "/**"; |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 47 | mComment << "\n * " << std::move(comment); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void AnnotationProcessor::appendComment(const StringPiece16& comment) { |
| 51 | // We need to process line by line to clean-up whitespace and append prefixes. |
| 52 | for (StringPiece16 line : util::tokenize(comment, u'\n')) { |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 53 | line = util::trimWhitespace(line); |
| 54 | if (!line.empty()) { |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame^] | 55 | std::string utf8Line = util::utf16ToUtf8(line); |
| 56 | appendCommentLine(utf8Line); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void AnnotationProcessor::appendComment(const StringPiece& comment) { |
| 62 | for (StringPiece line : util::tokenize(comment, '\n')) { |
| 63 | line = util::trimWhitespace(line); |
| 64 | if (!line.empty()) { |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame^] | 65 | std::string utf8Line = line.toString(); |
| 66 | appendCommentLine(utf8Line); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 67 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 71 | void AnnotationProcessor::appendNewLine() { |
| 72 | mComment << "\n *"; |
| 73 | } |
| 74 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 75 | void AnnotationProcessor::writeToStream(std::ostream* out, const StringPiece& prefix) const { |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 76 | if (mHasComments) { |
| 77 | std::string result = mComment.str(); |
| 78 | for (StringPiece line : util::tokenize<char>(result, '\n')) { |
| 79 | *out << prefix << line << "\n"; |
| 80 | } |
| 81 | *out << prefix << " */" << "\n"; |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 82 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 83 | |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 84 | if (mAnnotationBitMask & kDeprecated) { |
| 85 | *out << prefix << "@Deprecated\n"; |
| 86 | } |
| 87 | |
| 88 | if (mAnnotationBitMask & kSystemApi) { |
| 89 | *out << prefix << "@android.annotation.SystemApi\n"; |
| 90 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | } // namespace aapt |