blob: 45104301e7f510c736be78625ae780d32d92ea24 [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 Lesinskiceb9b2f2017-02-16 12:05:42 -080028#include "process/SymbolTable.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080030namespace aapt {
31
Adam Lesinskib274e352015-11-06 15:14:35 -080032class AnnotationProcessor;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070033class ClassDefinition;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080034class MethodDefinition;
Adam Lesinskib274e352015-11-06 15:14:35 -080035
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -080036// Options for generating onResourcesLoaded callback in R.java.
37struct OnResourcesLoadedCallbackOptions {
38 // Other R classes to delegate the same callback to (with the same package ID).
39 std::vector<std::string> packages_to_callback;
40};
41
Adam Lesinski1ab598f2015-08-14 14:26:04 -070042struct JavaClassGeneratorOptions {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080043 // Specifies whether to use the 'final' modifier on resource entries. Default is true.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 bool use_final = true;
Adam Lesinski9e10ac72015-10-16 14:37:48 -070045
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -080046 // If set, generates code to rewrite the package ID of resources.
47 // Implies use_final == true. Default is unset.
48 Maybe<OnResourcesLoadedCallbackOptions> rewrite_callback_options;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080049
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 enum class SymbolTypes {
51 kAll,
52 kPublicPrivate,
53 kPublic,
54 };
Adam Lesinski9e10ac72015-10-16 14:37:48 -070055
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 SymbolTypes types = SymbolTypes::kAll;
Adam Lesinski3524a232016-04-01 19:19:24 -070057
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080058 // A list of JavaDoc annotations to add to the comments of all generated classes.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 std::vector<std::string> javadoc_annotations;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060};
61
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080062// Generates the R.java file for a resource table.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063class JavaClassGenerator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 public:
65 JavaClassGenerator(IAaptContext* context, ResourceTable* table,
66 const JavaClassGeneratorOptions& options);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080067
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080068 // Writes the R.java file to `out`. Only symbols belonging to `package` are written.
69 // All symbols technically belong to a single package, but linked libraries will
70 // have their names mangled, denoting that they came from a different package.
71 // We need to generate these symbols in a separate file. Returns true on success.
72 bool Generate(const android::StringPiece& package_name_to_generate, std::ostream* out);
Adam Lesinski9e10ac72015-10-16 14:37:48 -070073
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080074 bool Generate(const android::StringPiece& package_name_to_generate,
75 const android::StringPiece& output_package_name, std::ostream* out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 const std::string& getError() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 bool SkipSymbol(SymbolState state);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080081 bool SkipSymbol(const Maybe<SymbolTable::Symbol>& symbol);
82
83 // Returns the unmangled resource entry name if the unmangled package is the same as
84 // package_name_to_generate. Returns nothing if the resource should be skipped.
85 Maybe<std::string> UnmangleResource(const android::StringPiece& package_name,
86 const android::StringPiece& package_name_to_generate,
87 const ResourceEntry& entry);
88
89 bool ProcessType(const android::StringPiece& package_name_to_generate,
90 const ResourceTablePackage& package, const ResourceTableType& type,
91 ClassDefinition* out_type_class_def, MethodDefinition* out_rewrite_method_def);
92
93 // Writes a resource to the R.java file, optionally writing out a rewrite rule for its package
94 // ID if `out_rewrite_method` is not nullptr.
95 void ProcessResource(const ResourceNameRef& name, const ResourceId& id,
96 const ResourceEntry& entry, ClassDefinition* out_class_def,
97 MethodDefinition* out_rewrite_method);
98
99 // Writes a styleable resource to the R.java file, optionally writing out a rewrite rule for
100 // its package ID if `out_rewrite_method` is not nullptr.
101 // `package_name_to_generate` is the package
102 void ProcessStyleable(const ResourceNameRef& name, const ResourceId& id,
103 const Styleable& styleable,
104 const android::StringPiece& package_name_to_generate,
105 ClassDefinition* out_class_def, MethodDefinition* out_rewrite_method);
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700106
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 IAaptContext* context_;
108 ResourceTable* table_;
109 JavaClassGeneratorOptions options_;
110 std::string error_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800111};
112
113inline const std::string& JavaClassGenerator::getError() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 return error_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800115}
116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800118
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119#endif // AAPT_JAVA_CLASS_GENERATOR_H