blob: 234df04472ce5429165298d64c0bc4ed9667d287 [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 <string>
21
Adam Lesinskid5083f62017-01-16 15:07:21 -080022#include "androidfw/StringPiece.h"
23
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include "ResourceTable.h"
25#include "ResourceValues.h"
Adam Lesinskia693c4a2017-11-09 11:29:39 -080026#include "io/Io.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 Lesinskia693c4a2017-11-09 11:29:39 -080029#include "text/Printer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080031namespace aapt {
32
Adam Lesinskib274e352015-11-06 15:14:35 -080033class AnnotationProcessor;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070034class ClassDefinition;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080035class MethodDefinition;
Adam Lesinskib274e352015-11-06 15:14:35 -080036
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -080037// Options for generating onResourcesLoaded callback in R.java.
38struct OnResourcesLoadedCallbackOptions {
39 // Other R classes to delegate the same callback to (with the same package ID).
40 std::vector<std::string> packages_to_callback;
41};
42
Adam Lesinski1ab598f2015-08-14 14:26:04 -070043struct JavaClassGeneratorOptions {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080044 // Specifies whether to use the 'final' modifier on resource entries. Default is true.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 bool use_final = true;
Adam Lesinski9e10ac72015-10-16 14:37:48 -070046
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -080047 // If set, generates code to rewrite the package ID of resources.
48 // Implies use_final == true. Default is unset.
Ryan Mitchell4382e442021-07-14 12:53:01 -070049 std::optional<OnResourcesLoadedCallbackOptions> rewrite_callback_options;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080050
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 enum class SymbolTypes {
52 kAll,
53 kPublicPrivate,
54 kPublic,
55 };
Adam Lesinski9e10ac72015-10-16 14:37:48 -070056
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 SymbolTypes types = SymbolTypes::kAll;
Adam Lesinski3524a232016-04-01 19:19:24 -070058
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080059 // A list of JavaDoc annotations to add to the comments of all generated classes.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 std::vector<std::string> javadoc_annotations;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070061};
62
Adam Lesinski418763f2017-04-11 17:36:53 -070063// Generates the R.java file for a resource table and optionally an R.txt file.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064class JavaClassGenerator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 public:
66 JavaClassGenerator(IAaptContext* context, ResourceTable* table,
67 const JavaClassGeneratorOptions& options);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080069 // Writes the R.java file to `out`. Only symbols belonging to `package` are written.
70 // All symbols technically belong to a single package, but linked libraries will
71 // have their names mangled, denoting that they came from a different package.
72 // We need to generate these symbols in a separate file. Returns true on success.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070073 bool Generate(android::StringPiece package_name_to_generate, io::OutputStream* out,
Adam Lesinskia693c4a2017-11-09 11:29:39 -080074 io::OutputStream* out_r_txt = nullptr);
Adam Lesinski9e10ac72015-10-16 14:37:48 -070075
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070076 bool Generate(android::StringPiece package_name_to_generate,
77 android::StringPiece output_package_name, io::OutputStream* out,
Adam Lesinskia693c4a2017-11-09 11:29:39 -080078 io::OutputStream* out_r_txt = nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080079
Adam Lesinskia693c4a2017-11-09 11:29:39 -080080 const std::string& GetError() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080081
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070082 static std::string TransformToFieldName(android::StringPiece symbol);
Adam Koskidc21dea2017-07-21 10:55:27 -070083
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 private:
Adam Lesinski71be7052017-12-12 16:48:07 -080085 bool SkipSymbol(Visibility::Level state);
Ryan Mitchell4382e442021-07-14 12:53:01 -070086 bool SkipSymbol(const std::optional<SymbolTable::Symbol>& symbol);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080087
88 // Returns the unmangled resource entry name if the unmangled package is the same as
89 // package_name_to_generate. Returns nothing if the resource should be skipped.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070090 std::optional<std::string> UnmangleResource(android::StringPiece package_name,
91 android::StringPiece package_name_to_generate,
Ryan Mitchell4382e442021-07-14 12:53:01 -070092 const ResourceEntry& entry);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080093
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070094 bool ProcessType(android::StringPiece package_name_to_generate,
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080095 const ResourceTablePackage& package, const ResourceTableType& type,
Adam Lesinski418763f2017-04-11 17:36:53 -070096 ClassDefinition* out_type_class_def, MethodDefinition* out_rewrite_method_def,
Adam Lesinskia693c4a2017-11-09 11:29:39 -080097 text::Printer* r_txt_printer);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080098
99 // Writes a resource to the R.java file, optionally writing out a rewrite rule for its package
100 // ID if `out_rewrite_method` is not nullptr.
101 void ProcessResource(const ResourceNameRef& name, const ResourceId& id,
102 const ResourceEntry& entry, ClassDefinition* out_class_def,
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800103 MethodDefinition* out_rewrite_method, text::Printer* r_txt_printer);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800104
105 // Writes a styleable resource to the R.java file, optionally writing out a rewrite rule for
106 // its package ID if `out_rewrite_method` is not nullptr.
107 // `package_name_to_generate` is the package
Ryan Mitchell5855de72021-02-24 14:39:13 -0800108 bool ProcessStyleable(const ResourceNameRef& name, const ResourceId& id,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700109 const Styleable& styleable, android::StringPiece package_name_to_generate,
Adam Lesinski418763f2017-04-11 17:36:53 -0700110 ClassDefinition* out_class_def, MethodDefinition* out_rewrite_method,
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800111 text::Printer* r_txt_printer);
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700112
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 IAaptContext* context_;
114 ResourceTable* table_;
115 JavaClassGeneratorOptions options_;
116 std::string error_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800117};
118
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800119inline const std::string& JavaClassGenerator::GetError() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 return error_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800121}
122
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800124
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125#endif // AAPT_JAVA_CLASS_GENERATOR_H