blob: 64e4b2987be32214bd2c9a33f64cbf6ea798b032 [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 Lesinskid5083f62017-01-16 15:07:21 -080044 virtual void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 std::ostream* out) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 processor_.WriteToStream(out, prefix);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070048
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 AnnotationProcessor processor_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070051};
52
53template <typename T>
54class PrimitiveMember : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -080056 PrimitiveMember(const android::StringPiece& name, const T& val)
57 : name_(name.to_string()), val_(val) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070058
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 bool empty() const override { return false; }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070060
Adam Lesinskid5083f62017-01-16 15:07:21 -080061 void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 std::ostream* out) const override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 ClassMember::WriteToStream(prefix, final, out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070064
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 *out << prefix << "public static " << (final ? "final " : "") << "int "
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 << name_ << "=" << val_ << ";";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070068
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 std::string name_;
71 T val_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070072
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070074};
75
76/**
77 * Specialization for strings so they get the right type and are quoted with "".
78 */
79template <>
80class PrimitiveMember<std::string> : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -080082 PrimitiveMember(const android::StringPiece& name, const std::string& val)
83 : name_(name.to_string()), val_(val) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070084
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 bool empty() const override { return false; }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070086
Adam Lesinskid5083f62017-01-16 15:07:21 -080087 void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 std::ostream* out) const override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 ClassMember::WriteToStream(prefix, final, out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070090
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 *out << prefix << "public static " << (final ? "final " : "") << "String "
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 << name_ << "=\"" << val_ << "\";";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070094
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 std::string name_;
97 std::string val_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070098
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700100};
101
102using IntMember = PrimitiveMember<uint32_t>;
103using ResourceMember = PrimitiveMember<ResourceId>;
104using StringMember = PrimitiveMember<std::string>;
105
106template <typename T>
107class PrimitiveArrayMember : public ClassMember {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -0800109 explicit PrimitiveArrayMember(const android::StringPiece& name) : name_(name.to_string()) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 void AddElement(const T& val) { elements_.push_back(val); }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112
113 bool empty() const override { return false; }
114
Adam Lesinskid5083f62017-01-16 15:07:21 -0800115 void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 std::ostream* out) const override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 ClassMember::WriteToStream(prefix, final, out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 *out << prefix << "public static final int[] " << name_ << "={";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 const auto begin = elements_.begin();
122 const auto end = elements_.end();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 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:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 std::string name_;
138 std::vector<T> elements_;
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:
Adam Lesinskid5083f62017-01-16 15:07:21 -0800149 static bool WriteJavaFile(const ClassDefinition* def, const android::StringPiece& package,
150 bool final, std::ostream* out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700151
Adam Lesinskid5083f62017-01-16 15:07:21 -0800152 ClassDefinition(const android::StringPiece& name, ClassQualifier qualifier, bool createIfEmpty)
153 : name_(name.to_string()), qualifier_(qualifier), create_if_empty_(createIfEmpty) {}
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700154
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 void AddMember(std::unique_ptr<ClassMember> member) {
156 members_.push_back(std::move(member));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700158
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 bool empty() const override;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800160 void WriteToStream(const android::StringPiece& prefix, bool final,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 std::ostream* out) const override;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700162
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 std::string name_;
165 ClassQualifier qualifier_;
166 bool create_if_empty_;
167 std::vector<std::unique_ptr<ClassMember>> members_;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700168
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 DISALLOW_COPY_AND_ASSIGN(ClassDefinition);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700170};
171
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172} // namespace aapt
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700173
174#endif /* AAPT_JAVA_CLASSDEFINITION_H */