blob: 1e4b6816075a4162b3d4988de5011e36e29fb050 [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
Makoto Onukide6e6f22020-06-22 10:17:02 -070073 void Print(bool final, text::Printer* printer, bool strip_api_annotations = false)
74 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
130template <typename T>
131class 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) {
136 elements_.push_back(val);
137 }
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
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800161 printer->Print(to_string(*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
178using ResourceArrayMember = PrimitiveArrayMember<ResourceId>;
179
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800180// Represents a method in a class.
181class MethodDefinition : public ClassMember {
182 public:
183 // Expected method signature example: 'public static void onResourcesLoaded(int p)'.
184 explicit MethodDefinition(const android::StringPiece& signature)
185 : signature_(signature.to_string()) {}
186
187 // Appends a single statement to the method. It should include no newlines or else
188 // formatting may be broken.
189 void AppendStatement(const android::StringPiece& statement);
190
Adam Lesinskif852dd02017-08-18 19:49:58 -0700191 // Not quite the same as a name, but good enough.
192 const std::string& GetName() const override {
193 return signature_;
194 }
195
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800196 // Even if the method is empty, we always want to write the method signature.
Adam Lesinski761d4342017-09-29 11:15:17 -0700197 bool empty() const override {
198 return false;
199 }
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800200
Makoto Onukide6e6f22020-06-22 10:17:02 -0700201 void Print(bool final, text::Printer* printer, bool strip_api_annotations = false) const override;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800202
203 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700204 DISALLOW_COPY_AND_ASSIGN(MethodDefinition);
205
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800206 std::string signature_;
207 std::vector<std::string> statements_;
208};
209
210enum class ClassQualifier { kNone, kStatic };
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700211
212class ClassDefinition : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 public:
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800214 static void WriteJavaFile(const ClassDefinition* def, const android::StringPiece& package,
Makoto Onukide6e6f22020-06-22 10:17:02 -0700215 bool final, bool strip_api_annotations, io::OutputStream* out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700216
Adam Lesinskid5083f62017-01-16 15:07:21 -0800217 ClassDefinition(const android::StringPiece& name, ClassQualifier qualifier, bool createIfEmpty)
218 : name_(name.to_string()), qualifier_(qualifier), create_if_empty_(createIfEmpty) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700219
Adam Lesinskif852dd02017-08-18 19:49:58 -0700220 enum class Result {
221 kAdded,
222 kOverridden,
223 };
224
225 Result AddMember(std::unique_ptr<ClassMember> member);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700226
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 bool empty() const override;
Adam Lesinskif852dd02017-08-18 19:49:58 -0700228
229 const std::string& GetName() const override {
230 return name_;
231 }
232
Makoto Onukide6e6f22020-06-22 10:17:02 -0700233 void Print(bool final, text::Printer* printer, bool strip_api_annotations = false) const override;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700234
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700236 DISALLOW_COPY_AND_ASSIGN(ClassDefinition);
Adam Lesinskif852dd02017-08-18 19:49:58 -0700237
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 std::string name_;
239 ClassQualifier qualifier_;
240 bool create_if_empty_;
Adam Lesinski761d4342017-09-29 11:15:17 -0700241 std::vector<std::unique_ptr<ClassMember>> ordered_members_;
242 std::unordered_map<android::StringPiece, size_t> indexed_members_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700243};
244
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245} // namespace aapt
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700246
247#endif /* AAPT_JAVA_CLASSDEFINITION_H */