Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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/ClassDefinition.h" |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 19 | #include "androidfw/StringPiece.h" |
| 20 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 21 | using ::android::StringPiece; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 22 | |
| 23 | namespace aapt { |
| 24 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 25 | void ClassMember::WriteToStream(const StringPiece& prefix, bool final, std::ostream* out) const { |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 26 | processor_.WriteToStream(prefix, out); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void MethodDefinition::AppendStatement(const StringPiece& statement) { |
| 30 | statements_.push_back(statement.to_string()); |
| 31 | } |
| 32 | |
| 33 | void MethodDefinition::WriteToStream(const StringPiece& prefix, bool final, |
| 34 | std::ostream* out) const { |
| 35 | *out << prefix << signature_ << " {\n"; |
| 36 | for (const auto& statement : statements_) { |
| 37 | *out << prefix << " " << statement << "\n"; |
| 38 | } |
| 39 | *out << prefix << "}"; |
| 40 | } |
| 41 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 42 | ClassDefinition::Result ClassDefinition::AddMember(std::unique_ptr<ClassMember> member) { |
| 43 | Result result = Result::kAdded; |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 44 | auto iter = indexed_members_.find(member->GetName()); |
| 45 | if (iter != indexed_members_.end()) { |
| 46 | // Overwrite the entry. |
| 47 | ordered_members_[iter->second].reset(); |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 48 | result = Result::kOverridden; |
| 49 | } |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 50 | |
| 51 | indexed_members_[member->GetName()] = ordered_members_.size(); |
| 52 | ordered_members_.push_back(std::move(member)); |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 53 | return result; |
| 54 | } |
| 55 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 56 | bool ClassDefinition::empty() const { |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 57 | for (const std::unique_ptr<ClassMember>& member : ordered_members_) { |
| 58 | if (member != nullptr && !member->empty()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | return false; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 60 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 61 | } |
| 62 | return true; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | void ClassDefinition::WriteToStream(const StringPiece& prefix, bool final, |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 66 | std::ostream* out) const { |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 67 | if (empty() && !create_if_empty_) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 68 | return; |
| 69 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 70 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 71 | ClassMember::WriteToStream(prefix, final, out); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 72 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 73 | *out << prefix << "public "; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 74 | if (qualifier_ == ClassQualifier::kStatic) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | *out << "static "; |
| 76 | } |
| 77 | *out << "final class " << name_ << " {\n"; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 78 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 79 | std::string new_prefix = prefix.to_string(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | new_prefix.append(kIndent); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 81 | |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 82 | for (const std::unique_ptr<ClassMember>& member : ordered_members_) { |
| 83 | // There can be nullptr members when a member is added to the ClassDefinition |
| 84 | // and takes precedence over a previous member with the same name. The overridden member is |
| 85 | // set to nullptr. |
| 86 | if (member != nullptr) { |
| 87 | member->WriteToStream(new_prefix, final, out); |
| 88 | *out << "\n"; |
| 89 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 91 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 92 | *out << prefix << "}"; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | constexpr static const char* sWarningHeader = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 96 | "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n" |
| 97 | " *\n" |
| 98 | " * This class was automatically generated by the\n" |
| 99 | " * aapt tool from the resource data it found. It\n" |
| 100 | " * should not be modified by hand.\n" |
| 101 | " */\n\n"; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 102 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 103 | bool ClassDefinition::WriteJavaFile(const ClassDefinition* def, const StringPiece& package, |
| 104 | bool final, std::ostream* out) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 105 | *out << sWarningHeader << "package " << package << ";\n\n"; |
| 106 | def->WriteToStream("", final, out); |
| 107 | return bool(*out); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | } // namespace aapt |