blob: b692ccf7e52d4753e91c6f1d59682867290fc759 [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
Adam Lesinskia693c4a2017-11-09 11:29:39 -080026void ClassMember::Print(bool /*final*/, Printer* printer) const {
27 processor_.Print(printer);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080028}
29
30void MethodDefinition::AppendStatement(const StringPiece& statement) {
31 statements_.push_back(statement.to_string());
32}
33
Adam Lesinskia693c4a2017-11-09 11:29:39 -080034void MethodDefinition::Print(bool final, Printer* printer) const {
35 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()) {
48 // Overwrite the entry.
49 ordered_members_[iter->second].reset();
Adam Lesinskif852dd02017-08-18 19:49:58 -070050 result = Result::kOverridden;
51 }
Adam Lesinski761d4342017-09-29 11:15:17 -070052
53 indexed_members_[member->GetName()] = ordered_members_.size();
54 ordered_members_.push_back(std::move(member));
Adam Lesinskif852dd02017-08-18 19:49:58 -070055 return result;
56}
57
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070058bool ClassDefinition::empty() const {
Adam Lesinski761d4342017-09-29 11:15:17 -070059 for (const std::unique_ptr<ClassMember>& member : ordered_members_) {
60 if (member != nullptr && !member->empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 return false;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070062 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 }
64 return true;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070065}
66
Adam Lesinskia693c4a2017-11-09 11:29:39 -080067void ClassDefinition::Print(bool final, Printer* printer) const {
Adam Lesinski761d4342017-09-29 11:15:17 -070068 if (empty() && !create_if_empty_) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 return;
70 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070071
Adam Lesinskia693c4a2017-11-09 11:29:39 -080072 ClassMember::Print(final, printer);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070073
Adam Lesinskia693c4a2017-11-09 11:29:39 -080074 printer->Print("public ");
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080075 if (qualifier_ == ClassQualifier::kStatic) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -080076 printer->Print("static ");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 }
Adam Lesinskia693c4a2017-11-09 11:29:39 -080078 printer->Print("final class ").Print(name_).Println(" {");
79 printer->Indent();
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070080
Adam Lesinski761d4342017-09-29 11:15:17 -070081 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 Lesinskia693c4a2017-11-09 11:29:39 -080086 member->Print(final, printer);
87 printer->Println();
Adam Lesinski761d4342017-09-29 11:15:17 -070088 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070090
Adam Lesinskia693c4a2017-11-09 11:29:39 -080091 printer->Undent();
92 printer->Print("}");
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070093}
94
95constexpr static const char* sWarningHeader =
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 "/* 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 Lesinski6cbfb1d2016-03-31 13:33:02 -0700102
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800103void 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 Lesinski6cbfb1d2016-03-31 13:33:02 -0700109}
110
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111} // namespace aapt