blob: 6ad0dd68cb6ae84c92068ad38bc532c45b2c76be [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;
44 auto iter = members_.find(member);
45 if (iter != members_.end()) {
46 members_.erase(iter);
47 result = Result::kOverridden;
48 }
49 members_.insert(std::move(member));
50 return result;
51}
52
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070053bool ClassDefinition::empty() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 for (const std::unique_ptr<ClassMember>& member : members_) {
55 if (!member->empty()) {
56 return false;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070057 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 }
59 return true;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070060}
61
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062void ClassDefinition::WriteToStream(const StringPiece& prefix, bool final,
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070063 std::ostream* out) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 if (members_.empty() && !create_if_empty_) {
65 return;
66 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070067
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 ClassMember::WriteToStream(prefix, final, out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070069
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 *out << prefix << "public ";
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080071 if (qualifier_ == ClassQualifier::kStatic) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 *out << "static ";
73 }
74 *out << "final class " << name_ << " {\n";
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070075
Adam Lesinskid5083f62017-01-16 15:07:21 -080076 std::string new_prefix = prefix.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 new_prefix.append(kIndent);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070078
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 for (const std::unique_ptr<ClassMember>& member : members_) {
80 member->WriteToStream(new_prefix, final, out);
81 *out << "\n";
82 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070083
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 *out << prefix << "}";
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070085}
86
87constexpr static const char* sWarningHeader =
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
89 " *\n"
90 " * This class was automatically generated by the\n"
91 " * aapt tool from the resource data it found. It\n"
92 " * should not be modified by hand.\n"
93 " */\n\n";
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070094
Adam Lesinski09f4d702017-08-08 10:39:55 -070095bool ClassDefinition::WriteJavaFile(const ClassDefinition* def, const StringPiece& package,
96 bool final, std::ostream* out) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 *out << sWarningHeader << "package " << package << ";\n\n";
98 def->WriteToStream("", final, out);
99 return bool(*out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700100}
101
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102} // namespace aapt