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 | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 21 | using ::aapt::text::Printer; |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 22 | using ::android::StringPiece; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 23 | |
| 24 | namespace aapt { |
| 25 | |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame^] | 26 | void ClassMember::Print(bool /*final*/, Printer* printer, bool strip_api_annotations) const { |
| 27 | processor_.Print(printer, strip_api_annotations); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void MethodDefinition::AppendStatement(const StringPiece& statement) { |
| 31 | statements_.push_back(statement.to_string()); |
| 32 | } |
| 33 | |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame^] | 34 | void MethodDefinition::Print(bool final, Printer* printer, bool) const { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 35 | printer->Print(signature_).Println(" {"); |
| 36 | printer->Indent(); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 37 | for (const auto& statement : statements_) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 38 | printer->Println(statement); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 39 | } |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 40 | printer->Undent(); |
| 41 | printer->Print("}"); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 44 | ClassDefinition::Result ClassDefinition::AddMember(std::unique_ptr<ClassMember> member) { |
| 45 | Result result = Result::kAdded; |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 46 | auto iter = indexed_members_.find(member->GetName()); |
| 47 | if (iter != indexed_members_.end()) { |
Adam Lesinski | a4fb17b | 2018-01-16 17:05:10 -0800 | [diff] [blame] | 48 | // Overwrite the entry. Be careful, as the key in indexed_members_ is actually memory owned |
| 49 | // by the value at ordered_members_[index]. Since overwriting a value for a key doesn't replace |
| 50 | // the key (the initial key inserted into the unordered_map is kept), we must erase and then |
| 51 | // insert a new key, whose memory is being kept around. We do all this to avoid using more |
| 52 | // memory for each key. |
| 53 | size_t index = iter->second; |
| 54 | |
| 55 | // Erase the key + value from the map. |
| 56 | indexed_members_.erase(iter); |
| 57 | |
| 58 | // Now clear the memory that was backing the key (now erased). |
| 59 | ordered_members_[index].reset(); |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 60 | result = Result::kOverridden; |
| 61 | } |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 62 | |
| 63 | indexed_members_[member->GetName()] = ordered_members_.size(); |
| 64 | ordered_members_.push_back(std::move(member)); |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 65 | return result; |
| 66 | } |
| 67 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 68 | bool ClassDefinition::empty() const { |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 69 | for (const std::unique_ptr<ClassMember>& member : ordered_members_) { |
| 70 | if (member != nullptr && !member->empty()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 71 | return false; |
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 | } |
| 74 | return true; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame^] | 77 | void ClassDefinition::Print(bool final, Printer* printer, bool strip_api_annotations) const { |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 78 | if (empty() && !create_if_empty_) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | return; |
| 80 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 81 | |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame^] | 82 | ClassMember::Print(final, printer, strip_api_annotations); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 83 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 84 | printer->Print("public "); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 85 | if (qualifier_ == ClassQualifier::kStatic) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 86 | printer->Print("static "); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | } |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 88 | printer->Print("final class ").Print(name_).Println(" {"); |
| 89 | printer->Indent(); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 90 | |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 91 | for (const std::unique_ptr<ClassMember>& member : ordered_members_) { |
| 92 | // There can be nullptr members when a member is added to the ClassDefinition |
| 93 | // and takes precedence over a previous member with the same name. The overridden member is |
| 94 | // set to nullptr. |
| 95 | if (member != nullptr) { |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame^] | 96 | member->Print(final, printer, strip_api_annotations); |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 97 | printer->Println(); |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 98 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 99 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 100 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 101 | printer->Undent(); |
| 102 | printer->Print("}"); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | constexpr static const char* sWarningHeader = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 106 | "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n" |
| 107 | " *\n" |
| 108 | " * This class was automatically generated by the\n" |
| 109 | " * aapt tool from the resource data it found. It\n" |
| 110 | " * should not be modified by hand.\n" |
| 111 | " */\n\n"; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 112 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 113 | void ClassDefinition::WriteJavaFile(const ClassDefinition* def, const StringPiece& package, |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame^] | 114 | bool final, bool strip_api_annotations, io::OutputStream* out) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 115 | Printer printer(out); |
| 116 | printer.Print(sWarningHeader).Print("package ").Print(package).Println(";"); |
| 117 | printer.Println(); |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame^] | 118 | def->Print(final, &printer, strip_api_annotations); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 121 | } // namespace aapt |