blob: fb11266f1761c8bba57422380b51176c4202ffe1 [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.
Adam Lesinskia693c4a2017-11-09 11:29:39 -080053 virtual void Print(bool final, text::Printer* printer) 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
Adam Lesinskia693c4a2017-11-09 11:29:39 -080073 void Print(bool final, text::Printer* printer) const override {
74 using std::to_string;
75
76 ClassMember::Print(final, printer);
77
78 printer->Print("public static ");
79 if (final) {
80 printer->Print("final ");
81 }
82 printer->Print("int ").Print(name_).Print("=").Print(to_string(val_)).Print(";");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070084
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 private:
Adam Lesinski761d4342017-09-29 11:15:17 -070086 DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
87
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 std::string name_;
89 T val_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070090};
91
Adam Lesinski761d4342017-09-29 11:15:17 -070092// Specialization for strings so they get the right type and are quoted with "".
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070093template <>
94class PrimitiveMember<std::string> : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -080096 PrimitiveMember(const android::StringPiece& name, const std::string& val)
97 : name_(name.to_string()), val_(val) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070098
Adam Lesinskif852dd02017-08-18 19:49:58 -070099 bool empty() const override {
100 return false;
101 }
102
103 const std::string& GetName() const override {
104 return name_;
105 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700106
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800107 void Print(bool final, text::Printer* printer) const override {
108 ClassMember::Print(final, printer);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700109
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800110 printer->Print("public static ");
111 if (final) {
112 printer->Print("final ");
113 }
114 printer->Print("String ").Print(name_).Print("=\"").Print(val_).Print("\";");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700118 DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
119
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 std::string name_;
121 std::string val_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700122};
123
124using IntMember = PrimitiveMember<uint32_t>;
125using ResourceMember = PrimitiveMember<ResourceId>;
126using StringMember = PrimitiveMember<std::string>;
127
128template <typename T>
129class PrimitiveArrayMember : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -0800131 explicit PrimitiveArrayMember(const android::StringPiece& name) : name_(name.to_string()) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132
Adam Lesinskif852dd02017-08-18 19:49:58 -0700133 void AddElement(const T& val) {
134 elements_.push_back(val);
135 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136
Adam Lesinskif852dd02017-08-18 19:49:58 -0700137 bool empty() const override {
138 return false;
139 }
140
141 const std::string& GetName() const override {
142 return name_;
143 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800145 void Print(bool final, text::Printer* printer) const override {
146 ClassMember::Print(final, printer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800148 printer->Print("public static final int[] ").Print(name_).Print("={");
149 printer->Indent();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 const auto begin = elements_.begin();
152 const auto end = elements_.end();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 for (auto current = begin; current != end; ++current) {
154 if (std::distance(begin, current) % kAttribsPerLine == 0) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800155 printer->Println();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 }
157
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800158 printer->Print(to_string(*current));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 if (std::distance(current, end) > 1) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800160 printer->Print(", ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700162 }
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800163 printer->Println();
164 printer->Undent();
165 printer->Print("};");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700167
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700169 DISALLOW_COPY_AND_ASSIGN(PrimitiveArrayMember);
170
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 std::string name_;
172 std::vector<T> elements_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700173};
174
175using ResourceArrayMember = PrimitiveArrayMember<ResourceId>;
176
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800177// Represents a method in a class.
178class MethodDefinition : public ClassMember {
179 public:
180 // Expected method signature example: 'public static void onResourcesLoaded(int p)'.
181 explicit MethodDefinition(const android::StringPiece& signature)
182 : signature_(signature.to_string()) {}
183
184 // Appends a single statement to the method. It should include no newlines or else
185 // formatting may be broken.
186 void AppendStatement(const android::StringPiece& statement);
187
Adam Lesinskif852dd02017-08-18 19:49:58 -0700188 // Not quite the same as a name, but good enough.
189 const std::string& GetName() const override {
190 return signature_;
191 }
192
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800193 // Even if the method is empty, we always want to write the method signature.
Adam Lesinski761d4342017-09-29 11:15:17 -0700194 bool empty() const override {
195 return false;
196 }
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800197
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800198 void Print(bool final, text::Printer* printer) const override;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800199
200 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700201 DISALLOW_COPY_AND_ASSIGN(MethodDefinition);
202
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800203 std::string signature_;
204 std::vector<std::string> statements_;
205};
206
207enum class ClassQualifier { kNone, kStatic };
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700208
209class ClassDefinition : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 public:
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800211 static void WriteJavaFile(const ClassDefinition* def, const android::StringPiece& package,
212 bool final, io::OutputStream* out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700213
Adam Lesinskid5083f62017-01-16 15:07:21 -0800214 ClassDefinition(const android::StringPiece& name, ClassQualifier qualifier, bool createIfEmpty)
215 : name_(name.to_string()), qualifier_(qualifier), create_if_empty_(createIfEmpty) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700216
Adam Lesinskif852dd02017-08-18 19:49:58 -0700217 enum class Result {
218 kAdded,
219 kOverridden,
220 };
221
222 Result AddMember(std::unique_ptr<ClassMember> member);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700223
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 bool empty() const override;
Adam Lesinskif852dd02017-08-18 19:49:58 -0700225
226 const std::string& GetName() const override {
227 return name_;
228 }
229
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800230 void Print(bool final, text::Printer* printer) const override;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700231
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 private:
Adam Lesinski761d4342017-09-29 11:15:17 -0700233 DISALLOW_COPY_AND_ASSIGN(ClassDefinition);
Adam Lesinskif852dd02017-08-18 19:49:58 -0700234
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235 std::string name_;
236 ClassQualifier qualifier_;
237 bool create_if_empty_;
Adam Lesinski761d4342017-09-29 11:15:17 -0700238 std::vector<std::unique_ptr<ClassMember>> ordered_members_;
239 std::unordered_map<android::StringPiece, size_t> indexed_members_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700240};
241
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242} // namespace aapt
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700243
244#endif /* AAPT_JAVA_CLASSDEFINITION_H */