blob: bd7e7b277fe54a1a924200069a354baab3b38aab [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
20#include "Resource.h"
21#include "java/AnnotationProcessor.h"
22#include "util/StringPiece.h"
23#include "util/Util.h"
24
25#include <android-base/macros.h>
26#include <sstream>
27#include <string>
28
29namespace aapt {
30
31// The number of attributes to emit per line in a Styleable array.
32constexpr static size_t kAttribsPerLine = 4;
33constexpr static const char* kIndent = " ";
34
35class ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 public:
37 virtual ~ClassMember() = default;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070038
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 AnnotationProcessor* getCommentBuilder() { return &mProcessor; }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070040
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 virtual bool empty() const = 0;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070042
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 virtual void writeToStream(const StringPiece& prefix, bool final,
44 std::ostream* out) const {
45 mProcessor.writeToStream(out, prefix);
46 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070047
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 private:
49 AnnotationProcessor mProcessor;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070050};
51
52template <typename T>
53class PrimitiveMember : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 public:
55 PrimitiveMember(const StringPiece& name, const T& val)
56 : mName(name.toString()), mVal(val) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070057
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 bool empty() const override { return false; }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070059
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 void writeToStream(const StringPiece& prefix, bool final,
61 std::ostream* out) const override {
62 ClassMember::writeToStream(prefix, final, out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070063
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 *out << prefix << "public static " << (final ? "final " : "") << "int "
65 << mName << "=" << mVal << ";";
66 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070067
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 private:
69 std::string mName;
70 T mVal;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070071
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070073};
74
75/**
76 * Specialization for strings so they get the right type and are quoted with "".
77 */
78template <>
79class PrimitiveMember<std::string> : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 public:
81 PrimitiveMember(const StringPiece& name, const std::string& val)
82 : mName(name.toString()), mVal(val) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070083
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 bool empty() const override { return false; }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070085
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 void writeToStream(const StringPiece& prefix, bool final,
87 std::ostream* out) const override {
88 ClassMember::writeToStream(prefix, final, out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070089
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 *out << prefix << "public static " << (final ? "final " : "") << "String "
91 << mName << "=\"" << mVal << "\";";
92 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070093
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 private:
95 std::string mName;
96 std::string mVal;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070097
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070099};
100
101using IntMember = PrimitiveMember<uint32_t>;
102using ResourceMember = PrimitiveMember<ResourceId>;
103using StringMember = PrimitiveMember<std::string>;
104
105template <typename T>
106class PrimitiveArrayMember : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 public:
108 explicit PrimitiveArrayMember(const StringPiece& name)
109 : mName(name.toString()) {}
110
111 void addElement(const T& val) { mElements.push_back(val); }
112
113 bool empty() const override { return false; }
114
115 void writeToStream(const StringPiece& prefix, bool final,
116 std::ostream* out) const override {
117 ClassMember::writeToStream(prefix, final, out);
118
119 *out << prefix << "public static final int[] " << mName << "={";
120
121 const auto begin = mElements.begin();
122 const auto end = mElements.end();
123 for (auto current = begin; current != end; ++current) {
124 if (std::distance(begin, current) % kAttribsPerLine == 0) {
125 *out << "\n" << prefix << kIndent << kIndent;
126 }
127
128 *out << *current;
129 if (std::distance(current, end) > 1) {
130 *out << ", ";
131 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700132 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 *out << "\n" << prefix << kIndent << "};";
134 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700135
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 private:
137 std::string mName;
138 std::vector<T> mElements;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700139
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 DISALLOW_COPY_AND_ASSIGN(PrimitiveArrayMember);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700141};
142
143using ResourceArrayMember = PrimitiveArrayMember<ResourceId>;
144
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145enum class ClassQualifier { None, Static };
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700146
147class ClassDefinition : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 public:
149 static bool writeJavaFile(const ClassDefinition* def,
150 const StringPiece& package, bool final,
151 std::ostream* out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700152
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 ClassDefinition(const StringPiece& name, ClassQualifier qualifier,
154 bool createIfEmpty)
155 : mName(name.toString()),
156 mQualifier(qualifier),
157 mCreateIfEmpty(createIfEmpty) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700158
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 void addMember(std::unique_ptr<ClassMember> member) {
160 mMembers.push_back(std::move(member));
161 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700162
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 bool empty() const override;
164 void writeToStream(const StringPiece& prefix, bool final,
165 std::ostream* out) const override;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700166
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 private:
168 std::string mName;
169 ClassQualifier mQualifier;
170 bool mCreateIfEmpty;
171 std::vector<std::unique_ptr<ClassMember>> mMembers;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700172
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 DISALLOW_COPY_AND_ASSIGN(ClassDefinition);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700174};
175
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176} // namespace aapt
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700177
178#endif /* AAPT_JAVA_CLASSDEFINITION_H */