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 | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 28 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 29 | namespace aapt { |
| 30 | |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 31 | class AnnotationProcessor; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 32 | class ClassDefinition; |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 33 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 34 | struct JavaClassGeneratorOptions { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 35 | /* |
| 36 | * Specifies whether to use the 'final' modifier |
| 37 | * on resource entries. Default is true. |
| 38 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | bool use_final = true; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 40 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 41 | enum class SymbolTypes { |
| 42 | kAll, |
| 43 | kPublicPrivate, |
| 44 | kPublic, |
| 45 | }; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 46 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 47 | SymbolTypes types = SymbolTypes::kAll; |
Adam Lesinski | 3524a23 | 2016-04-01 19:19:24 -0700 | [diff] [blame] | 48 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 49 | /** |
| 50 | * A list of JavaDoc annotations to add to the comments of all generated |
| 51 | * classes. |
| 52 | */ |
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 | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 56 | /* |
| 57 | * Generates the R.java file for a resource table. |
| 58 | */ |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 59 | class JavaClassGenerator { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 60 | public: |
| 61 | JavaClassGenerator(IAaptContext* context, ResourceTable* table, |
| 62 | const JavaClassGeneratorOptions& options); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 63 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | /* |
| 65 | * Writes the R.java file to `out`. Only symbols belonging to `package` are |
| 66 | * written. |
| 67 | * All symbols technically belong to a single package, but linked libraries |
| 68 | * will |
| 69 | * have their names mangled, denoting that they came from a different package. |
| 70 | * We need to generate these symbols in a separate file. |
| 71 | * Returns true on success. |
| 72 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 73 | bool Generate(const android::StringPiece& packageNameToGenerate, std::ostream* out); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 74 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 75 | bool Generate(const android::StringPiece& packageNameToGenerate, |
| 76 | const android::StringPiece& outputPackageName, std::ostream* out); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 77 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 78 | const std::string& getError() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 79 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 80 | private: |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 81 | bool AddMembersToTypeClass(const android::StringPiece& packageNameToGenerate, |
| 82 | const ResourceTablePackage* package, const ResourceTableType* type, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 83 | ClassDefinition* outTypeClassDef); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 84 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 85 | void AddMembersToStyleableClass(const android::StringPiece& packageNameToGenerate, |
| 86 | const std::string& entryName, const Styleable* styleable, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 87 | ClassDefinition* outStyleableClassDef); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 88 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 89 | bool SkipSymbol(SymbolState state); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 90 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 91 | IAaptContext* context_; |
| 92 | ResourceTable* table_; |
| 93 | JavaClassGeneratorOptions options_; |
| 94 | std::string error_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | inline const std::string& JavaClassGenerator::getError() const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | return error_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 101 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 102 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 103 | #endif // AAPT_JAVA_CLASS_GENERATOR_H |