blob: ca76421390d6a5437204d284dc7768906b0060b4 [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>
22
23#include "android-base/macros.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080024#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070026#include "Resource.h"
27#include "java/AnnotationProcessor.h"
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070028#include "util/Util.h"
29
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070030namespace aapt {
31
32// The number of attributes to emit per line in a Styleable array.
33constexpr static size_t kAttribsPerLine = 4;
34constexpr static const char* kIndent = " ";
35
36class ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 public:
38 virtual ~ClassMember() = default;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070039
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 AnnotationProcessor* GetCommentBuilder() { return &processor_; }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070041
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 virtual bool empty() const = 0;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070043
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080044 // Writes the class member to the out stream. Subclasses should derive this method
45 // to write their own data. Call this base method from the subclass to write out
46 // this member's comments/annotations.
Adam Lesinskid5083f62017-01-16 15:07:21 -080047 virtual void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080048 std::ostream* out) const;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070049
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 AnnotationProcessor processor_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070052};
53
54template <typename T>
55class PrimitiveMember : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -080057 PrimitiveMember(const android::StringPiece& name, const T& val)
58 : name_(name.to_string()), val_(val) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070059
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 bool empty() const override { return false; }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070061
Adam Lesinskid5083f62017-01-16 15:07:21 -080062 void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 std::ostream* out) const override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 ClassMember::WriteToStream(prefix, final, out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070065
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 *out << prefix << "public static " << (final ? "final " : "") << "int "
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 << name_ << "=" << val_ << ";";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070069
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 std::string name_;
72 T val_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070073
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070075};
76
77/**
78 * Specialization for strings so they get the right type and are quoted with "".
79 */
80template <>
81class PrimitiveMember<std::string> : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -080083 PrimitiveMember(const android::StringPiece& name, const std::string& val)
84 : name_(name.to_string()), val_(val) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070085
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 bool empty() const override { return false; }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070087
Adam Lesinskid5083f62017-01-16 15:07:21 -080088 void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 std::ostream* out) const override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 ClassMember::WriteToStream(prefix, final, out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070091
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 *out << prefix << "public static " << (final ? "final " : "") << "String "
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 << name_ << "=\"" << val_ << "\";";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070095
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 std::string name_;
98 std::string val_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070099
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700101};
102
103using IntMember = PrimitiveMember<uint32_t>;
104using ResourceMember = PrimitiveMember<ResourceId>;
105using StringMember = PrimitiveMember<std::string>;
106
107template <typename T>
108class PrimitiveArrayMember : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -0800110 explicit PrimitiveArrayMember(const android::StringPiece& name) : name_(name.to_string()) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 void AddElement(const T& val) { elements_.push_back(val); }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113
114 bool empty() const override { return false; }
115
Adam Lesinskid5083f62017-01-16 15:07:21 -0800116 void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 std::ostream* out) const override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 ClassMember::WriteToStream(prefix, final, out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 *out << prefix << "public static final int[] " << name_ << "={";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 const auto begin = elements_.begin();
123 const auto end = elements_.end();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 for (auto current = begin; current != end; ++current) {
125 if (std::distance(begin, current) % kAttribsPerLine == 0) {
126 *out << "\n" << prefix << kIndent << kIndent;
127 }
128
129 *out << *current;
130 if (std::distance(current, end) > 1) {
131 *out << ", ";
132 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700133 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 *out << "\n" << prefix << kIndent << "};";
135 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700136
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 std::string name_;
139 std::vector<T> elements_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700140
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 DISALLOW_COPY_AND_ASSIGN(PrimitiveArrayMember);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700142};
143
144using ResourceArrayMember = PrimitiveArrayMember<ResourceId>;
145
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800146// Represents a method in a class.
147class MethodDefinition : public ClassMember {
148 public:
149 // Expected method signature example: 'public static void onResourcesLoaded(int p)'.
150 explicit MethodDefinition(const android::StringPiece& signature)
151 : signature_(signature.to_string()) {}
152
153 // Appends a single statement to the method. It should include no newlines or else
154 // formatting may be broken.
155 void AppendStatement(const android::StringPiece& statement);
156
157 // Even if the method is empty, we always want to write the method signature.
158 bool empty() const override { return false; }
159
160 void WriteToStream(const android::StringPiece& prefix, bool final,
161 std::ostream* out) const override;
162
163 private:
164 std::string signature_;
165 std::vector<std::string> statements_;
166};
167
168enum class ClassQualifier { kNone, kStatic };
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700169
170class ClassDefinition : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -0800172 static bool WriteJavaFile(const ClassDefinition* def, const android::StringPiece& package,
173 bool final, std::ostream* out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700174
Adam Lesinskid5083f62017-01-16 15:07:21 -0800175 ClassDefinition(const android::StringPiece& name, ClassQualifier qualifier, bool createIfEmpty)
176 : name_(name.to_string()), qualifier_(qualifier), create_if_empty_(createIfEmpty) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700177
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 void AddMember(std::unique_ptr<ClassMember> member) {
179 members_.push_back(std::move(member));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700181
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 bool empty() const override;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800183 void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 std::ostream* out) const override;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700185
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 std::string name_;
188 ClassQualifier qualifier_;
189 bool create_if_empty_;
190 std::vector<std::unique_ptr<ClassMember>> members_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700191
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 DISALLOW_COPY_AND_ASSIGN(ClassDefinition);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700193};
194
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195} // namespace aapt
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700196
197#endif /* AAPT_JAVA_CLASSDEFINITION_H */