blob: 0c57e7e06128828a2f9fc273ebb2c83653d0d6db [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 Lesinski09f4d702017-08-08 10:39:55 -070021using ::android::StringPiece;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070022
23namespace aapt {
24
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080025void ClassMember::WriteToStream(const StringPiece& prefix, bool final, std::ostream* out) const {
Adam Lesinski09f4d702017-08-08 10:39:55 -070026 processor_.WriteToStream(prefix, out);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080027}
28
29void MethodDefinition::AppendStatement(const StringPiece& statement) {
30 statements_.push_back(statement.to_string());
31}
32
33void 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 Lesinskif852dd02017-08-18 19:49:58 -070042ClassDefinition::Result ClassDefinition::AddMember(std::unique_ptr<ClassMember> member) {
43 Result result = Result::kAdded;
Adam Lesinski761d4342017-09-29 11:15:17 -070044 auto iter = indexed_members_.find(member->GetName());
45 if (iter != indexed_members_.end()) {
46 // Overwrite the entry.
47 ordered_members_[iter->second].reset();
Adam Lesinskif852dd02017-08-18 19:49:58 -070048 result = Result::kOverridden;
49 }
Adam Lesinski761d4342017-09-29 11:15:17 -070050
51 indexed_members_[member->GetName()] = ordered_members_.size();
52 ordered_members_.push_back(std::move(member));
Adam Lesinskif852dd02017-08-18 19:49:58 -070053 return result;
54}
55
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070056bool ClassDefinition::empty() const {
Adam Lesinski761d4342017-09-29 11:15:17 -070057 for (const std::unique_ptr<ClassMember>& member : ordered_members_) {
58 if (member != nullptr && !member->empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 return false;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070060 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 }
62 return true;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070063}
64
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065void ClassDefinition::WriteToStream(const StringPiece& prefix, bool final,
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070066 std::ostream* out) const {
Adam Lesinski761d4342017-09-29 11:15:17 -070067 if (empty() && !create_if_empty_) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 return;
69 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070070
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 ClassMember::WriteToStream(prefix, final, out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070072
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 *out << prefix << "public ";
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080074 if (qualifier_ == ClassQualifier::kStatic) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 *out << "static ";
76 }
77 *out << "final class " << name_ << " {\n";
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070078
Adam Lesinskid5083f62017-01-16 15:07:21 -080079 std::string new_prefix = prefix.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 new_prefix.append(kIndent);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070081
Adam Lesinski761d4342017-09-29 11:15:17 -070082 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 Lesinskice5e56e2016-10-21 17:56:45 -070090 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070091
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 *out << prefix << "}";
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 Lesinski09f4d702017-08-08 10:39:55 -0700103bool ClassDefinition::WriteJavaFile(const ClassDefinition* def, const StringPiece& package,
104 bool final, std::ostream* out) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 *out << sWarningHeader << "package " << package << ";\n\n";
106 def->WriteToStream("", final, out);
107 return bool(*out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700108}
109
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110} // namespace aapt