blob: 84e3f33db94140bee8316b02aad8fa8a76405842 [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 Lesinskice5e56e22016-10-21 17:56:45 -070020#include <string>
Adam Lesinski761d4342017-09-29 11:15:17 -070021#include <unordered_map>
22#include <vector>
Adam Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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:
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070062 PrimitiveMember(android::StringPiece name, const T& val, bool staged_api = false)
63 : name_(name), val_(val), staged_api_(staged_api) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -070064 }
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 Lesinskice5e56e22016-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:
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700107 PrimitiveMember(android::StringPiece name, const std::string& val, bool staged_api = false)
108 : name_(name), val_(val) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700109 }
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 Lesinskice5e56e22016-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:
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700144 explicit PrimitiveArrayMember(android::StringPiece name) : name_(name) {
145 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146
Adam Lesinskif852dd02017-08-18 19:49:58 -0700147 void AddElement(const T& val) {
Ryan Mitchell5855de72021-02-24 14:39:13 -0800148 elements_.emplace_back(val);
Adam Lesinskif852dd02017-08-18 19:49:58 -0700149 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150
Adam Lesinskif852dd02017-08-18 19:49:58 -0700151 bool empty() const override {
152 return false;
153 }
154
155 const std::string& GetName() const override {
156 return name_;
157 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158
Makoto Onukide6e6f22020-06-22 10:17:02 -0700159 void Print(bool final, text::Printer* printer, bool strip_api_annotations = false)
160 const override {
161 ClassMember::Print(final, printer, strip_api_annotations);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800163 printer->Print("public static final int[] ").Print(name_).Print("={");
164 printer->Indent();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700166 const auto begin = elements_.begin();
167 const auto end = elements_.end();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 for (auto current = begin; current != end; ++current) {
169 if (std::distance(begin, current) % kAttribsPerLine == 0) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800170 printer->Println();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 }
172
Ryan Mitchell5855de72021-02-24 14:39:13 -0800173 printer->Print(StringConverter::ToString(*current));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 if (std::distance(current, end) > 1) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800175 printer->Print(", ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700177 }
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800178 printer->Println();
179 printer->Undent();
180 printer->Print("};");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700182
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700184 DISALLOW_COPY_AND_ASSIGN(PrimitiveArrayMember);
185
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700186 std::string name_;
187 std::vector<T> elements_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700188};
189
Ryan Mitchell5855de72021-02-24 14:39:13 -0800190struct FieldReference {
191 explicit FieldReference(std::string reference) : ref(std::move(reference)) {
192 }
193 std::string ref;
194};
195
196struct ResourceArrayMemberStringConverter {
197 static std::string ToString(const std::variant<ResourceId, FieldReference>& ref) {
198 if (auto id = std::get_if<ResourceId>(&ref)) {
199 return to_string(*id);
200 } else {
201 return std::get<FieldReference>(ref).ref;
202 }
203 }
204};
205
206using ResourceArrayMember = PrimitiveArrayMember<std::variant<ResourceId, FieldReference>,
207 ResourceArrayMemberStringConverter>;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700208
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800209// Represents a method in a class.
210class MethodDefinition : public ClassMember {
211 public:
212 // Expected method signature example: 'public static void onResourcesLoaded(int p)'.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700213 explicit MethodDefinition(android::StringPiece signature) : signature_(signature) {
214 }
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800215
216 // Appends a single statement to the method. It should include no newlines or else
217 // formatting may be broken.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700218 void AppendStatement(android::StringPiece statement);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800219
Adam Lesinskif852dd02017-08-18 19:49:58 -0700220 // Not quite the same as a name, but good enough.
221 const std::string& GetName() const override {
222 return signature_;
223 }
224
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800225 // Even if the method is empty, we always want to write the method signature.
Adam Lesinski761d4342017-09-29 11:15:17 -0700226 bool empty() const override {
227 return false;
228 }
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800229
Makoto Onukide6e6f22020-06-22 10:17:02 -0700230 void Print(bool final, text::Printer* printer, bool strip_api_annotations = false) const override;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800231
232 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700233 DISALLOW_COPY_AND_ASSIGN(MethodDefinition);
234
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800235 std::string signature_;
236 std::vector<std::string> statements_;
237};
238
239enum class ClassQualifier { kNone, kStatic };
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700240
241class ClassDefinition : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 public:
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700243 static void WriteJavaFile(const ClassDefinition* def, android::StringPiece package, bool final,
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000244 bool strip_api_annotations, android::OutputStream* out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700245
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700246 ClassDefinition(android::StringPiece name, ClassQualifier qualifier, bool createIfEmpty)
247 : name_(name), qualifier_(qualifier), create_if_empty_(createIfEmpty) {
248 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700249
Adam Lesinskif852dd02017-08-18 19:49:58 -0700250 enum class Result {
251 kAdded,
252 kOverridden,
253 };
254
255 Result AddMember(std::unique_ptr<ClassMember> member);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700256
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 bool empty() const override;
Adam Lesinskif852dd02017-08-18 19:49:58 -0700258
259 const std::string& GetName() const override {
260 return name_;
261 }
262
Makoto Onukide6e6f22020-06-22 10:17:02 -0700263 void Print(bool final, text::Printer* printer, bool strip_api_annotations = false) const override;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700264
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700266 DISALLOW_COPY_AND_ASSIGN(ClassDefinition);
Adam Lesinskif852dd02017-08-18 19:49:58 -0700267
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700268 std::string name_;
269 ClassQualifier qualifier_;
270 bool create_if_empty_;
Adam Lesinski761d4342017-09-29 11:15:17 -0700271 std::vector<std::unique_ptr<ClassMember>> ordered_members_;
272 std::unordered_map<android::StringPiece, size_t> indexed_members_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700273};
274
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275} // namespace aapt
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700276
277#endif /* AAPT_JAVA_CLASSDEFINITION_H */