blob: 5cf556ea570769c9b35c3a44800d3195e1724753 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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 Lesinskice5e56e2016-10-21 17:56:45 -070020#include <ostream>
21#include <string>
22
Adam Lesinskid5083f62017-01-16 15:07:21 -080023#include "androidfw/StringPiece.h"
24
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080025#include "ResourceTable.h"
26#include "ResourceValues.h"
Adam Lesinski76565542016-03-10 21:55:04 -080027#include "process/IResourceTableConsumer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080029namespace aapt {
30
Adam Lesinskib274e352015-11-06 15:14:35 -080031class AnnotationProcessor;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070032class ClassDefinition;
Adam Lesinskib274e352015-11-06 15:14:35 -080033
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034struct JavaClassGeneratorOptions {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 /*
36 * Specifies whether to use the 'final' modifier
37 * on resource entries. Default is true.
38 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 bool use_final = true;
Adam Lesinski9e10ac72015-10-16 14:37:48 -070040
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 enum class SymbolTypes {
42 kAll,
43 kPublicPrivate,
44 kPublic,
45 };
Adam Lesinski9e10ac72015-10-16 14:37:48 -070046
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 SymbolTypes types = SymbolTypes::kAll;
Adam Lesinski3524a232016-04-01 19:19:24 -070048
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 /**
50 * A list of JavaDoc annotations to add to the comments of all generated
51 * classes.
52 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 std::vector<std::string> javadoc_annotations;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054};
55
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056/*
57 * Generates the R.java file for a resource table.
58 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059class JavaClassGenerator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 public:
61 JavaClassGenerator(IAaptContext* context, ResourceTable* table,
62 const JavaClassGeneratorOptions& options);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080063
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 /*
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 Lesinskid5083f62017-01-16 15:07:21 -080073 bool Generate(const android::StringPiece& packageNameToGenerate, std::ostream* out);
Adam Lesinski9e10ac72015-10-16 14:37:48 -070074
Adam Lesinskid5083f62017-01-16 15:07:21 -080075 bool Generate(const android::StringPiece& packageNameToGenerate,
76 const android::StringPiece& outputPackageName, std::ostream* out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 const std::string& getError() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080079
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 private:
Adam Lesinskid5083f62017-01-16 15:07:21 -080081 bool AddMembersToTypeClass(const android::StringPiece& packageNameToGenerate,
82 const ResourceTablePackage* package, const ResourceTableType* type,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 ClassDefinition* outTypeClassDef);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084
Adam Lesinskid5083f62017-01-16 15:07:21 -080085 void AddMembersToStyleableClass(const android::StringPiece& packageNameToGenerate,
86 const std::string& entryName, const Styleable* styleable,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 ClassDefinition* outStyleableClassDef);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070088
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 bool SkipSymbol(SymbolState state);
Adam Lesinski9e10ac72015-10-16 14:37:48 -070090
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 IAaptContext* context_;
92 ResourceTable* table_;
93 JavaClassGeneratorOptions options_;
94 std::string error_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095};
96
97inline const std::string& JavaClassGenerator::getError() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 return error_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080099}
100
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800102
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103#endif // AAPT_JAVA_CLASS_GENERATOR_H