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 <string> |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 21 | #include <unordered_map> |
| 22 | #include <vector> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 23 | |
| 24 | #include "android-base/macros.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 25 | #include "androidfw/StringPiece.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 27 | #include "Resource.h" |
| 28 | #include "java/AnnotationProcessor.h" |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 29 | #include "text/Printer.h" |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 30 | #include "util/Util.h" |
| 31 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 32 | namespace aapt { |
| 33 | |
| 34 | // The number of attributes to emit per line in a Styleable array. |
| 35 | constexpr static size_t kAttribsPerLine = 4; |
| 36 | constexpr static const char* kIndent = " "; |
| 37 | |
| 38 | class ClassMember { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 39 | public: |
| 40 | virtual ~ClassMember() = default; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 41 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 42 | AnnotationProcessor* GetCommentBuilder() { |
| 43 | return &processor_; |
| 44 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 45 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 46 | virtual bool empty() const = 0; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 47 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 48 | virtual const std::string& GetName() const = 0; |
| 49 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 50 | // Writes the class member to the Printer. Subclasses should derive this method |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 51 | // to write their own data. Call this base method from the subclass to write out |
| 52 | // this member's comments/annotations. |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame] | 53 | virtual void Print(bool final, text::Printer* printer, bool strip_api_annotations = false) const; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 54 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 55 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 56 | AnnotationProcessor processor_; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | template <typename T> |
| 60 | class PrimitiveMember : public ClassMember { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | public: |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 62 | PrimitiveMember(const android::StringPiece& name, const T& val) |
| 63 | : name_(name.to_string()), val_(val) {} |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 64 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 65 | bool empty() const override { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | const std::string& GetName() const override { |
| 70 | return name_; |
| 71 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 72 | |
Ryan Mitchell | 5855de7 | 2021-02-24 14:39:13 -0800 | [diff] [blame^] | 73 | void Print(bool final, text::Printer* printer, |
| 74 | bool strip_api_annotations = false) const override { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 75 | using std::to_string; |
| 76 | |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame] | 77 | ClassMember::Print(final, printer, strip_api_annotations); |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 78 | |
| 79 | printer->Print("public static "); |
| 80 | if (final) { |
| 81 | printer->Print("final "); |
| 82 | } |
| 83 | printer->Print("int ").Print(name_).Print("=").Print(to_string(val_)).Print(";"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 84 | } |
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 | private: |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 87 | DISALLOW_COPY_AND_ASSIGN(PrimitiveMember); |
| 88 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 89 | std::string name_; |
| 90 | T val_; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 93 | // Specialization for strings so they get the right type and are quoted with "". |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 94 | template <> |
| 95 | class PrimitiveMember<std::string> : public ClassMember { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 96 | public: |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 97 | PrimitiveMember(const android::StringPiece& name, const std::string& val) |
| 98 | : name_(name.to_string()), val_(val) {} |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 99 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 100 | bool empty() const override { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | const std::string& GetName() const override { |
| 105 | return name_; |
| 106 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 107 | |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame] | 108 | void Print(bool final, text::Printer* printer, bool strip_api_annotations = false) |
| 109 | const override { |
| 110 | ClassMember::Print(final, printer, strip_api_annotations); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 111 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 112 | printer->Print("public static "); |
| 113 | if (final) { |
| 114 | printer->Print("final "); |
| 115 | } |
| 116 | printer->Print("String ").Print(name_).Print("=\"").Print(val_).Print("\";"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 118 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | private: |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 120 | DISALLOW_COPY_AND_ASSIGN(PrimitiveMember); |
| 121 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 122 | std::string name_; |
| 123 | std::string val_; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | using IntMember = PrimitiveMember<uint32_t>; |
| 127 | using ResourceMember = PrimitiveMember<ResourceId>; |
| 128 | using StringMember = PrimitiveMember<std::string>; |
| 129 | |
Ryan Mitchell | 5855de7 | 2021-02-24 14:39:13 -0800 | [diff] [blame^] | 130 | template <typename T, typename StringConverter> |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 131 | class PrimitiveArrayMember : public ClassMember { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 132 | public: |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 133 | explicit PrimitiveArrayMember(const android::StringPiece& name) : name_(name.to_string()) {} |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 134 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 135 | void AddElement(const T& val) { |
Ryan Mitchell | 5855de7 | 2021-02-24 14:39:13 -0800 | [diff] [blame^] | 136 | elements_.emplace_back(val); |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 137 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 138 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 139 | bool empty() const override { |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | const std::string& GetName() const override { |
| 144 | return name_; |
| 145 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 146 | |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame] | 147 | void Print(bool final, text::Printer* printer, bool strip_api_annotations = false) |
| 148 | const override { |
| 149 | ClassMember::Print(final, printer, strip_api_annotations); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 150 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 151 | printer->Print("public static final int[] ").Print(name_).Print("={"); |
| 152 | printer->Indent(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 153 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 154 | const auto begin = elements_.begin(); |
| 155 | const auto end = elements_.end(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 156 | for (auto current = begin; current != end; ++current) { |
| 157 | if (std::distance(begin, current) % kAttribsPerLine == 0) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 158 | printer->Println(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Ryan Mitchell | 5855de7 | 2021-02-24 14:39:13 -0800 | [diff] [blame^] | 161 | printer->Print(StringConverter::ToString(*current)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 162 | if (std::distance(current, end) > 1) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 163 | printer->Print(", "); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 164 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 165 | } |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 166 | printer->Println(); |
| 167 | printer->Undent(); |
| 168 | printer->Print("};"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 169 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 170 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | private: |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 172 | DISALLOW_COPY_AND_ASSIGN(PrimitiveArrayMember); |
| 173 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 174 | std::string name_; |
| 175 | std::vector<T> elements_; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
Ryan Mitchell | 5855de7 | 2021-02-24 14:39:13 -0800 | [diff] [blame^] | 178 | struct FieldReference { |
| 179 | explicit FieldReference(std::string reference) : ref(std::move(reference)) { |
| 180 | } |
| 181 | std::string ref; |
| 182 | }; |
| 183 | |
| 184 | struct ResourceArrayMemberStringConverter { |
| 185 | static std::string ToString(const std::variant<ResourceId, FieldReference>& ref) { |
| 186 | if (auto id = std::get_if<ResourceId>(&ref)) { |
| 187 | return to_string(*id); |
| 188 | } else { |
| 189 | return std::get<FieldReference>(ref).ref; |
| 190 | } |
| 191 | } |
| 192 | }; |
| 193 | |
| 194 | using ResourceArrayMember = PrimitiveArrayMember<std::variant<ResourceId, FieldReference>, |
| 195 | ResourceArrayMemberStringConverter>; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 196 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 197 | // Represents a method in a class. |
| 198 | class MethodDefinition : public ClassMember { |
| 199 | public: |
| 200 | // Expected method signature example: 'public static void onResourcesLoaded(int p)'. |
| 201 | explicit MethodDefinition(const android::StringPiece& signature) |
| 202 | : signature_(signature.to_string()) {} |
| 203 | |
| 204 | // Appends a single statement to the method. It should include no newlines or else |
| 205 | // formatting may be broken. |
| 206 | void AppendStatement(const android::StringPiece& statement); |
| 207 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 208 | // Not quite the same as a name, but good enough. |
| 209 | const std::string& GetName() const override { |
| 210 | return signature_; |
| 211 | } |
| 212 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 213 | // Even if the method is empty, we always want to write the method signature. |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 214 | bool empty() const override { |
| 215 | return false; |
| 216 | } |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 217 | |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame] | 218 | void Print(bool final, text::Printer* printer, bool strip_api_annotations = false) const override; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 219 | |
| 220 | private: |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 221 | DISALLOW_COPY_AND_ASSIGN(MethodDefinition); |
| 222 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 223 | std::string signature_; |
| 224 | std::vector<std::string> statements_; |
| 225 | }; |
| 226 | |
| 227 | enum class ClassQualifier { kNone, kStatic }; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 228 | |
| 229 | class ClassDefinition : public ClassMember { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 230 | public: |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 231 | static void WriteJavaFile(const ClassDefinition* def, const android::StringPiece& package, |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame] | 232 | bool final, bool strip_api_annotations, io::OutputStream* out); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 233 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 234 | ClassDefinition(const android::StringPiece& name, ClassQualifier qualifier, bool createIfEmpty) |
| 235 | : name_(name.to_string()), qualifier_(qualifier), create_if_empty_(createIfEmpty) {} |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 236 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 237 | enum class Result { |
| 238 | kAdded, |
| 239 | kOverridden, |
| 240 | }; |
| 241 | |
| 242 | Result AddMember(std::unique_ptr<ClassMember> member); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 243 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 244 | bool empty() const override; |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 245 | |
| 246 | const std::string& GetName() const override { |
| 247 | return name_; |
| 248 | } |
| 249 | |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame] | 250 | void Print(bool final, text::Printer* printer, bool strip_api_annotations = false) const override; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 251 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 252 | private: |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 253 | DISALLOW_COPY_AND_ASSIGN(ClassDefinition); |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 254 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 255 | std::string name_; |
| 256 | ClassQualifier qualifier_; |
| 257 | bool create_if_empty_; |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 258 | std::vector<std::unique_ptr<ClassMember>> ordered_members_; |
| 259 | std::unordered_map<android::StringPiece, size_t> indexed_members_; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 260 | }; |
| 261 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 262 | } // namespace aapt |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 263 | |
| 264 | #endif /* AAPT_JAVA_CLASSDEFINITION_H */ |