blob: 7e46f8c9043c3f25b24ea7018941cc8c2cbb0088 [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
20#include "ResourceTable.h"
21#include "ResourceValues.h"
Adam Lesinski76565542016-03-10 21:55:04 -080022#include "process/IResourceTableConsumer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023#include "util/StringPiece.h"
24
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080025#include <ostream>
26#include <string>
27
28namespace aapt {
29
Adam Lesinskib274e352015-11-06 15:14:35 -080030class AnnotationProcessor;
31class ClassDefinitionWriter;
32
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033struct JavaClassGeneratorOptions {
34 /*
35 * Specifies whether to use the 'final' modifier
36 * on resource entries. Default is true.
37 */
38 bool useFinal = true;
Adam Lesinski9e10ac72015-10-16 14:37:48 -070039
40 enum class SymbolTypes {
41 kAll,
42 kPublicPrivate,
43 kPublic,
44 };
45
Adam Lesinski9e10ac72015-10-16 14:37:48 -070046 SymbolTypes types = SymbolTypes::kAll;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047};
48
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049/*
50 * Generates the R.java file for a resource table.
51 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -070052class JavaClassGenerator {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080053public:
Adam Lesinski76565542016-03-10 21:55:04 -080054 JavaClassGenerator(IAaptContext* context, ResourceTable* table,
55 const JavaClassGeneratorOptions& options);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056
57 /*
Adam Lesinski769de982015-04-10 19:43:55 -070058 * Writes the R.java file to `out`. Only symbols belonging to `package` are written.
59 * All symbols technically belong to a single package, but linked libraries will
60 * have their names mangled, denoting that they came from a different package.
61 * We need to generate these symbols in a separate file.
62 * Returns true on success.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080063 */
Adam Lesinski9e10ac72015-10-16 14:37:48 -070064 bool generate(const StringPiece16& packageNameToGenerate, std::ostream* out);
65
66 bool generate(const StringPiece16& packageNameToGenerate,
67 const StringPiece16& outputPackageName,
68 std::ostream* out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069
70 const std::string& getError() const;
71
72private:
Adam Lesinskib274e352015-11-06 15:14:35 -080073 bool writeEntriesForClass(ClassDefinitionWriter* outClassDef,
74 const StringPiece16& packageNameToGenerate,
75 const ResourceTablePackage* package,
76 const ResourceTableType* type);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077
Adam Lesinskib274e352015-11-06 15:14:35 -080078 void writeStyleableEntryForClass(ClassDefinitionWriter* outClassDef,
79 AnnotationProcessor* processor,
80 const StringPiece16& packageNameToGenerate,
81 const std::u16string& entryName,
82 const Styleable* styleable);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070083
Adam Lesinski9e10ac72015-10-16 14:37:48 -070084 bool skipSymbol(SymbolState state);
85
Adam Lesinski76565542016-03-10 21:55:04 -080086 IAaptContext* mContext;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070087 ResourceTable* mTable;
88 JavaClassGeneratorOptions mOptions;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080089 std::string mError;
90};
91
92inline const std::string& JavaClassGenerator::getError() const {
93 return mError;
94}
95
96} // namespace aapt
97
98#endif // AAPT_JAVA_CLASS_GENERATOR_H