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. |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 53 | virtual void Print(bool final, text::Printer* printer) 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 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 73 | void Print(bool final, text::Printer* printer) const override { |
| 74 | using std::to_string; |
| 75 | |
| 76 | ClassMember::Print(final, printer); |
| 77 | |
| 78 | printer->Print("public static "); |
| 79 | if (final) { |
| 80 | printer->Print("final "); |
| 81 | } |
| 82 | printer->Print("int ").Print(name_).Print("=").Print(to_string(val_)).Print(";"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 83 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 84 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 85 | private: |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 86 | DISALLOW_COPY_AND_ASSIGN(PrimitiveMember); |
| 87 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 88 | std::string name_; |
| 89 | T val_; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 92 | // 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] | 93 | template <> |
| 94 | class PrimitiveMember<std::string> : public ClassMember { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | public: |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 96 | PrimitiveMember(const android::StringPiece& name, const std::string& val) |
| 97 | : name_(name.to_string()), val_(val) {} |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 98 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 99 | bool empty() const override { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | const std::string& GetName() const override { |
| 104 | return name_; |
| 105 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 106 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 107 | void Print(bool final, text::Printer* printer) const override { |
| 108 | ClassMember::Print(final, printer); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 109 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 110 | printer->Print("public static "); |
| 111 | if (final) { |
| 112 | printer->Print("final "); |
| 113 | } |
| 114 | printer->Print("String ").Print(name_).Print("=\"").Print(val_).Print("\";"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 116 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | private: |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 118 | DISALLOW_COPY_AND_ASSIGN(PrimitiveMember); |
| 119 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 120 | std::string name_; |
| 121 | std::string val_; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | using IntMember = PrimitiveMember<uint32_t>; |
| 125 | using ResourceMember = PrimitiveMember<ResourceId>; |
| 126 | using StringMember = PrimitiveMember<std::string>; |
| 127 | |
| 128 | template <typename T> |
| 129 | class PrimitiveArrayMember : public ClassMember { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 130 | public: |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 131 | explicit PrimitiveArrayMember(const android::StringPiece& name) : name_(name.to_string()) {} |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 132 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 133 | void AddElement(const T& val) { |
| 134 | elements_.push_back(val); |
| 135 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 136 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 137 | bool empty() const override { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | const std::string& GetName() const override { |
| 142 | return name_; |
| 143 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 144 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 145 | void Print(bool final, text::Printer* printer) const override { |
| 146 | ClassMember::Print(final, printer); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 147 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 148 | printer->Print("public static final int[] ").Print(name_).Print("={"); |
| 149 | printer->Indent(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 150 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 151 | const auto begin = elements_.begin(); |
| 152 | const auto end = elements_.end(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 153 | for (auto current = begin; current != end; ++current) { |
| 154 | if (std::distance(begin, current) % kAttribsPerLine == 0) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 155 | printer->Println(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 158 | printer->Print(to_string(*current)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 159 | if (std::distance(current, end) > 1) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 160 | printer->Print(", "); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 161 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 162 | } |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 163 | printer->Println(); |
| 164 | printer->Undent(); |
| 165 | printer->Print("};"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 166 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 167 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 168 | private: |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 169 | DISALLOW_COPY_AND_ASSIGN(PrimitiveArrayMember); |
| 170 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 171 | std::string name_; |
| 172 | std::vector<T> elements_; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | using ResourceArrayMember = PrimitiveArrayMember<ResourceId>; |
| 176 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 177 | // Represents a method in a class. |
| 178 | class MethodDefinition : public ClassMember { |
| 179 | public: |
| 180 | // Expected method signature example: 'public static void onResourcesLoaded(int p)'. |
| 181 | explicit MethodDefinition(const android::StringPiece& signature) |
| 182 | : signature_(signature.to_string()) {} |
| 183 | |
| 184 | // Appends a single statement to the method. It should include no newlines or else |
| 185 | // formatting may be broken. |
| 186 | void AppendStatement(const android::StringPiece& statement); |
| 187 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 188 | // Not quite the same as a name, but good enough. |
| 189 | const std::string& GetName() const override { |
| 190 | return signature_; |
| 191 | } |
| 192 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 193 | // 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] | 194 | bool empty() const override { |
| 195 | return false; |
| 196 | } |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 197 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 198 | void Print(bool final, text::Printer* printer) const override; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 199 | |
| 200 | private: |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 201 | DISALLOW_COPY_AND_ASSIGN(MethodDefinition); |
| 202 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 203 | std::string signature_; |
| 204 | std::vector<std::string> statements_; |
| 205 | }; |
| 206 | |
| 207 | enum class ClassQualifier { kNone, kStatic }; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 208 | |
| 209 | class ClassDefinition : public ClassMember { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 210 | public: |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 211 | static void WriteJavaFile(const ClassDefinition* def, const android::StringPiece& package, |
| 212 | bool final, io::OutputStream* out); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 213 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 214 | ClassDefinition(const android::StringPiece& name, ClassQualifier qualifier, bool createIfEmpty) |
| 215 | : name_(name.to_string()), qualifier_(qualifier), create_if_empty_(createIfEmpty) {} |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 216 | |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 217 | enum class Result { |
| 218 | kAdded, |
| 219 | kOverridden, |
| 220 | }; |
| 221 | |
| 222 | Result AddMember(std::unique_ptr<ClassMember> member); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 223 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 224 | bool empty() const override; |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 225 | |
| 226 | const std::string& GetName() const override { |
| 227 | return name_; |
| 228 | } |
| 229 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame^] | 230 | void Print(bool final, text::Printer* printer) const override; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 231 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 232 | private: |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 233 | DISALLOW_COPY_AND_ASSIGN(ClassDefinition); |
Adam Lesinski | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame] | 234 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 235 | std::string name_; |
| 236 | ClassQualifier qualifier_; |
| 237 | bool create_if_empty_; |
Adam Lesinski | 761d434 | 2017-09-29 11:15:17 -0700 | [diff] [blame] | 238 | std::vector<std::unique_ptr<ClassMember>> ordered_members_; |
| 239 | std::unordered_map<android::StringPiece, size_t> indexed_members_; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 240 | }; |
| 241 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 242 | } // namespace aapt |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 243 | |
| 244 | #endif /* AAPT_JAVA_CLASSDEFINITION_H */ |