blob: 2acdadb3c03421f6dbf2ed8622244da30f600bc7 [file] [log] [blame]
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -07001/*
2 * Copyright (C) 2015 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#ifndef AAPT_JAVA_CLASSDEFINITION_H
18#define AAPT_JAVA_CLASSDEFINITION_H
19
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include <string>
Adam Lesinski761d4342017-09-29 11:15:17 -070021#include <unordered_map>
22#include <vector>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070023
24#include "android-base/macros.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080025#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070027#include "Resource.h"
28#include "java/AnnotationProcessor.h"
Adam Lesinskia693c4a2017-11-09 11:29:39 -080029#include "text/Printer.h"
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070030#include "util/Util.h"
31
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070032namespace aapt {
33
34// The number of attributes to emit per line in a Styleable array.
35constexpr static size_t kAttribsPerLine = 4;
36constexpr static const char* kIndent = " ";
37
38class ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 public:
40 virtual ~ClassMember() = default;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070041
Adam Lesinskif852dd02017-08-18 19:49:58 -070042 AnnotationProcessor* GetCommentBuilder() {
43 return &processor_;
44 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070045
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 virtual bool empty() const = 0;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070047
Adam Lesinskif852dd02017-08-18 19:49:58 -070048 virtual const std::string& GetName() const = 0;
49
Adam Lesinskia693c4a2017-11-09 11:29:39 -080050 // Writes the class member to the Printer. Subclasses should derive this method
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080051 // to write their own data. Call this base method from the subclass to write out
52 // this member's comments/annotations.
Makoto Onukide6e6f22020-06-22 10:17:02 -070053 virtual void Print(bool final, text::Printer* printer, bool strip_api_annotations = false) const;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070054
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 AnnotationProcessor processor_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070057};
58
59template <typename T>
60class PrimitiveMember : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 public:
Ryan Mitchell2e9bec12021-03-22 09:31:00 -070062 PrimitiveMember(const android::StringPiece& name, const T& val, bool staged_api = false)
63 : name_(name.to_string()), val_(val), staged_api_(staged_api) {
64 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070065
Adam Lesinskif852dd02017-08-18 19:49:58 -070066 bool empty() const override {
67 return false;
68 }
69
70 const std::string& GetName() const override {
71 return name_;
72 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070073
Ryan Mitchell5855de72021-02-24 14:39:13 -080074 void Print(bool final, text::Printer* printer,
75 bool strip_api_annotations = false) const override {
Adam Lesinskia693c4a2017-11-09 11:29:39 -080076 using std::to_string;
77
Makoto Onukide6e6f22020-06-22 10:17:02 -070078 ClassMember::Print(final, printer, strip_api_annotations);
Adam Lesinskia693c4a2017-11-09 11:29:39 -080079
80 printer->Print("public static ");
Ryan Mitchellca3b4f72021-03-29 14:47:02 -070081 if (final) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -080082 printer->Print("final ");
83 }
Ryan Mitchellca3b4f72021-03-29 14:47:02 -070084 printer->Print("int ").Print(name_);
85 if (staged_api_) {
86 // Prevent references to staged apis from being inline by setting their value out-of-line.
87 printer->Print("; static { ").Print(name_);
88 }
89 printer->Print("=").Print(to_string(val_)).Print(";");
90 if (staged_api_) {
91 printer->Print(" }");
92 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070094
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 private:
Adam Lesinski761d4342017-09-29 11:15:17 -070096 DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
97
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 std::string name_;
99 T val_;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700100 bool staged_api_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700101};
102
Adam Lesinski761d4342017-09-29 11:15:17 -0700103// Specialization for strings so they get the right type and are quoted with "".
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700104template <>
105class PrimitiveMember<std::string> : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 public:
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700107 PrimitiveMember(const android::StringPiece& name, const std::string& val, bool staged_api = false)
108 : name_(name.to_string()), val_(val) {
109 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700110
Adam Lesinskif852dd02017-08-18 19:49:58 -0700111 bool empty() const override {
112 return false;
113 }
114
115 const std::string& GetName() const override {
116 return name_;
117 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700118
Makoto Onukide6e6f22020-06-22 10:17:02 -0700119 void Print(bool final, text::Printer* printer, bool strip_api_annotations = false)
120 const override {
121 ClassMember::Print(final, printer, strip_api_annotations);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700122
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800123 printer->Print("public static ");
124 if (final) {
125 printer->Print("final ");
126 }
127 printer->Print("String ").Print(name_).Print("=\"").Print(val_).Print("\";");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700129
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700131 DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
132
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 std::string name_;
134 std::string val_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700135};
136
137using IntMember = PrimitiveMember<uint32_t>;
138using ResourceMember = PrimitiveMember<ResourceId>;
139using StringMember = PrimitiveMember<std::string>;
140
Ryan Mitchell5855de72021-02-24 14:39:13 -0800141template <typename T, typename StringConverter>
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700142class PrimitiveArrayMember : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -0800144 explicit PrimitiveArrayMember(const android::StringPiece& name) : name_(name.to_string()) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145
Adam Lesinskif852dd02017-08-18 19:49:58 -0700146 void AddElement(const T& val) {
Ryan Mitchell5855de72021-02-24 14:39:13 -0800147 elements_.emplace_back(val);
Adam Lesinskif852dd02017-08-18 19:49:58 -0700148 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149
Adam Lesinskif852dd02017-08-18 19:49:58 -0700150 bool empty() const override {
151 return false;
152 }
153
154 const std::string& GetName() const override {
155 return name_;
156 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157
Makoto Onukide6e6f22020-06-22 10:17:02 -0700158 void Print(bool final, text::Printer* printer, bool strip_api_annotations = false)
159 const override {
160 ClassMember::Print(final, printer, strip_api_annotations);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800162 printer->Print("public static final int[] ").Print(name_).Print("={");
163 printer->Indent();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 const auto begin = elements_.begin();
166 const auto end = elements_.end();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 for (auto current = begin; current != end; ++current) {
168 if (std::distance(begin, current) % kAttribsPerLine == 0) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800169 printer->Println();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 }
171
Ryan Mitchell5855de72021-02-24 14:39:13 -0800172 printer->Print(StringConverter::ToString(*current));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 if (std::distance(current, end) > 1) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800174 printer->Print(", ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700176 }
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800177 printer->Println();
178 printer->Undent();
179 printer->Print("};");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700181
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700183 DISALLOW_COPY_AND_ASSIGN(PrimitiveArrayMember);
184
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 std::string name_;
186 std::vector<T> elements_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700187};
188
Ryan Mitchell5855de72021-02-24 14:39:13 -0800189struct FieldReference {
190 explicit FieldReference(std::string reference) : ref(std::move(reference)) {
191 }
192 std::string ref;
193};
194
195struct ResourceArrayMemberStringConverter {
196 static std::string ToString(const std::variant<ResourceId, FieldReference>& ref) {
197 if (auto id = std::get_if<ResourceId>(&ref)) {
198 return to_string(*id);
199 } else {
200 return std::get<FieldReference>(ref).ref;
201 }
202 }
203};
204
205using ResourceArrayMember = PrimitiveArrayMember<std::variant<ResourceId, FieldReference>,
206 ResourceArrayMemberStringConverter>;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700207
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800208// Represents a method in a class.
209class MethodDefinition : public ClassMember {
210 public:
211 // Expected method signature example: 'public static void onResourcesLoaded(int p)'.
212 explicit MethodDefinition(const android::StringPiece& signature)
213 : signature_(signature.to_string()) {}
214
215 // Appends a single statement to the method. It should include no newlines or else
216 // formatting may be broken.
217 void AppendStatement(const android::StringPiece& statement);
218
Adam Lesinskif852dd02017-08-18 19:49:58 -0700219 // Not quite the same as a name, but good enough.
220 const std::string& GetName() const override {
221 return signature_;
222 }
223
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800224 // Even if the method is empty, we always want to write the method signature.
Adam Lesinski761d4342017-09-29 11:15:17 -0700225 bool empty() const override {
226 return false;
227 }
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800228
Makoto Onukide6e6f22020-06-22 10:17:02 -0700229 void Print(bool final, text::Printer* printer, bool strip_api_annotations = false) const override;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800230
231 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700232 DISALLOW_COPY_AND_ASSIGN(MethodDefinition);
233
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800234 std::string signature_;
235 std::vector<std::string> statements_;
236};
237
238enum class ClassQualifier { kNone, kStatic };
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700239
240class ClassDefinition : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 public:
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800242 static void WriteJavaFile(const ClassDefinition* def, const android::StringPiece& package,
Makoto Onukide6e6f22020-06-22 10:17:02 -0700243 bool final, bool strip_api_annotations, io::OutputStream* out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700244
Adam Lesinskid5083f62017-01-16 15:07:21 -0800245 ClassDefinition(const android::StringPiece& name, ClassQualifier qualifier, bool createIfEmpty)
246 : name_(name.to_string()), qualifier_(qualifier), create_if_empty_(createIfEmpty) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700247
Adam Lesinskif852dd02017-08-18 19:49:58 -0700248 enum class Result {
249 kAdded,
250 kOverridden,
251 };
252
253 Result AddMember(std::unique_ptr<ClassMember> member);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700254
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 bool empty() const override;
Adam Lesinskif852dd02017-08-18 19:49:58 -0700256
257 const std::string& GetName() const override {
258 return name_;
259 }
260
Makoto Onukide6e6f22020-06-22 10:17:02 -0700261 void Print(bool final, text::Printer* printer, bool strip_api_annotations = false) const override;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700262
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700264 DISALLOW_COPY_AND_ASSIGN(ClassDefinition);
Adam Lesinskif852dd02017-08-18 19:49:58 -0700265
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 std::string name_;
267 ClassQualifier qualifier_;
268 bool create_if_empty_;
Adam Lesinski761d4342017-09-29 11:15:17 -0700269 std::vector<std::unique_ptr<ClassMember>> ordered_members_;
270 std::unordered_map<android::StringPiece, size_t> indexed_members_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700271};
272
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273} // namespace aapt
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700274
275#endif /* AAPT_JAVA_CLASSDEFINITION_H */