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