2014-11-14 14:48:12 -08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef AAPT_JAVA_CLASS_GENERATOR_H
|
|
|
|
#define AAPT_JAVA_CLASS_GENERATOR_H
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
#include <string>
|
|
|
|
|
2017-01-16 15:07:21 -08:00
|
|
|
#include "androidfw/StringPiece.h"
|
|
|
|
|
2014-11-14 14:48:12 -08:00
|
|
|
#include "ResourceTable.h"
|
|
|
|
#include "ResourceValues.h"
|
2017-11-09 11:29:39 -08:00
|
|
|
#include "io/Io.h"
|
2016-03-10 21:55:04 -08:00
|
|
|
#include "process/IResourceTableConsumer.h"
|
2017-02-16 12:05:42 -08:00
|
|
|
#include "process/SymbolTable.h"
|
2017-11-09 11:29:39 -08:00
|
|
|
#include "text/Printer.h"
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2014-11-14 14:48:12 -08:00
|
|
|
namespace aapt {
|
|
|
|
|
2015-11-06 15:14:35 -08:00
|
|
|
class AnnotationProcessor;
|
2016-03-31 13:33:02 -07:00
|
|
|
class ClassDefinition;
|
2017-02-16 12:05:42 -08:00
|
|
|
class MethodDefinition;
|
2015-11-06 15:14:35 -08:00
|
|
|
|
2017-02-22 19:29:29 -08:00
|
|
|
// Options for generating onResourcesLoaded callback in R.java.
|
|
|
|
struct OnResourcesLoadedCallbackOptions {
|
|
|
|
// Other R classes to delegate the same callback to (with the same package ID).
|
|
|
|
std::vector<std::string> packages_to_callback;
|
|
|
|
};
|
|
|
|
|
2015-08-14 14:26:04 -07:00
|
|
|
struct JavaClassGeneratorOptions {
|
2017-02-16 12:05:42 -08:00
|
|
|
// Specifies whether to use the 'final' modifier on resource entries. Default is true.
|
2016-10-21 17:56:45 -07:00
|
|
|
bool use_final = true;
|
2016-10-19 12:18:14 -07:00
|
|
|
|
2017-02-22 19:29:29 -08:00
|
|
|
// If set, generates code to rewrite the package ID of resources.
|
|
|
|
// Implies use_final == true. Default is unset.
|
|
|
|
Maybe<OnResourcesLoadedCallbackOptions> rewrite_callback_options;
|
2017-02-16 12:05:42 -08:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
enum class SymbolTypes {
|
|
|
|
kAll,
|
|
|
|
kPublicPrivate,
|
|
|
|
kPublic,
|
|
|
|
};
|
|
|
|
|
|
|
|
SymbolTypes types = SymbolTypes::kAll;
|
|
|
|
|
2017-02-16 12:05:42 -08:00
|
|
|
// A list of JavaDoc annotations to add to the comments of all generated classes.
|
2016-10-21 17:56:45 -07:00
|
|
|
std::vector<std::string> javadoc_annotations;
|
2015-08-14 14:26:04 -07:00
|
|
|
};
|
|
|
|
|
2017-04-11 17:36:53 -07:00
|
|
|
// Generates the R.java file for a resource table and optionally an R.txt file.
|
2015-08-14 14:26:04 -07:00
|
|
|
class JavaClassGenerator {
|
2016-10-19 12:18:14 -07:00
|
|
|
public:
|
|
|
|
JavaClassGenerator(IAaptContext* context, ResourceTable* table,
|
|
|
|
const JavaClassGeneratorOptions& options);
|
|
|
|
|
2017-02-16 12:05:42 -08:00
|
|
|
// Writes the R.java file to `out`. Only symbols belonging to `package` are written.
|
|
|
|
// All symbols technically belong to a single package, but linked libraries will
|
|
|
|
// have their names mangled, denoting that they came from a different package.
|
|
|
|
// We need to generate these symbols in a separate file. Returns true on success.
|
2017-11-09 11:29:39 -08:00
|
|
|
bool Generate(const android::StringPiece& package_name_to_generate, io::OutputStream* out,
|
|
|
|
io::OutputStream* out_r_txt = nullptr);
|
2016-10-19 12:18:14 -07:00
|
|
|
|
2017-02-16 12:05:42 -08:00
|
|
|
bool Generate(const android::StringPiece& package_name_to_generate,
|
2017-11-09 11:29:39 -08:00
|
|
|
const android::StringPiece& output_package_name, io::OutputStream* out,
|
|
|
|
io::OutputStream* out_r_txt = nullptr);
|
2016-10-19 12:18:14 -07:00
|
|
|
|
2017-11-09 11:29:39 -08:00
|
|
|
const std::string& GetError() const;
|
2016-10-19 12:18:14 -07:00
|
|
|
|
2017-07-21 10:55:27 -07:00
|
|
|
static std::string TransformToFieldName(const android::StringPiece& symbol);
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
private:
|
2017-12-12 16:48:07 -08:00
|
|
|
bool SkipSymbol(Visibility::Level state);
|
2017-02-16 12:05:42 -08:00
|
|
|
bool SkipSymbol(const Maybe<SymbolTable::Symbol>& symbol);
|
|
|
|
|
|
|
|
// Returns the unmangled resource entry name if the unmangled package is the same as
|
|
|
|
// package_name_to_generate. Returns nothing if the resource should be skipped.
|
|
|
|
Maybe<std::string> UnmangleResource(const android::StringPiece& package_name,
|
|
|
|
const android::StringPiece& package_name_to_generate,
|
|
|
|
const ResourceEntry& entry);
|
|
|
|
|
|
|
|
bool ProcessType(const android::StringPiece& package_name_to_generate,
|
|
|
|
const ResourceTablePackage& package, const ResourceTableType& type,
|
2017-04-11 17:36:53 -07:00
|
|
|
ClassDefinition* out_type_class_def, MethodDefinition* out_rewrite_method_def,
|
2017-11-09 11:29:39 -08:00
|
|
|
text::Printer* r_txt_printer);
|
2017-02-16 12:05:42 -08:00
|
|
|
|
|
|
|
// Writes a resource to the R.java file, optionally writing out a rewrite rule for its package
|
|
|
|
// ID if `out_rewrite_method` is not nullptr.
|
|
|
|
void ProcessResource(const ResourceNameRef& name, const ResourceId& id,
|
|
|
|
const ResourceEntry& entry, ClassDefinition* out_class_def,
|
2017-11-09 11:29:39 -08:00
|
|
|
MethodDefinition* out_rewrite_method, text::Printer* r_txt_printer);
|
2017-02-16 12:05:42 -08:00
|
|
|
|
|
|
|
// Writes a styleable resource to the R.java file, optionally writing out a rewrite rule for
|
|
|
|
// its package ID if `out_rewrite_method` is not nullptr.
|
|
|
|
// `package_name_to_generate` is the package
|
|
|
|
void ProcessStyleable(const ResourceNameRef& name, const ResourceId& id,
|
|
|
|
const Styleable& styleable,
|
|
|
|
const android::StringPiece& package_name_to_generate,
|
2017-04-11 17:36:53 -07:00
|
|
|
ClassDefinition* out_class_def, MethodDefinition* out_rewrite_method,
|
2017-11-09 11:29:39 -08:00
|
|
|
text::Printer* r_txt_printer);
|
2016-10-19 12:18:14 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
IAaptContext* context_;
|
|
|
|
ResourceTable* table_;
|
|
|
|
JavaClassGeneratorOptions options_;
|
|
|
|
std::string error_;
|
2014-11-14 14:48:12 -08:00
|
|
|
};
|
|
|
|
|
2017-11-09 11:29:39 -08:00
|
|
|
inline const std::string& JavaClassGenerator::GetError() const {
|
2016-10-21 17:56:45 -07:00
|
|
|
return error_;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
} // namespace aapt
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
#endif // AAPT_JAVA_CLASS_GENERATOR_H
|