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 <string>
21
22 #include "androidfw/StringPiece.h"
23
24 #include "ResourceTable.h"
25 #include "ResourceValues.h"
26 #include "io/Io.h"
27 #include "process/IResourceTableConsumer.h"
28 #include "process/SymbolTable.h"
29 #include "text/Printer.h"
30
31 namespace aapt {
32
33 class AnnotationProcessor;
34 class ClassDefinition;
35 class MethodDefinition;
36
37 // Options for generating onResourcesLoaded callback in R.java.
38 struct 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
43 struct JavaClassGeneratorOptions {
44 // Specifies whether to use the 'final' modifier on resource entries. Default is true.
45 bool use_final = true;
46
47 // If set, generates code to rewrite the package ID of resources.
48 // Implies use_final == true. Default is unset.
49 Maybe<OnResourcesLoadedCallbackOptions> rewrite_callback_options;
50
51 enum class SymbolTypes {
52 kAll,
53 kPublicPrivate,
54 kPublic,
55 };
56
57 SymbolTypes types = SymbolTypes::kAll;
58
59 // A list of JavaDoc annotations to add to the comments of all generated classes.
60 std::vector<std::string> javadoc_annotations;
61 };
62
63 // Generates the R.java file for a resource table and optionally an R.txt file.
64 class JavaClassGenerator {
65 public:
66 JavaClassGenerator(IAaptContext* context, ResourceTable* table,
67 const JavaClassGeneratorOptions& options);
68
69 // 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.
73 bool Generate(const android::StringPiece& package_name_to_generate, io::OutputStream* out,
74 io::OutputStream* out_r_txt = nullptr);
75
76 bool Generate(const android::StringPiece& package_name_to_generate,
77 const android::StringPiece& output_package_name, io::OutputStream* out,
78 io::OutputStream* out_r_txt = nullptr);
79
80 const std::string& GetError() const;
81
82 static std::string TransformToFieldName(const android::StringPiece& symbol);
83
84 private:
85 bool SkipSymbol(Visibility::Level state);
86 bool SkipSymbol(const Maybe<SymbolTable::Symbol>& symbol);
87
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.
90 Maybe<std::string> UnmangleResource(const android::StringPiece& package_name,
91 const android::StringPiece& package_name_to_generate,
92 const ResourceEntry& entry);
93
94 bool ProcessType(const android::StringPiece& package_name_to_generate,
95 const ResourceTablePackage& package, const ResourceTableType& type,
96 ClassDefinition* out_type_class_def, MethodDefinition* out_rewrite_method_def,
97 text::Printer* r_txt_printer);
98
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,
103 MethodDefinition* out_rewrite_method, text::Printer* r_txt_printer);
104
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
108 bool ProcessStyleable(const ResourceNameRef& name, const ResourceId& id,
109 const Styleable& styleable,
110 const android::StringPiece& package_name_to_generate,
111 ClassDefinition* out_class_def, MethodDefinition* out_rewrite_method,
112 text::Printer* r_txt_printer);
113
114 IAaptContext* context_;
115 ResourceTable* table_;
116 JavaClassGeneratorOptions options_;
117 std::string error_;
118 };
119
GetError()120 inline const std::string& JavaClassGenerator::GetError() const {
121 return error_;
122 }
123
124 } // namespace aapt
125
126 #endif // AAPT_JAVA_CLASS_GENERATOR_H
127