blob: 3163497f0da67fff322555d7e399c06602ad325f [file] [log] [blame]
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -07001/*
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 Lesinski6cbfb1d2016-03-31 13:33:02 -070018
Adam Lesinskid5083f62017-01-16 15:07:21 -080019#include "androidfw/StringPiece.h"
20
Adam Lesinskia693c4a2017-11-09 11:29:39 -080021using ::aapt::text::Printer;
Adam Lesinski09f4d702017-08-08 10:39:55 -070022using ::android::StringPiece;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070023
24namespace aapt {
25
Makoto Onukide6e6f22020-06-22 10:17:02 -070026void ClassMember::Print(bool /*final*/, Printer* printer, bool strip_api_annotations) const {
27 processor_.Print(printer, strip_api_annotations);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080028}
29
30void MethodDefinition::AppendStatement(const StringPiece& statement) {
31 statements_.push_back(statement.to_string());
32}
33
Makoto Onukide6e6f22020-06-22 10:17:02 -070034void MethodDefinition::Print(bool final, Printer* printer, bool) const {
Adam Lesinskia693c4a2017-11-09 11:29:39 -080035 printer->Print(signature_).Println(" {");
36 printer->Indent();
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080037 for (const auto& statement : statements_) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -080038 printer->Println(statement);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080039 }
Adam Lesinskia693c4a2017-11-09 11:29:39 -080040 printer->Undent();
41 printer->Print("}");
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080042}
43
Adam Lesinskif852dd02017-08-18 19:49:58 -070044ClassDefinition::Result ClassDefinition::AddMember(std::unique_ptr<ClassMember> member) {
45 Result result = Result::kAdded;
Adam Lesinski761d4342017-09-29 11:15:17 -070046 auto iter = indexed_members_.find(member->GetName());
47 if (iter != indexed_members_.end()) {
Adam Lesinskia4fb17b2018-01-16 17:05:10 -080048 // 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 Lesinskif852dd02017-08-18 19:49:58 -070060 result = Result::kOverridden;
61 }
Adam Lesinski761d4342017-09-29 11:15:17 -070062
63 indexed_members_[member->GetName()] = ordered_members_.size();
64 ordered_members_.push_back(std::move(member));
Adam Lesinskif852dd02017-08-18 19:49:58 -070065 return result;
66}
67
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070068bool ClassDefinition::empty() const {
Adam Lesinski761d4342017-09-29 11:15:17 -070069 for (const std::unique_ptr<ClassMember>& member : ordered_members_) {
70 if (member != nullptr && !member->empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 return false;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070072 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 }
74 return true;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070075}
76
Makoto Onukide6e6f22020-06-22 10:17:02 -070077void ClassDefinition::Print(bool final, Printer* printer, bool strip_api_annotations) const {
Adam Lesinski761d4342017-09-29 11:15:17 -070078 if (empty() && !create_if_empty_) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 return;
80 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070081
Makoto Onukide6e6f22020-06-22 10:17:02 -070082 ClassMember::Print(final, printer, strip_api_annotations);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070083
Adam Lesinskia693c4a2017-11-09 11:29:39 -080084 printer->Print("public ");
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080085 if (qualifier_ == ClassQualifier::kStatic) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -080086 printer->Print("static ");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 }
Adam Lesinskia693c4a2017-11-09 11:29:39 -080088 printer->Print("final class ").Print(name_).Println(" {");
89 printer->Indent();
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070090
Adam Lesinski761d4342017-09-29 11:15:17 -070091 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 Onukide6e6f22020-06-22 10:17:02 -070096 member->Print(final, printer, strip_api_annotations);
Adam Lesinskia693c4a2017-11-09 11:29:39 -080097 printer->Println();
Adam Lesinski761d4342017-09-29 11:15:17 -070098 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700100
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800101 printer->Undent();
102 printer->Print("}");
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700103}
104
105constexpr static const char* sWarningHeader =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 "/* 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 Lesinski6cbfb1d2016-03-31 13:33:02 -0700112
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800113void ClassDefinition::WriteJavaFile(const ClassDefinition* def, const StringPiece& package,
Makoto Onukide6e6f22020-06-22 10:17:02 -0700114 bool final, bool strip_api_annotations, io::OutputStream* out) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800115 Printer printer(out);
116 printer.Print(sWarningHeader).Print("package ").Print(package).Println(";");
117 printer.Println();
Makoto Onukide6e6f22020-06-22 10:17:02 -0700118 def->Print(final, &printer, strip_api_annotations);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700119}
120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121} // namespace aapt