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 <ostream> |
| 21 | #include <string> |
| 22 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 23 | #include "androidfw/StringPiece.h" |
| 24 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 25 | #include "ResourceTable.h" |
| 26 | #include "ResourceValues.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 | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 29 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 30 | namespace aapt { |
| 31 | |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 32 | class AnnotationProcessor; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 33 | class ClassDefinition; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 34 | class MethodDefinition; |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 35 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 36 | struct JavaClassGeneratorOptions { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 37 | // 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] | 38 | bool use_final = true; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 39 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 40 | // Whether to generate code to rewrite the package ID of resources. |
| 41 | // Implies use_final == true. Default is false. |
| 42 | bool generate_rewrite_callback = false; |
| 43 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 44 | enum class SymbolTypes { |
| 45 | kAll, |
| 46 | kPublicPrivate, |
| 47 | kPublic, |
| 48 | }; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 49 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 50 | SymbolTypes types = SymbolTypes::kAll; |
Adam Lesinski | 3524a23 | 2016-04-01 19:19:24 -0700 | [diff] [blame] | 51 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 52 | // 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] | 53 | std::vector<std::string> javadoc_annotations; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 56 | // Generates the R.java file for a resource table. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 57 | class JavaClassGenerator { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 58 | public: |
| 59 | JavaClassGenerator(IAaptContext* context, ResourceTable* table, |
| 60 | const JavaClassGeneratorOptions& options); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 61 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 62 | // Writes the R.java file to `out`. Only symbols belonging to `package` are written. |
| 63 | // All symbols technically belong to a single package, but linked libraries will |
| 64 | // have their names mangled, denoting that they came from a different package. |
| 65 | // We need to generate these symbols in a separate file. Returns true on success. |
| 66 | bool Generate(const android::StringPiece& package_name_to_generate, std::ostream* out); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 67 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 68 | bool Generate(const android::StringPiece& package_name_to_generate, |
| 69 | const android::StringPiece& output_package_name, std::ostream* out); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 70 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 71 | const std::string& getError() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 72 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 74 | bool SkipSymbol(SymbolState state); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 75 | bool SkipSymbol(const Maybe<SymbolTable::Symbol>& symbol); |
| 76 | |
| 77 | // Returns the unmangled resource entry name if the unmangled package is the same as |
| 78 | // package_name_to_generate. Returns nothing if the resource should be skipped. |
| 79 | Maybe<std::string> UnmangleResource(const android::StringPiece& package_name, |
| 80 | const android::StringPiece& package_name_to_generate, |
| 81 | const ResourceEntry& entry); |
| 82 | |
| 83 | bool ProcessType(const android::StringPiece& package_name_to_generate, |
| 84 | const ResourceTablePackage& package, const ResourceTableType& type, |
| 85 | ClassDefinition* out_type_class_def, MethodDefinition* out_rewrite_method_def); |
| 86 | |
| 87 | // Writes a resource to the R.java file, optionally writing out a rewrite rule for its package |
| 88 | // ID if `out_rewrite_method` is not nullptr. |
| 89 | void ProcessResource(const ResourceNameRef& name, const ResourceId& id, |
| 90 | const ResourceEntry& entry, ClassDefinition* out_class_def, |
| 91 | MethodDefinition* out_rewrite_method); |
| 92 | |
| 93 | // Writes a styleable resource to the R.java file, optionally writing out a rewrite rule for |
| 94 | // its package ID if `out_rewrite_method` is not nullptr. |
| 95 | // `package_name_to_generate` is the package |
| 96 | void ProcessStyleable(const ResourceNameRef& name, const ResourceId& id, |
| 97 | const Styleable& styleable, |
| 98 | const android::StringPiece& package_name_to_generate, |
| 99 | ClassDefinition* out_class_def, MethodDefinition* out_rewrite_method); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 100 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 101 | IAaptContext* context_; |
| 102 | ResourceTable* table_; |
| 103 | JavaClassGeneratorOptions options_; |
| 104 | std::string error_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | inline const std::string& JavaClassGenerator::getError() const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 108 | return error_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 111 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 112 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | #endif // AAPT_JAVA_CLASS_GENERATOR_H |