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 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 26 | void ClassMember::Print(bool /*final*/, Printer* printer) const { |
| 27 | processor_.Print(printer); |
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 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 34 | void MethodDefinition::Print(bool final, Printer* printer) const { |
| 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()) { |
| 48 | // Overwrite the entry. |
| 49 | ordered_members_[iter->second].reset(); |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 50 | result = Result::kOverridden; |
| 51 | } |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 52 | |
| 53 | indexed_members_[member->GetName()] = ordered_members_.size(); |
| 54 | ordered_members_.push_back(std::move(member)); |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 55 | return result; |
| 56 | } |
| 57 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 58 | bool ClassDefinition::empty() const { |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 59 | for (const std::unique_ptr<ClassMember>& member : ordered_members_) { |
| 60 | if (member != nullptr && !member->empty()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 61 | return false; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 62 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | } |
| 64 | return true; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 67 | void ClassDefinition::Print(bool final, Printer* printer) const { |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 68 | if (empty() && !create_if_empty_) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 69 | return; |
| 70 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 71 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 72 | ClassMember::Print(final, printer); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 73 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 74 | printer->Print("public "); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 75 | if (qualifier_ == ClassQualifier::kStatic) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 76 | printer->Print("static "); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 77 | } |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 78 | printer->Print("final class ").Print(name_).Println(" {"); |
| 79 | printer->Indent(); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 80 | |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 81 | for (const std::unique_ptr<ClassMember>& member : ordered_members_) { |
| 82 | // There can be nullptr members when a member is added to the ClassDefinition |
| 83 | // and takes precedence over a previous member with the same name. The overridden member is |
| 84 | // set to nullptr. |
| 85 | if (member != nullptr) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 86 | member->Print(final, printer); |
| 87 | printer->Println(); |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 88 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 89 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 90 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 91 | printer->Undent(); |
| 92 | printer->Print("}"); |
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 | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 103 | void ClassDefinition::WriteJavaFile(const ClassDefinition* def, const StringPiece& package, |
| 104 | bool final, io::OutputStream* out) { |
| 105 | Printer printer(out); |
| 106 | printer.Print(sWarningHeader).Print("package ").Print(package).Println(";"); |
| 107 | printer.Println(); |
| 108 | def->Print(final, &printer); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 111 | } // namespace aapt |