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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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: |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 62 | PrimitiveMember(android::StringPiece name, const T& val, bool staged_api = false) |
| 63 | : name_(name), val_(val), staged_api_(staged_api) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 64 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 65 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 66 | bool empty() const override { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | const std::string& GetName() const override { |
| 71 | return name_; |
| 72 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 73 | |
Ryan Mitchell | 5855de7 | 2021-02-24 14:39:13 -0800 | [diff] [blame] | 74 | void Print(bool final, text::Printer* printer, |
| 75 | bool strip_api_annotations = false) const override { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 76 | using std::to_string; |
| 77 | |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame] | 78 | ClassMember::Print(final, printer, strip_api_annotations); |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 79 | |
| 80 | printer->Print("public static "); |
Ryan Mitchell | ca3b4f7 | 2021-03-29 14:47:02 -0700 | [diff] [blame] | 81 | if (final) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 82 | printer->Print("final "); |
| 83 | } |
Ryan Mitchell | ca3b4f7 | 2021-03-29 14:47:02 -0700 | [diff] [blame] | 84 | printer->Print("int ").Print(name_); |
| 85 | if (staged_api_) { |
| 86 | // Prevent references to staged apis from being inline by setting their value out-of-line. |
| 87 | printer->Print("; static { ").Print(name_); |
| 88 | } |
| 89 | printer->Print("=").Print(to_string(val_)).Print(";"); |
| 90 | if (staged_api_) { |
| 91 | printer->Print(" }"); |
| 92 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 93 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 94 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | private: |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 96 | DISALLOW_COPY_AND_ASSIGN(PrimitiveMember); |
| 97 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | std::string name_; |
| 99 | T val_; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 100 | bool staged_api_; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 103 | // 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] | 104 | template <> |
| 105 | class PrimitiveMember<std::string> : public ClassMember { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 106 | public: |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 107 | PrimitiveMember(android::StringPiece name, const std::string& val, bool staged_api = false) |
| 108 | : name_(name), val_(val) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 109 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 110 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 111 | bool empty() const override { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | const std::string& GetName() const override { |
| 116 | return name_; |
| 117 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 118 | |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame] | 119 | void Print(bool final, text::Printer* printer, bool strip_api_annotations = false) |
| 120 | const override { |
| 121 | ClassMember::Print(final, printer, strip_api_annotations); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 122 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 123 | printer->Print("public static "); |
| 124 | if (final) { |
| 125 | printer->Print("final "); |
| 126 | } |
| 127 | printer->Print("String ").Print(name_).Print("=\"").Print(val_).Print("\";"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 128 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 129 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 130 | private: |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 131 | DISALLOW_COPY_AND_ASSIGN(PrimitiveMember); |
| 132 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 133 | std::string name_; |
| 134 | std::string val_; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | using IntMember = PrimitiveMember<uint32_t>; |
| 138 | using ResourceMember = PrimitiveMember<ResourceId>; |
| 139 | using StringMember = PrimitiveMember<std::string>; |
| 140 | |
Ryan Mitchell | 5855de7 | 2021-02-24 14:39:13 -0800 | [diff] [blame] | 141 | template <typename T, typename StringConverter> |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 142 | class PrimitiveArrayMember : public ClassMember { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 143 | public: |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 144 | explicit PrimitiveArrayMember(android::StringPiece name) : name_(name) { |
| 145 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 146 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 147 | void AddElement(const T& val) { |
Ryan Mitchell | 5855de7 | 2021-02-24 14:39:13 -0800 | [diff] [blame] | 148 | elements_.emplace_back(val); |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 149 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 150 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 151 | bool empty() const override { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | const std::string& GetName() const override { |
| 156 | return name_; |
| 157 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 158 | |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame] | 159 | void Print(bool final, text::Printer* printer, bool strip_api_annotations = false) |
| 160 | const override { |
| 161 | ClassMember::Print(final, printer, strip_api_annotations); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 162 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 163 | printer->Print("public static final int[] ").Print(name_).Print("={"); |
| 164 | printer->Indent(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 165 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 166 | const auto begin = elements_.begin(); |
| 167 | const auto end = elements_.end(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 168 | for (auto current = begin; current != end; ++current) { |
| 169 | if (std::distance(begin, current) % kAttribsPerLine == 0) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 170 | printer->Println(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Ryan Mitchell | 5855de7 | 2021-02-24 14:39:13 -0800 | [diff] [blame] | 173 | printer->Print(StringConverter::ToString(*current)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 174 | if (std::distance(current, end) > 1) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 175 | printer->Print(", "); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 176 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 177 | } |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 178 | printer->Println(); |
| 179 | printer->Undent(); |
| 180 | printer->Print("};"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 181 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 182 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 183 | private: |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 184 | DISALLOW_COPY_AND_ASSIGN(PrimitiveArrayMember); |
| 185 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 186 | std::string name_; |
| 187 | std::vector<T> elements_; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
Ryan Mitchell | 5855de7 | 2021-02-24 14:39:13 -0800 | [diff] [blame] | 190 | struct FieldReference { |
| 191 | explicit FieldReference(std::string reference) : ref(std::move(reference)) { |
| 192 | } |
| 193 | std::string ref; |
| 194 | }; |
| 195 | |
| 196 | struct ResourceArrayMemberStringConverter { |
| 197 | static std::string ToString(const std::variant<ResourceId, FieldReference>& ref) { |
| 198 | if (auto id = std::get_if<ResourceId>(&ref)) { |
| 199 | return to_string(*id); |
| 200 | } else { |
| 201 | return std::get<FieldReference>(ref).ref; |
| 202 | } |
| 203 | } |
| 204 | }; |
| 205 | |
| 206 | using ResourceArrayMember = PrimitiveArrayMember<std::variant<ResourceId, FieldReference>, |
| 207 | ResourceArrayMemberStringConverter>; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 208 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 209 | // Represents a method in a class. |
| 210 | class MethodDefinition : public ClassMember { |
| 211 | public: |
| 212 | // Expected method signature example: 'public static void onResourcesLoaded(int p)'. |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 213 | explicit MethodDefinition(android::StringPiece signature) : signature_(signature) { |
| 214 | } |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 215 | |
| 216 | // Appends a single statement to the method. It should include no newlines or else |
| 217 | // formatting may be broken. |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 218 | void AppendStatement(android::StringPiece statement); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 219 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 220 | // Not quite the same as a name, but good enough. |
| 221 | const std::string& GetName() const override { |
| 222 | return signature_; |
| 223 | } |
| 224 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 225 | // 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] | 226 | bool empty() const override { |
| 227 | return false; |
| 228 | } |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 229 | |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame] | 230 | 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] | 231 | |
| 232 | private: |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 233 | DISALLOW_COPY_AND_ASSIGN(MethodDefinition); |
| 234 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 235 | std::string signature_; |
| 236 | std::vector<std::string> statements_; |
| 237 | }; |
| 238 | |
| 239 | enum class ClassQualifier { kNone, kStatic }; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 240 | |
| 241 | class ClassDefinition : public ClassMember { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 242 | public: |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 243 | static void WriteJavaFile(const ClassDefinition* def, android::StringPiece package, bool final, |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 244 | bool strip_api_annotations, android::OutputStream* out); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 245 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 246 | ClassDefinition(android::StringPiece name, ClassQualifier qualifier, bool createIfEmpty) |
| 247 | : name_(name), qualifier_(qualifier), create_if_empty_(createIfEmpty) { |
| 248 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 249 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 250 | enum class Result { |
| 251 | kAdded, |
| 252 | kOverridden, |
| 253 | }; |
| 254 | |
| 255 | Result AddMember(std::unique_ptr<ClassMember> member); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 256 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 257 | bool empty() const override; |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 258 | |
| 259 | const std::string& GetName() const override { |
| 260 | return name_; |
| 261 | } |
| 262 | |
Makoto Onuki | de6e6f2 | 2020-06-22 10:17:02 -0700 | [diff] [blame] | 263 | 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] | 264 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 265 | private: |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 266 | DISALLOW_COPY_AND_ASSIGN(ClassDefinition); |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 267 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 268 | std::string name_; |
| 269 | ClassQualifier qualifier_; |
| 270 | bool create_if_empty_; |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 271 | std::vector<std::unique_ptr<ClassMember>> ordered_members_; |
| 272 | std::unordered_map<android::StringPiece, size_t> indexed_members_; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 273 | }; |
| 274 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 275 | } // namespace aapt |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 276 | |
| 277 | #endif /* AAPT_JAVA_CLASSDEFINITION_H */ |