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