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"
18
19 #include "androidfw/StringPiece.h"
20
21 using android::StringPiece;
22
23 namespace aapt {
24
WriteToStream(const StringPiece & prefix,bool final,std::ostream * out) const25 void ClassMember::WriteToStream(const StringPiece& prefix, bool final, std::ostream* out) const {
26 processor_.WriteToStream(out, prefix);
27 }
28
AppendStatement(const StringPiece & statement)29 void MethodDefinition::AppendStatement(const StringPiece& statement) {
30 statements_.push_back(statement.to_string());
31 }
32
WriteToStream(const StringPiece & prefix,bool final,std::ostream * out) const33 void 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
empty() const42 bool ClassDefinition::empty() const {
43 for (const std::unique_ptr<ClassMember>& member : members_) {
44 if (!member->empty()) {
45 return false;
46 }
47 }
48 return true;
49 }
50
WriteToStream(const StringPiece & prefix,bool final,std::ostream * out) const51 void ClassDefinition::WriteToStream(const StringPiece& prefix, bool final,
52 std::ostream* out) const {
53 if (members_.empty() && !create_if_empty_) {
54 return;
55 }
56
57 ClassMember::WriteToStream(prefix, final, out);
58
59 *out << prefix << "public ";
60 if (qualifier_ == ClassQualifier::kStatic) {
61 *out << "static ";
62 }
63 *out << "final class " << name_ << " {\n";
64
65 std::string new_prefix = prefix.to_string();
66 new_prefix.append(kIndent);
67
68 for (const std::unique_ptr<ClassMember>& member : members_) {
69 member->WriteToStream(new_prefix, final, out);
70 *out << "\n";
71 }
72
73 *out << prefix << "}";
74 }
75
76 constexpr static const char* sWarningHeader =
77 "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
78 " *\n"
79 " * This class was automatically generated by the\n"
80 " * aapt tool from the resource data it found. It\n"
81 " * should not be modified by hand.\n"
82 " */\n\n";
83
WriteJavaFile(const ClassDefinition * def,const StringPiece & package,bool final,std::ostream * out)84 bool ClassDefinition::WriteJavaFile(const ClassDefinition* def,
85 const StringPiece& package, bool final,
86 std::ostream* out) {
87 *out << sWarningHeader << "package " << package << ";\n\n";
88 def->WriteToStream("", final, out);
89 return bool(*out);
90 }
91
92 } // namespace aapt
93