blob: 6c4bcad83d2ab93155f1c2f8f6c16f370f8915a2 [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>
Adam Lesinski6e241e72017-08-18 19:49:58 -070021#include <set>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include <string>
23
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 Lesinski6cbfb1d2016-03-31 13:33:02 -070029#include "util/Util.h"
30
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070031namespace aapt {
32
33// The number of attributes to emit per line in a Styleable array.
34constexpr static size_t kAttribsPerLine = 4;
35constexpr static const char* kIndent = " ";
36
37class ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 public:
39 virtual ~ClassMember() = default;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070040
Adam Lesinski6e241e72017-08-18 19:49:58 -070041 AnnotationProcessor* GetCommentBuilder() {
42 return &processor_;
43 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070044
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 virtual bool empty() const = 0;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070046
Adam Lesinski6e241e72017-08-18 19:49:58 -070047 virtual const std::string& GetName() const = 0;
48
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080049 // Writes the class member to the out stream. Subclasses should derive this method
50 // to write their own data. Call this base method from the subclass to write out
51 // this member's comments/annotations.
Adam Lesinskid5083f62017-01-16 15:07:21 -080052 virtual void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080053 std::ostream* out) 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 Lesinski6e241e72017-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 Lesinskid5083f62017-01-16 15:07:21 -080073 void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 std::ostream* out) const override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 ClassMember::WriteToStream(prefix, final, out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070076
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 *out << prefix << "public static " << (final ? "final " : "") << "int "
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 << name_ << "=" << val_ << ";";
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 Lesinskice5e56e2016-10-21 17:56:45 -070082 std::string name_;
83 T val_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070084
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070086};
87
88/**
89 * Specialization for strings so they get the right type and are quoted with "".
90 */
91template <>
92class PrimitiveMember<std::string> : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -080094 PrimitiveMember(const android::StringPiece& name, const std::string& val)
95 : name_(name.to_string()), val_(val) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070096
Adam Lesinski6e241e72017-08-18 19:49:58 -070097 bool empty() const override {
98 return false;
99 }
100
101 const std::string& GetName() const override {
102 return name_;
103 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700104
Adam Lesinskid5083f62017-01-16 15:07:21 -0800105 void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 std::ostream* out) const override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 ClassMember::WriteToStream(prefix, final, out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700108
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 *out << prefix << "public static " << (final ? "final " : "") << "String "
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 << name_ << "=\"" << val_ << "\";";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700112
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 std::string name_;
115 std::string val_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700118};
119
120using IntMember = PrimitiveMember<uint32_t>;
121using ResourceMember = PrimitiveMember<ResourceId>;
122using StringMember = PrimitiveMember<std::string>;
123
124template <typename T>
125class PrimitiveArrayMember : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -0800127 explicit PrimitiveArrayMember(const android::StringPiece& name) : name_(name.to_string()) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128
Adam Lesinski6e241e72017-08-18 19:49:58 -0700129 void AddElement(const T& val) {
130 elements_.push_back(val);
131 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132
Adam Lesinski6e241e72017-08-18 19:49:58 -0700133 bool empty() const override {
134 return false;
135 }
136
137 const std::string& GetName() const override {
138 return name_;
139 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140
Adam Lesinskid5083f62017-01-16 15:07:21 -0800141 void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 std::ostream* out) const override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143 ClassMember::WriteToStream(prefix, final, out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 *out << prefix << "public static final int[] " << name_ << "={";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 const auto begin = elements_.begin();
148 const auto end = elements_.end();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 for (auto current = begin; current != end; ++current) {
150 if (std::distance(begin, current) % kAttribsPerLine == 0) {
151 *out << "\n" << prefix << kIndent << kIndent;
152 }
153
154 *out << *current;
155 if (std::distance(current, end) > 1) {
156 *out << ", ";
157 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700158 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 *out << "\n" << prefix << kIndent << "};";
160 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700161
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 std::string name_;
164 std::vector<T> elements_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700165
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 DISALLOW_COPY_AND_ASSIGN(PrimitiveArrayMember);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700167};
168
169using ResourceArrayMember = PrimitiveArrayMember<ResourceId>;
170
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800171// Represents a method in a class.
172class MethodDefinition : public ClassMember {
173 public:
174 // Expected method signature example: 'public static void onResourcesLoaded(int p)'.
175 explicit MethodDefinition(const android::StringPiece& signature)
176 : signature_(signature.to_string()) {}
177
178 // Appends a single statement to the method. It should include no newlines or else
179 // formatting may be broken.
180 void AppendStatement(const android::StringPiece& statement);
181
Adam Lesinski6e241e72017-08-18 19:49:58 -0700182 // Not quite the same as a name, but good enough.
183 const std::string& GetName() const override {
184 return signature_;
185 }
186
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800187 // Even if the method is empty, we always want to write the method signature.
188 bool empty() const override { return false; }
189
190 void WriteToStream(const android::StringPiece& prefix, bool final,
191 std::ostream* out) const override;
192
193 private:
194 std::string signature_;
195 std::vector<std::string> statements_;
196};
197
198enum class ClassQualifier { kNone, kStatic };
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700199
200class ClassDefinition : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -0800202 static bool WriteJavaFile(const ClassDefinition* def, const android::StringPiece& package,
203 bool final, std::ostream* out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700204
Adam Lesinskid5083f62017-01-16 15:07:21 -0800205 ClassDefinition(const android::StringPiece& name, ClassQualifier qualifier, bool createIfEmpty)
206 : name_(name.to_string()), qualifier_(qualifier), create_if_empty_(createIfEmpty) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700207
Adam Lesinski6e241e72017-08-18 19:49:58 -0700208 enum class Result {
209 kAdded,
210 kOverridden,
211 };
212
213 Result AddMember(std::unique_ptr<ClassMember> member);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700214
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 bool empty() const override;
Adam Lesinski6e241e72017-08-18 19:49:58 -0700216
217 const std::string& GetName() const override {
218 return name_;
219 }
220
Adam Lesinskid5083f62017-01-16 15:07:21 -0800221 void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 std::ostream* out) const override;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700223
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 private:
Adam Lesinski6e241e72017-08-18 19:49:58 -0700225 struct ClassMemberCompare {
226 using T = std::unique_ptr<ClassMember>;
227 bool operator()(const T& a, const T& b) const {
228 return a->GetName() < b->GetName();
229 }
230 };
231
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 std::string name_;
233 ClassQualifier qualifier_;
234 bool create_if_empty_;
Adam Lesinski6e241e72017-08-18 19:49:58 -0700235 std::set<std::unique_ptr<ClassMember>, ClassMemberCompare> members_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700236
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 DISALLOW_COPY_AND_ASSIGN(ClassDefinition);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700238};
239
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240} // namespace aapt
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700241
242#endif /* AAPT_JAVA_CLASSDEFINITION_H */