Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [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_CLASS_GENERATOR_H |
| 18 | #define AAPT_JAVA_CLASS_GENERATOR_H |
| 19 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 22 | #include "androidfw/StringPiece.h" |
| 23 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 24 | #include "ResourceTable.h" |
| 25 | #include "ResourceValues.h" |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 26 | #include "io/Io.h" |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 27 | #include "process/IResourceTableConsumer.h" |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 28 | #include "process/SymbolTable.h" |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 29 | #include "text/Printer.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 31 | namespace aapt { |
| 32 | |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 33 | class AnnotationProcessor; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 34 | class ClassDefinition; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 35 | class MethodDefinition; |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 36 | |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 37 | // Options for generating onResourcesLoaded callback in R.java. |
| 38 | struct OnResourcesLoadedCallbackOptions { |
| 39 | // Other R classes to delegate the same callback to (with the same package ID). |
| 40 | std::vector<std::string> packages_to_callback; |
| 41 | }; |
| 42 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 43 | struct JavaClassGeneratorOptions { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 44 | // Specifies whether to use the 'final' modifier on resource entries. Default is true. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 45 | bool use_final = true; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 46 | |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 47 | // If set, generates code to rewrite the package ID of resources. |
| 48 | // Implies use_final == true. Default is unset. |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 49 | std::optional<OnResourcesLoadedCallbackOptions> rewrite_callback_options; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 50 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | enum class SymbolTypes { |
| 52 | kAll, |
| 53 | kPublicPrivate, |
| 54 | kPublic, |
| 55 | }; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 56 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 57 | SymbolTypes types = SymbolTypes::kAll; |
Adam Lesinski | 3524a23 | 2016-04-01 19:19:24 -0700 | [diff] [blame] | 58 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 59 | // A list of JavaDoc annotations to add to the comments of all generated classes. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 60 | std::vector<std::string> javadoc_annotations; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 63 | // Generates the R.java file for a resource table and optionally an R.txt file. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 64 | class JavaClassGenerator { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 65 | public: |
| 66 | JavaClassGenerator(IAaptContext* context, ResourceTable* table, |
| 67 | const JavaClassGeneratorOptions& options); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 68 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 69 | // Writes the R.java file to `out`. Only symbols belonging to `package` are written. |
| 70 | // All symbols technically belong to a single package, but linked libraries will |
| 71 | // have their names mangled, denoting that they came from a different package. |
| 72 | // We need to generate these symbols in a separate file. Returns true on success. |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 73 | bool Generate(const android::StringPiece& package_name_to_generate, io::OutputStream* out, |
| 74 | io::OutputStream* out_r_txt = nullptr); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 75 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 76 | bool Generate(const android::StringPiece& package_name_to_generate, |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 77 | const android::StringPiece& output_package_name, io::OutputStream* out, |
| 78 | io::OutputStream* out_r_txt = nullptr); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 79 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 80 | const std::string& GetError() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 81 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 82 | static std::string TransformToFieldName(const android::StringPiece& symbol); |
| 83 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 84 | private: |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 85 | bool SkipSymbol(Visibility::Level state); |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 86 | bool SkipSymbol(const std::optional<SymbolTable::Symbol>& symbol); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 87 | |
| 88 | // Returns the unmangled resource entry name if the unmangled package is the same as |
| 89 | // package_name_to_generate. Returns nothing if the resource should be skipped. |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 90 | std::optional<std::string> UnmangleResource(const android::StringPiece& package_name, |
| 91 | const android::StringPiece& package_name_to_generate, |
| 92 | const ResourceEntry& entry); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 93 | |
| 94 | bool ProcessType(const android::StringPiece& package_name_to_generate, |
| 95 | const ResourceTablePackage& package, const ResourceTableType& type, |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 96 | ClassDefinition* out_type_class_def, MethodDefinition* out_rewrite_method_def, |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 97 | text::Printer* r_txt_printer); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 98 | |
| 99 | // Writes a resource to the R.java file, optionally writing out a rewrite rule for its package |
| 100 | // ID if `out_rewrite_method` is not nullptr. |
| 101 | void ProcessResource(const ResourceNameRef& name, const ResourceId& id, |
| 102 | const ResourceEntry& entry, ClassDefinition* out_class_def, |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 103 | MethodDefinition* out_rewrite_method, text::Printer* r_txt_printer); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 104 | |
| 105 | // Writes a styleable resource to the R.java file, optionally writing out a rewrite rule for |
| 106 | // its package ID if `out_rewrite_method` is not nullptr. |
| 107 | // `package_name_to_generate` is the package |
Ryan Mitchell | 5855de7 | 2021-02-24 14:39:13 -0800 | [diff] [blame] | 108 | bool ProcessStyleable(const ResourceNameRef& name, const ResourceId& id, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 109 | const Styleable& styleable, |
| 110 | const android::StringPiece& package_name_to_generate, |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 111 | ClassDefinition* out_class_def, MethodDefinition* out_rewrite_method, |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 112 | text::Printer* r_txt_printer); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 113 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 114 | IAaptContext* context_; |
| 115 | ResourceTable* table_; |
| 116 | JavaClassGeneratorOptions options_; |
| 117 | std::string error_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 118 | }; |
| 119 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 120 | inline const std::string& JavaClassGenerator::GetError() const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 121 | return error_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 124 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 125 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 126 | #endif // AAPT_JAVA_CLASS_GENERATOR_H |