blob: 995495ac56a898f710c314f5d96c8b85df35b8c3 [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:
Adam Lesinskid5083f62017-01-16 15:07:21 -080062 PrimitiveMember(const android::StringPiece& name, const T& val)
63 : name_(name.to_string()), val_(val) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070064
Adam Lesinskif852dd02017-08-18 19:49:58 -070065 bool empty() const override {
66 return false;
67 }
68
69 const std::string& GetName() const override {
70 return name_;
71 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070072
Ryan Mitchell5855de72021-02-24 14:39:13 -080073 void Print(bool final, text::Printer* printer,
74 bool strip_api_annotations = false) const override {
Adam Lesinskia693c4a2017-11-09 11:29:39 -080075 using std::to_string;
76
Makoto Onukide6e6f22020-06-22 10:17:02 -070077 ClassMember::Print(final, printer, strip_api_annotations);
Adam Lesinskia693c4a2017-11-09 11:29:39 -080078
79 printer->Print("public static ");
80 if (final) {
81 printer->Print("final ");
82 }
83 printer->Print("int ").Print(name_).Print("=").Print(to_string(val_)).Print(";");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070085
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 private:
Adam Lesinski761d4342017-09-29 11:15:17 -070087 DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
88
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 std::string name_;
90 T val_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070091};
92
Adam Lesinski761d4342017-09-29 11:15:17 -070093// Specialization for strings so they get the right type and are quoted with "".
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070094template <>
95class PrimitiveMember<std::string> : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -080097 PrimitiveMember(const android::StringPiece& name, const std::string& val)
98 : name_(name.to_string()), val_(val) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070099
Adam Lesinskif852dd02017-08-18 19:49:58 -0700100 bool empty() const override {
101 return false;
102 }
103
104 const std::string& GetName() const override {
105 return name_;
106 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700107
Makoto Onukide6e6f22020-06-22 10:17:02 -0700108 void Print(bool final, text::Printer* printer, bool strip_api_annotations = false)
109 const override {
110 ClassMember::Print(final, printer, strip_api_annotations);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700111
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800112 printer->Print("public static ");
113 if (final) {
114 printer->Print("final ");
115 }
116 printer->Print("String ").Print(name_).Print("=\"").Print(val_).Print("\";");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700118
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700120 DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
121
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 std::string name_;
123 std::string val_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700124};
125
126using IntMember = PrimitiveMember<uint32_t>;
127using ResourceMember = PrimitiveMember<ResourceId>;
128using StringMember = PrimitiveMember<std::string>;
129
Ryan Mitchell5855de72021-02-24 14:39:13 -0800130template <typename T, typename StringConverter>
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700131class PrimitiveArrayMember : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -0800133 explicit PrimitiveArrayMember(const android::StringPiece& name) : name_(name.to_string()) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134
Adam Lesinskif852dd02017-08-18 19:49:58 -0700135 void AddElement(const T& val) {
Ryan Mitchell5855de72021-02-24 14:39:13 -0800136 elements_.emplace_back(val);
Adam Lesinskif852dd02017-08-18 19:49:58 -0700137 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138
Adam Lesinskif852dd02017-08-18 19:49:58 -0700139 bool empty() const override {
140 return false;
141 }
142
143 const std::string& GetName() const override {
144 return name_;
145 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146
Makoto Onukide6e6f22020-06-22 10:17:02 -0700147 void Print(bool final, text::Printer* printer, bool strip_api_annotations = false)
148 const override {
149 ClassMember::Print(final, printer, strip_api_annotations);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800151 printer->Print("public static final int[] ").Print(name_).Print("={");
152 printer->Indent();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 const auto begin = elements_.begin();
155 const auto end = elements_.end();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 for (auto current = begin; current != end; ++current) {
157 if (std::distance(begin, current) % kAttribsPerLine == 0) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800158 printer->Println();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 }
160
Ryan Mitchell5855de72021-02-24 14:39:13 -0800161 printer->Print(StringConverter::ToString(*current));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 if (std::distance(current, end) > 1) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800163 printer->Print(", ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700165 }
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800166 printer->Println();
167 printer->Undent();
168 printer->Print("};");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700170
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700172 DISALLOW_COPY_AND_ASSIGN(PrimitiveArrayMember);
173
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 std::string name_;
175 std::vector<T> elements_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700176};
177
Ryan Mitchell5855de72021-02-24 14:39:13 -0800178struct FieldReference {
179 explicit FieldReference(std::string reference) : ref(std::move(reference)) {
180 }
181 std::string ref;
182};
183
184struct ResourceArrayMemberStringConverter {
185 static std::string ToString(const std::variant<ResourceId, FieldReference>& ref) {
186 if (auto id = std::get_if<ResourceId>(&ref)) {
187 return to_string(*id);
188 } else {
189 return std::get<FieldReference>(ref).ref;
190 }
191 }
192};
193
194using ResourceArrayMember = PrimitiveArrayMember<std::variant<ResourceId, FieldReference>,
195 ResourceArrayMemberStringConverter>;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700196
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800197// Represents a method in a class.
198class MethodDefinition : public ClassMember {
199 public:
200 // Expected method signature example: 'public static void onResourcesLoaded(int p)'.
201 explicit MethodDefinition(const android::StringPiece& signature)
202 : signature_(signature.to_string()) {}
203
204 // Appends a single statement to the method. It should include no newlines or else
205 // formatting may be broken.
206 void AppendStatement(const android::StringPiece& statement);
207
Adam Lesinskif852dd02017-08-18 19:49:58 -0700208 // Not quite the same as a name, but good enough.
209 const std::string& GetName() const override {
210 return signature_;
211 }
212
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800213 // Even if the method is empty, we always want to write the method signature.
Adam Lesinski761d4342017-09-29 11:15:17 -0700214 bool empty() const override {
215 return false;
216 }
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800217
Makoto Onukide6e6f22020-06-22 10:17:02 -0700218 void Print(bool final, text::Printer* printer, bool strip_api_annotations = false) const override;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800219
220 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700221 DISALLOW_COPY_AND_ASSIGN(MethodDefinition);
222
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800223 std::string signature_;
224 std::vector<std::string> statements_;
225};
226
227enum class ClassQualifier { kNone, kStatic };
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700228
229class ClassDefinition : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 public:
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800231 static void WriteJavaFile(const ClassDefinition* def, const android::StringPiece& package,
Makoto Onukide6e6f22020-06-22 10:17:02 -0700232 bool final, bool strip_api_annotations, io::OutputStream* out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700233
Adam Lesinskid5083f62017-01-16 15:07:21 -0800234 ClassDefinition(const android::StringPiece& name, ClassQualifier qualifier, bool createIfEmpty)
235 : name_(name.to_string()), qualifier_(qualifier), create_if_empty_(createIfEmpty) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700236
Adam Lesinskif852dd02017-08-18 19:49:58 -0700237 enum class Result {
238 kAdded,
239 kOverridden,
240 };
241
242 Result AddMember(std::unique_ptr<ClassMember> member);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700243
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 bool empty() const override;
Adam Lesinskif852dd02017-08-18 19:49:58 -0700245
246 const std::string& GetName() const override {
247 return name_;
248 }
249
Makoto Onukide6e6f22020-06-22 10:17:02 -0700250 void Print(bool final, text::Printer* printer, bool strip_api_annotations = false) const override;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700251
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700253 DISALLOW_COPY_AND_ASSIGN(ClassDefinition);
Adam Lesinskif852dd02017-08-18 19:49:58 -0700254
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 std::string name_;
256 ClassQualifier qualifier_;
257 bool create_if_empty_;
Adam Lesinski761d4342017-09-29 11:15:17 -0700258 std::vector<std::unique_ptr<ClassMember>> ordered_members_;
259 std::unordered_map<android::StringPiece, size_t> indexed_members_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700260};
261
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262} // namespace aapt
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700263
264#endif /* AAPT_JAVA_CLASSDEFINITION_H */