Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <ostream> |
| 21 | #include <string> |
| 22 | |
| 23 | #include "android-base/macros.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 24 | #include "androidfw/StringPiece.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 25 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 26 | #include "Resource.h" |
| 27 | #include "java/AnnotationProcessor.h" |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 28 | #include "util/Util.h" |
| 29 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 30 | namespace aapt { |
| 31 | |
| 32 | // The number of attributes to emit per line in a Styleable array. |
| 33 | constexpr static size_t kAttribsPerLine = 4; |
| 34 | constexpr static const char* kIndent = " "; |
| 35 | |
| 36 | class ClassMember { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 37 | public: |
| 38 | virtual ~ClassMember() = default; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 39 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 40 | AnnotationProcessor* GetCommentBuilder() { return &processor_; } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 41 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 42 | virtual bool empty() const = 0; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 43 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 44 | // 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 Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 47 | virtual void WriteToStream(const android::StringPiece& prefix, bool final, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 48 | std::ostream* out) const; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 49 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 50 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 51 | AnnotationProcessor processor_; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | template <typename T> |
| 55 | class PrimitiveMember : public ClassMember { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | public: |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 57 | PrimitiveMember(const android::StringPiece& name, const T& val) |
| 58 | : name_(name.to_string()), val_(val) {} |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 59 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 60 | bool empty() const override { return false; } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 61 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 62 | void WriteToStream(const android::StringPiece& prefix, bool final, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | std::ostream* out) const override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 64 | ClassMember::WriteToStream(prefix, final, out); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 65 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | *out << prefix << "public static " << (final ? "final " : "") << "int " |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 67 | << name_ << "=" << val_ << ";"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 68 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 69 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 70 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 71 | std::string name_; |
| 72 | T val_; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 73 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 74 | DISALLOW_COPY_AND_ASSIGN(PrimitiveMember); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | /** |
| 78 | * Specialization for strings so they get the right type and are quoted with "". |
| 79 | */ |
| 80 | template <> |
| 81 | class PrimitiveMember<std::string> : public ClassMember { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | public: |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 83 | PrimitiveMember(const android::StringPiece& name, const std::string& val) |
| 84 | : name_(name.to_string()), val_(val) {} |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 85 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 86 | bool empty() const override { return false; } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 87 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 88 | void WriteToStream(const android::StringPiece& prefix, bool final, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 89 | std::ostream* out) const override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | ClassMember::WriteToStream(prefix, final, out); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 91 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 92 | *out << prefix << "public static " << (final ? "final " : "") << "String " |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 93 | << name_ << "=\"" << val_ << "\";"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 94 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 95 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 96 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 97 | std::string name_; |
| 98 | std::string val_; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 99 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 100 | DISALLOW_COPY_AND_ASSIGN(PrimitiveMember); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | using IntMember = PrimitiveMember<uint32_t>; |
| 104 | using ResourceMember = PrimitiveMember<ResourceId>; |
| 105 | using StringMember = PrimitiveMember<std::string>; |
| 106 | |
| 107 | template <typename T> |
| 108 | class PrimitiveArrayMember : public ClassMember { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 109 | public: |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 110 | explicit PrimitiveArrayMember(const android::StringPiece& name) : name_(name.to_string()) {} |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 111 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | void AddElement(const T& val) { elements_.push_back(val); } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | |
| 114 | bool empty() const override { return false; } |
| 115 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 116 | void WriteToStream(const android::StringPiece& prefix, bool final, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | std::ostream* out) const override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 118 | ClassMember::WriteToStream(prefix, final, out); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 120 | *out << prefix << "public static final int[] " << name_ << "={"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 121 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 122 | const auto begin = elements_.begin(); |
| 123 | const auto end = elements_.end(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 124 | 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 Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 133 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 134 | *out << "\n" << prefix << kIndent << "};"; |
| 135 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 136 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 138 | std::string name_; |
| 139 | std::vector<T> elements_; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 140 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 141 | DISALLOW_COPY_AND_ASSIGN(PrimitiveArrayMember); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | using ResourceArrayMember = PrimitiveArrayMember<ResourceId>; |
| 145 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 146 | // Represents a method in a class. |
| 147 | class 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 | |
| 168 | enum class ClassQualifier { kNone, kStatic }; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 169 | |
| 170 | class ClassDefinition : public ClassMember { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | public: |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 172 | static bool WriteJavaFile(const ClassDefinition* def, const android::StringPiece& package, |
| 173 | bool final, std::ostream* out); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 174 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 175 | ClassDefinition(const android::StringPiece& name, ClassQualifier qualifier, bool createIfEmpty) |
| 176 | : name_(name.to_string()), qualifier_(qualifier), create_if_empty_(createIfEmpty) {} |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 177 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 178 | void AddMember(std::unique_ptr<ClassMember> member) { |
| 179 | members_.push_back(std::move(member)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 180 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 181 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 182 | bool empty() const override; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 183 | void WriteToStream(const android::StringPiece& prefix, bool final, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 184 | std::ostream* out) const override; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 185 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 186 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 187 | std::string name_; |
| 188 | ClassQualifier qualifier_; |
| 189 | bool create_if_empty_; |
| 190 | std::vector<std::unique_ptr<ClassMember>> members_; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 191 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 192 | DISALLOW_COPY_AND_ASSIGN(ClassDefinition); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 193 | }; |
| 194 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 195 | } // namespace aapt |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 196 | |
| 197 | #endif /* AAPT_JAVA_CLASSDEFINITION_H */ |