• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
AddMember(std::unique_ptr<ClassMember> member)42 ClassDefinition::Result ClassDefinition::AddMember(std::unique_ptr<ClassMember> member) {
43   Result result = Result::kAdded;
44   auto iter = indexed_members_.find(member->GetName());
45   if (iter != indexed_members_.end()) {
46     // Overwrite the entry.
47     ordered_members_[iter->second].reset();
48     result = Result::kOverridden;
49   }
50 
51   indexed_members_[member->GetName()] = ordered_members_.size();
52   ordered_members_.push_back(std::move(member));
53   return result;
54 }
55 
empty() const56 bool ClassDefinition::empty() const {
57   for (const std::unique_ptr<ClassMember>& member : ordered_members_) {
58     if (member != nullptr && !member->empty()) {
59       return false;
60     }
61   }
62   return true;
63 }
64 
WriteToStream(const StringPiece & prefix,bool final,std::ostream * out) const65 void ClassDefinition::WriteToStream(const StringPiece& prefix, bool final,
66                                     std::ostream* out) const {
67   if (empty() && !create_if_empty_) {
68     return;
69   }
70 
71   ClassMember::WriteToStream(prefix, final, out);
72 
73   *out << prefix << "public ";
74   if (qualifier_ == ClassQualifier::kStatic) {
75     *out << "static ";
76   }
77   *out << "final class " << name_ << " {\n";
78 
79   std::string new_prefix = prefix.to_string();
80   new_prefix.append(kIndent);
81 
82   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     }
90   }
91 
92   *out << prefix << "}";
93 }
94 
95 constexpr static const char* sWarningHeader =
96     "/* 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";
102 
WriteJavaFile(const ClassDefinition * def,const StringPiece & package,bool final,std::ostream * out)103 bool ClassDefinition::WriteJavaFile(const ClassDefinition* def,
104                                     const StringPiece& package, bool final,
105                                     std::ostream* out) {
106   *out << sWarningHeader << "package " << package << ";\n\n";
107   def->WriteToStream("", final, out);
108   return bool(*out);
109 }
110 
111 }  // namespace aapt
112