blob: 28a3489e71a43814a9b7be5e7d714485ac0cf1b6 [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 <ostream>
21#include <string>
Adam Lesinski1ee1a102017-09-29 11:15:17 -070022#include <unordered_map>
23#include <vector>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024
25#include "android-base/macros.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080026#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070028#include "Resource.h"
29#include "java/AnnotationProcessor.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 Lesinski6e241e72017-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 Lesinski6e241e72017-08-18 19:49:58 -070048 virtual const std::string& GetName() const = 0;
49
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080050 // Writes the class member to the out stream. Subclasses should derive this method
51 // to write their own data. Call this base method from the subclass to write out
52 // this member's comments/annotations.
Adam Lesinskid5083f62017-01-16 15:07:21 -080053 virtual void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080054 std::ostream* out) const;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070055
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 AnnotationProcessor processor_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070058};
59
60template <typename T>
61class PrimitiveMember : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -080063 PrimitiveMember(const android::StringPiece& name, const T& val)
64 : name_(name.to_string()), val_(val) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070065
Adam Lesinski6e241e72017-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
Adam Lesinskid5083f62017-01-16 15:07:21 -080074 void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 std::ostream* out) const override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 ClassMember::WriteToStream(prefix, final, out);
Adam Lesinski1ee1a102017-09-29 11:15:17 -070077 *out << prefix << "public static " << (final ? "final " : "") << "int " << name_ << "=" << val_
78 << ";";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070080
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 private:
Adam Lesinski1ee1a102017-09-29 11:15:17 -070082 DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
83
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 std::string name_;
85 T val_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070086};
87
Adam Lesinski1ee1a102017-09-29 11:15:17 -070088// Specialization for strings so they get the right type and are quoted with "".
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070089template <>
90class PrimitiveMember<std::string> : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -080092 PrimitiveMember(const android::StringPiece& name, const std::string& val)
93 : name_(name.to_string()), val_(val) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070094
Adam Lesinski6e241e72017-08-18 19:49:58 -070095 bool empty() const override {
96 return false;
97 }
98
99 const std::string& GetName() const override {
100 return name_;
101 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700102
Adam Lesinskid5083f62017-01-16 15:07:21 -0800103 void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 std::ostream* out) const override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 ClassMember::WriteToStream(prefix, final, out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 *out << prefix << "public static " << (final ? "final " : "") << "String "
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 << name_ << "=\"" << val_ << "\";";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700110
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 private:
Adam Lesinski1ee1a102017-09-29 11:15:17 -0700112 DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
113
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 std::string name_;
115 std::string val_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700116};
117
118using IntMember = PrimitiveMember<uint32_t>;
119using ResourceMember = PrimitiveMember<ResourceId>;
120using StringMember = PrimitiveMember<std::string>;
121
122template <typename T>
123class PrimitiveArrayMember : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -0800125 explicit PrimitiveArrayMember(const android::StringPiece& name) : name_(name.to_string()) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126
Adam Lesinski6e241e72017-08-18 19:49:58 -0700127 void AddElement(const T& val) {
128 elements_.push_back(val);
129 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130
Adam Lesinski6e241e72017-08-18 19:49:58 -0700131 bool empty() const override {
132 return false;
133 }
134
135 const std::string& GetName() const override {
136 return name_;
137 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138
Adam Lesinskid5083f62017-01-16 15:07:21 -0800139 void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 std::ostream* out) const override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 ClassMember::WriteToStream(prefix, final, out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143 *out << prefix << "public static final int[] " << name_ << "={";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 const auto begin = elements_.begin();
146 const auto end = elements_.end();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 for (auto current = begin; current != end; ++current) {
148 if (std::distance(begin, current) % kAttribsPerLine == 0) {
149 *out << "\n" << prefix << kIndent << kIndent;
150 }
151
152 *out << *current;
153 if (std::distance(current, end) > 1) {
154 *out << ", ";
155 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700156 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 *out << "\n" << prefix << kIndent << "};";
158 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700159
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 private:
Adam Lesinski1ee1a102017-09-29 11:15:17 -0700161 DISALLOW_COPY_AND_ASSIGN(PrimitiveArrayMember);
162
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 std::string name_;
164 std::vector<T> elements_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700165};
166
167using ResourceArrayMember = PrimitiveArrayMember<ResourceId>;
168
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800169// Represents a method in a class.
170class MethodDefinition : public ClassMember {
171 public:
172 // Expected method signature example: 'public static void onResourcesLoaded(int p)'.
173 explicit MethodDefinition(const android::StringPiece& signature)
174 : signature_(signature.to_string()) {}
175
176 // Appends a single statement to the method. It should include no newlines or else
177 // formatting may be broken.
178 void AppendStatement(const android::StringPiece& statement);
179
Adam Lesinski6e241e72017-08-18 19:49:58 -0700180 // Not quite the same as a name, but good enough.
181 const std::string& GetName() const override {
182 return signature_;
183 }
184
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800185 // Even if the method is empty, we always want to write the method signature.
Adam Lesinski1ee1a102017-09-29 11:15:17 -0700186 bool empty() const override {
187 return false;
188 }
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800189
190 void WriteToStream(const android::StringPiece& prefix, bool final,
191 std::ostream* out) const override;
192
193 private:
Adam Lesinski1ee1a102017-09-29 11:15:17 -0700194 DISALLOW_COPY_AND_ASSIGN(MethodDefinition);
195
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800196 std::string signature_;
197 std::vector<std::string> statements_;
198};
199
200enum class ClassQualifier { kNone, kStatic };
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700201
202class ClassDefinition : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -0800204 static bool WriteJavaFile(const ClassDefinition* def, const android::StringPiece& package,
205 bool final, std::ostream* out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700206
Adam Lesinskid5083f62017-01-16 15:07:21 -0800207 ClassDefinition(const android::StringPiece& name, ClassQualifier qualifier, bool createIfEmpty)
208 : name_(name.to_string()), qualifier_(qualifier), create_if_empty_(createIfEmpty) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700209
Adam Lesinski6e241e72017-08-18 19:49:58 -0700210 enum class Result {
211 kAdded,
212 kOverridden,
213 };
214
215 Result AddMember(std::unique_ptr<ClassMember> member);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700216
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 bool empty() const override;
Adam Lesinski6e241e72017-08-18 19:49:58 -0700218
219 const std::string& GetName() const override {
220 return name_;
221 }
222
Adam Lesinskid5083f62017-01-16 15:07:21 -0800223 void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 std::ostream* out) const override;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700225
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 private:
Adam Lesinski1ee1a102017-09-29 11:15:17 -0700227 DISALLOW_COPY_AND_ASSIGN(ClassDefinition);
Adam Lesinski6e241e72017-08-18 19:49:58 -0700228
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 std::string name_;
230 ClassQualifier qualifier_;
231 bool create_if_empty_;
Adam Lesinski1ee1a102017-09-29 11:15:17 -0700232 std::vector<std::unique_ptr<ClassMember>> ordered_members_;
233 std::unordered_map<android::StringPiece, size_t> indexed_members_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700234};
235
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236} // namespace aapt
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700237
238#endif /* AAPT_JAVA_CLASSDEFINITION_H */