blob: d3648c8aae529a5b95b11b7c5e3e6fc1df1013e7 [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 Mitchell2e9bec12021-03-22 09:31:00 -070081 if (final && !staged_api_) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -080082 printer->Print("final ");
83 }
84 printer->Print("int ").Print(name_).Print("=").Print(to_string(val_)).Print(";");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070086
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 private:
Adam Lesinski761d4342017-09-29 11:15:17 -070088 DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
89
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 std::string name_;
91 T val_;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -070092 bool staged_api_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070093};
94
Adam Lesinski761d4342017-09-29 11:15:17 -070095// Specialization for strings so they get the right type and are quoted with "".
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070096template <>
97class PrimitiveMember<std::string> : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 public:
Ryan Mitchell2e9bec12021-03-22 09:31:00 -070099 PrimitiveMember(const android::StringPiece& name, const std::string& val, bool staged_api = false)
100 : name_(name.to_string()), val_(val) {
101 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700102
Adam Lesinskif852dd02017-08-18 19:49:58 -0700103 bool empty() const override {
104 return false;
105 }
106
107 const std::string& GetName() const override {
108 return name_;
109 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700110
Makoto Onukide6e6f22020-06-22 10:17:02 -0700111 void Print(bool final, text::Printer* printer, bool strip_api_annotations = false)
112 const override {
113 ClassMember::Print(final, printer, strip_api_annotations);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700114
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800115 printer->Print("public static ");
116 if (final) {
117 printer->Print("final ");
118 }
119 printer->Print("String ").Print(name_).Print("=\"").Print(val_).Print("\";");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700121
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700123 DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
124
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 std::string name_;
126 std::string val_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700127};
128
129using IntMember = PrimitiveMember<uint32_t>;
130using ResourceMember = PrimitiveMember<ResourceId>;
131using StringMember = PrimitiveMember<std::string>;
132
Ryan Mitchell5855de72021-02-24 14:39:13 -0800133template <typename T, typename StringConverter>
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700134class PrimitiveArrayMember : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -0800136 explicit PrimitiveArrayMember(const android::StringPiece& name) : name_(name.to_string()) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137
Adam Lesinskif852dd02017-08-18 19:49:58 -0700138 void AddElement(const T& val) {
Ryan Mitchell5855de72021-02-24 14:39:13 -0800139 elements_.emplace_back(val);
Adam Lesinskif852dd02017-08-18 19:49:58 -0700140 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141
Adam Lesinskif852dd02017-08-18 19:49:58 -0700142 bool empty() const override {
143 return false;
144 }
145
146 const std::string& GetName() const override {
147 return name_;
148 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149
Makoto Onukide6e6f22020-06-22 10:17:02 -0700150 void Print(bool final, text::Printer* printer, bool strip_api_annotations = false)
151 const override {
152 ClassMember::Print(final, printer, strip_api_annotations);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800154 printer->Print("public static final int[] ").Print(name_).Print("={");
155 printer->Indent();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 const auto begin = elements_.begin();
158 const auto end = elements_.end();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 for (auto current = begin; current != end; ++current) {
160 if (std::distance(begin, current) % kAttribsPerLine == 0) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800161 printer->Println();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 }
163
Ryan Mitchell5855de72021-02-24 14:39:13 -0800164 printer->Print(StringConverter::ToString(*current));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 if (std::distance(current, end) > 1) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800166 printer->Print(", ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700168 }
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800169 printer->Println();
170 printer->Undent();
171 printer->Print("};");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700173
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700175 DISALLOW_COPY_AND_ASSIGN(PrimitiveArrayMember);
176
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 std::string name_;
178 std::vector<T> elements_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700179};
180
Ryan Mitchell5855de72021-02-24 14:39:13 -0800181struct FieldReference {
182 explicit FieldReference(std::string reference) : ref(std::move(reference)) {
183 }
184 std::string ref;
185};
186
187struct ResourceArrayMemberStringConverter {
188 static std::string ToString(const std::variant<ResourceId, FieldReference>& ref) {
189 if (auto id = std::get_if<ResourceId>(&ref)) {
190 return to_string(*id);
191 } else {
192 return std::get<FieldReference>(ref).ref;
193 }
194 }
195};
196
197using ResourceArrayMember = PrimitiveArrayMember<std::variant<ResourceId, FieldReference>,
198 ResourceArrayMemberStringConverter>;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700199
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800200// Represents a method in a class.
201class MethodDefinition : public ClassMember {
202 public:
203 // Expected method signature example: 'public static void onResourcesLoaded(int p)'.
204 explicit MethodDefinition(const android::StringPiece& signature)
205 : signature_(signature.to_string()) {}
206
207 // Appends a single statement to the method. It should include no newlines or else
208 // formatting may be broken.
209 void AppendStatement(const android::StringPiece& statement);
210
Adam Lesinskif852dd02017-08-18 19:49:58 -0700211 // Not quite the same as a name, but good enough.
212 const std::string& GetName() const override {
213 return signature_;
214 }
215
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800216 // Even if the method is empty, we always want to write the method signature.
Adam Lesinski761d4342017-09-29 11:15:17 -0700217 bool empty() const override {
218 return false;
219 }
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800220
Makoto Onukide6e6f22020-06-22 10:17:02 -0700221 void Print(bool final, text::Printer* printer, bool strip_api_annotations = false) const override;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800222
223 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700224 DISALLOW_COPY_AND_ASSIGN(MethodDefinition);
225
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800226 std::string signature_;
227 std::vector<std::string> statements_;
228};
229
230enum class ClassQualifier { kNone, kStatic };
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700231
232class ClassDefinition : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 public:
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800234 static void WriteJavaFile(const ClassDefinition* def, const android::StringPiece& package,
Makoto Onukide6e6f22020-06-22 10:17:02 -0700235 bool final, bool strip_api_annotations, io::OutputStream* out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700236
Adam Lesinskid5083f62017-01-16 15:07:21 -0800237 ClassDefinition(const android::StringPiece& name, ClassQualifier qualifier, bool createIfEmpty)
238 : name_(name.to_string()), qualifier_(qualifier), create_if_empty_(createIfEmpty) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700239
Adam Lesinskif852dd02017-08-18 19:49:58 -0700240 enum class Result {
241 kAdded,
242 kOverridden,
243 };
244
245 Result AddMember(std::unique_ptr<ClassMember> member);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700246
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 bool empty() const override;
Adam Lesinskif852dd02017-08-18 19:49:58 -0700248
249 const std::string& GetName() const override {
250 return name_;
251 }
252
Makoto Onukide6e6f22020-06-22 10:17:02 -0700253 void Print(bool final, text::Printer* printer, bool strip_api_annotations = false) const override;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700254
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700256 DISALLOW_COPY_AND_ASSIGN(ClassDefinition);
Adam Lesinskif852dd02017-08-18 19:49:58 -0700257
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 std::string name_;
259 ClassQualifier qualifier_;
260 bool create_if_empty_;
Adam Lesinski761d4342017-09-29 11:15:17 -0700261 std::vector<std::unique_ptr<ClassMember>> ordered_members_;
262 std::unordered_map<android::StringPiece, size_t> indexed_members_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700263};
264
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265} // namespace aapt
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700266
267#endif /* AAPT_JAVA_CLASSDEFINITION_H */