Merge "AAPT2: Change how we generate Java classes" into nyc-dev

This commit is contained in:
Adam Lesinski
2016-04-01 00:44:19 +00:00
committed by Android (Google) Code Review
12 changed files with 459 additions and 303 deletions

View File

@@ -54,6 +54,7 @@ sources := \
Debug.cpp \ Debug.cpp \
Flags.cpp \ Flags.cpp \
java/AnnotationProcessor.cpp \ java/AnnotationProcessor.cpp \
java/ClassDefinition.cpp \
java/JavaClassGenerator.cpp \ java/JavaClassGenerator.cpp \
java/ManifestClassGenerator.cpp \ java/ManifestClassGenerator.cpp \
java/ProguardRules.cpp \ java/ProguardRules.cpp \

View File

@@ -64,7 +64,7 @@ void AnnotationProcessor::appendNewLine() {
mComment << "\n *"; mComment << "\n *";
} }
void AnnotationProcessor::writeToStream(std::ostream* out, const StringPiece& prefix) { void AnnotationProcessor::writeToStream(std::ostream* out, const StringPiece& prefix) const {
if (mHasComments) { if (mHasComments) {
std::string result = mComment.str(); std::string result = mComment.str();
for (StringPiece line : util::tokenize<char>(result, '\n')) { for (StringPiece line : util::tokenize<char>(result, '\n')) {

View File

@@ -66,7 +66,7 @@ public:
/** /**
* Writes the comments and annotations to the stream, with the given prefix before each line. * Writes the comments and annotations to the stream, with the given prefix before each line.
*/ */
void writeToStream(std::ostream* out, const StringPiece& prefix); void writeToStream(std::ostream* out, const StringPiece& prefix) const;
private: private:
enum : uint32_t { enum : uint32_t {

View File

@@ -0,0 +1,75 @@
/*
* Copyright (C) 2016 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.
*/
#include "java/ClassDefinition.h"
#include "util/StringPiece.h"
#include <ostream>
namespace aapt {
bool ClassDefinition::empty() const {
for (const std::unique_ptr<ClassMember>& member : mMembers) {
if (!member->empty()) {
return false;
}
}
return true;
}
void ClassDefinition::writeToStream(const StringPiece& prefix, bool final,
std::ostream* out) const {
if (mMembers.empty() && !mCreateIfEmpty) {
return;
}
ClassMember::writeToStream(prefix, final, out);
*out << prefix << "public ";
if (mQualifier == ClassQualifier::Static) {
*out << "static ";
}
*out << "final class " << mName << " {\n";
std::string newPrefix = prefix.toString();
newPrefix.append(kIndent);
for (const std::unique_ptr<ClassMember>& member : mMembers) {
member->writeToStream(newPrefix, final, out);
*out << "\n";
}
*out << prefix << "}";
}
constexpr static const char* sWarningHeader =
"/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
" *\n"
" * This class was automatically generated by the\n"
" * aapt tool from the resource data it found. It\n"
" * should not be modified by hand.\n"
" */\n\n";
bool ClassDefinition::writeJavaFile(const ClassDefinition* def,
const StringPiece& package,
bool final,
std::ostream* out) {
*out << sWarningHeader << "package " << package << ";\n\n";
def->writeToStream("", final, out);
return bool(*out);
}
} // namespace aapt

View File

@@ -0,0 +1,188 @@
/*
* 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_CLASSDEFINITION_H
#define AAPT_JAVA_CLASSDEFINITION_H
#include "Resource.h"
#include "java/AnnotationProcessor.h"
#include "util/StringPiece.h"
#include "util/Util.h"
#include <android-base/macros.h>
#include <sstream>
#include <string>
namespace aapt {
// The number of attributes to emit per line in a Styleable array.
constexpr static size_t kAttribsPerLine = 4;
constexpr static const char* kIndent = " ";
class ClassMember {
public:
virtual ~ClassMember() = default;
AnnotationProcessor* getCommentBuilder() {
return &mProcessor;
}
virtual bool empty() const = 0;
virtual void writeToStream(const StringPiece& prefix, bool final, std::ostream* out) const {
mProcessor.writeToStream(out, prefix);
}
private:
AnnotationProcessor mProcessor;
};
template <typename T>
class PrimitiveMember : public ClassMember {
public:
PrimitiveMember(const StringPiece& name, const T& val) :
mName(name.toString()), mVal(val) {
}
bool empty() const override {
return false;
}
void writeToStream(const StringPiece& prefix, bool final, std::ostream* out) const override {
ClassMember::writeToStream(prefix, final, out);
*out << prefix << "public static " << (final ? "final " : "")
<< "int " << mName << "=" << mVal << ";";
}
private:
std::string mName;
T mVal;
DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
};
/**
* Specialization for strings so they get the right type and are quoted with "".
*/
template <>
class PrimitiveMember<std::string> : public ClassMember {
public:
PrimitiveMember(const StringPiece& name, const std::string& val) :
mName(name.toString()), mVal(val) {
}
bool empty() const override {
return false;
}
void writeToStream(const StringPiece& prefix, bool final, std::ostream* out) const override {
ClassMember::writeToStream(prefix, final, out);
*out << prefix << "public static " << (final ? "final " : "")
<< "String " << mName << "=\"" << mVal << "\";";
}
private:
std::string mName;
std::string mVal;
DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
};
using IntMember = PrimitiveMember<uint32_t>;
using ResourceMember = PrimitiveMember<ResourceId>;
using StringMember = PrimitiveMember<std::string>;
template <typename T>
class PrimitiveArrayMember : public ClassMember {
public:
PrimitiveArrayMember(const StringPiece& name) :
mName(name.toString()) {
}
void addElement(const T& val) {
mElements.push_back(val);
}
bool empty() const override {
return false;
}
void writeToStream(const StringPiece& prefix, bool final, std::ostream* out) const override {
ClassMember::writeToStream(prefix, final, out);
*out << "public static final int[] " << mName << "={";
const auto begin = mElements.begin();
const auto end = mElements.end();
for (auto current = begin; current != end; ++current) {
if (std::distance(begin, current) % kAttribsPerLine == 0) {
*out << "\n" << prefix << kIndent << kIndent;
}
*out << *current;
if (std::distance(current, end) > 1) {
*out << ", ";
}
}
*out << "\n" << prefix << kIndent <<"};";
}
private:
std::string mName;
std::vector<T> mElements;
DISALLOW_COPY_AND_ASSIGN(PrimitiveArrayMember);
};
using ResourceArrayMember = PrimitiveArrayMember<ResourceId>;
enum class ClassQualifier {
None,
Static
};
class ClassDefinition : public ClassMember {
public:
static bool writeJavaFile(const ClassDefinition* def,
const StringPiece& package,
bool final,
std::ostream* out);
ClassDefinition(const StringPiece& name, ClassQualifier qualifier, bool createIfEmpty) :
mName(name.toString()), mQualifier(qualifier), mCreateIfEmpty(createIfEmpty) {
}
void addMember(std::unique_ptr<ClassMember> member) {
mMembers.push_back(std::move(member));
}
bool empty() const override;
void writeToStream(const StringPiece& prefix, bool final, std::ostream* out) const override;
private:
std::string mName;
ClassQualifier mQualifier;
bool mCreateIfEmpty;
std::vector<std::unique_ptr<ClassMember>> mMembers;
DISALLOW_COPY_AND_ASSIGN(ClassDefinition);
};
} // namespace aapt
#endif /* AAPT_JAVA_CLASSDEFINITION_H */

View File

@@ -1,142 +0,0 @@
/*
* 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_CLASSDEFINITION_H
#define AAPT_JAVA_CLASSDEFINITION_H
#include "Resource.h"
#include "java/AnnotationProcessor.h"
#include "util/StringPiece.h"
#include "util/Util.h"
#include <sstream>
#include <string>
namespace aapt {
struct ClassDefinitionWriterOptions {
bool useFinalQualifier = false;
bool forceCreationIfEmpty = false;
};
/**
* Writes a class for use in R.java or Manifest.java.
*/
class ClassDefinitionWriter {
public:
ClassDefinitionWriter(const StringPiece& name, const ClassDefinitionWriterOptions& options) :
mName(name.toString()), mOptions(options), mStarted(false) {
}
ClassDefinitionWriter(const StringPiece16& name, const ClassDefinitionWriterOptions& options) :
mName(util::utf16ToUtf8(name)), mOptions(options), mStarted(false) {
}
void addIntMember(const StringPiece& name, AnnotationProcessor* processor,
const uint32_t val) {
ensureClassDeclaration();
if (processor) {
processor->writeToStream(&mOut, kIndent);
}
mOut << kIndent << "public static " << (mOptions.useFinalQualifier ? "final " : "")
<< "int " << name << "=" << val << ";\n";
}
void addStringMember(const StringPiece16& name, AnnotationProcessor* processor,
const StringPiece16& val) {
ensureClassDeclaration();
if (processor) {
processor->writeToStream(&mOut, kIndent);
}
mOut << kIndent << "public static " << (mOptions.useFinalQualifier ? "final " : "")
<< "String " << name << "=\"" << val << "\";\n";
}
void addResourceMember(const StringPiece& name, AnnotationProcessor* processor,
const ResourceId id) {
ensureClassDeclaration();
if (processor) {
processor->writeToStream(&mOut, kIndent);
}
mOut << kIndent << "public static " << (mOptions.useFinalQualifier ? "final " : "")
<< "int " << name << "=" << id <<";\n";
}
template <typename Iterator, typename FieldAccessorFunc>
void addArrayMember(const StringPiece& name, AnnotationProcessor* processor,
const Iterator begin, const Iterator end, FieldAccessorFunc f) {
ensureClassDeclaration();
if (processor) {
processor->writeToStream(&mOut, kIndent);
}
mOut << kIndent << "public static final int[] " << name << "={";
for (Iterator current = begin; current != end; ++current) {
if (std::distance(begin, current) % kAttribsPerLine == 0) {
mOut << "\n" << kIndent << kIndent;
}
mOut << f(*current);
if (std::distance(current, end) > 1) {
mOut << ", ";
}
}
mOut << "\n" << kIndent <<"};\n";
}
void writeToStream(std::ostream* out, const StringPiece& prefix,
AnnotationProcessor* processor=nullptr) {
if (mOptions.forceCreationIfEmpty) {
ensureClassDeclaration();
}
if (!mStarted) {
return;
}
if (processor) {
processor->writeToStream(out, prefix);
}
std::string result = mOut.str();
for (StringPiece line : util::tokenize<char>(result, '\n')) {
*out << prefix << line << "\n";
}
*out << prefix << "}\n";
}
private:
constexpr static const char* kIndent = " ";
// The number of attributes to emit per line in a Styleable array.
constexpr static size_t kAttribsPerLine = 4;
void ensureClassDeclaration() {
if (!mStarted) {
mStarted = true;
mOut << "public static final class " << mName << " {\n";
}
}
std::stringstream mOut;
std::string mName;
ClassDefinitionWriterOptions mOptions;
bool mStarted;
};
} // namespace aapt
#endif /* AAPT_JAVA_CLASSDEFINITION_H */

View File

@@ -21,7 +21,7 @@
#include "ValueVisitor.h" #include "ValueVisitor.h"
#include "java/AnnotationProcessor.h" #include "java/AnnotationProcessor.h"
#include "java/ClassDefinitionWriter.h" #include "java/ClassDefinition.h"
#include "java/JavaClassGenerator.h" #include "java/JavaClassGenerator.h"
#include "process/SymbolTable.h" #include "process/SymbolTable.h"
#include "util/StringPiece.h" #include "util/StringPiece.h"
@@ -39,16 +39,6 @@ JavaClassGenerator::JavaClassGenerator(IAaptContext* context, ResourceTable* tab
mContext(context), mTable(table), mOptions(options) { mContext(context), mTable(table), mOptions(options) {
} }
static void generateHeader(const StringPiece16& packageNameToGenerate, std::ostream* out) {
*out << "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
" *\n"
" * This class was automatically generated by the\n"
" * aapt tool from the resource data it found. It\n"
" * should not be modified by hand.\n"
" */\n\n"
"package " << packageNameToGenerate << ";\n\n";
}
static const std::set<StringPiece16> sJavaIdentifiers = { static const std::set<StringPiece16> sJavaIdentifiers = {
u"abstract", u"assert", u"boolean", u"break", u"byte", u"abstract", u"assert", u"boolean", u"break", u"byte",
u"case", u"catch", u"char", u"class", u"const", u"continue", u"case", u"catch", u"char", u"class", u"const", u"continue",
@@ -214,13 +204,15 @@ static bool lessStyleableAttr(const StyleableAttr& lhs, const StyleableAttr& rhs
} }
} }
void JavaClassGenerator::writeStyleableEntryForClass(ClassDefinitionWriter* outClassDef, void JavaClassGenerator::addMembersToStyleableClass(const StringPiece16& packageNameToGenerate,
AnnotationProcessor* processor,
const StringPiece16& packageNameToGenerate,
const std::u16string& entryName, const std::u16string& entryName,
const Styleable* styleable) { const Styleable* styleable,
ClassDefinition* outStyleableClassDef) {
const std::string className = transform(entryName); const std::string className = transform(entryName);
std::unique_ptr<ResourceArrayMember> styleableArrayDef =
util::make_unique<ResourceArrayMember>(className);
// This must be sorted by resource ID. // This must be sorted by resource ID.
std::vector<StyleableAttr> sortedAttributes; std::vector<StyleableAttr> sortedAttributes;
sortedAttributes.reserve(styleable->entries.size()); sortedAttributes.reserve(styleable->entries.size());
@@ -230,6 +222,8 @@ void JavaClassGenerator::writeStyleableEntryForClass(ClassDefinitionWriter* outC
assert((!mOptions.useFinal || attr.id) && "no ID set for Styleable entry"); assert((!mOptions.useFinal || attr.id) && "no ID set for Styleable entry");
assert(attr.name && "no name set for Styleable entry"); assert(attr.name && "no name set for Styleable entry");
// We will need the unmangled, transformed name in the comments and the field,
// so create it once and cache it in this StyleableAttr data structure.
StyleableAttr styleableAttr = {}; StyleableAttr styleableAttr = {};
styleableAttr.attrRef = &attr; styleableAttr.attrRef = &attr;
styleableAttr.fieldName = transformNestedAttr(attr.name.value(), className, styleableAttr.fieldName = transformNestedAttr(attr.name.value(), className,
@@ -247,6 +241,8 @@ void JavaClassGenerator::writeStyleableEntryForClass(ClassDefinitionWriter* outC
mangledReference.name = mangledName; mangledReference.name = mangledName;
} }
// Look up the symbol so that we can write out in the comments what are possible
// legal values for this attribute.
const SymbolTable::Symbol* symbol = mContext->getExternalSymbols()->findByReference( const SymbolTable::Symbol* symbol = mContext->getExternalSymbols()->findByReference(
mangledReference); mangledReference);
if (symbol) { if (symbol) {
@@ -254,10 +250,11 @@ void JavaClassGenerator::writeStyleableEntryForClass(ClassDefinitionWriter* outC
} }
sortedAttributes.push_back(std::move(styleableAttr)); sortedAttributes.push_back(std::move(styleableAttr));
} }
// Sort the attributes by ID.
std::sort(sortedAttributes.begin(), sortedAttributes.end(), lessStyleableAttr); std::sort(sortedAttributes.begin(), sortedAttributes.end(), lessStyleableAttr);
const size_t attrCount = sortedAttributes.size(); const size_t attrCount = sortedAttributes.size();
if (attrCount > 0) { if (attrCount > 0) {
// Build the comment string for the Styleable. It includes details about the // Build the comment string for the Styleable. It includes details about the
// child attributes. // child attributes.
@@ -267,6 +264,7 @@ void JavaClassGenerator::writeStyleableEntryForClass(ClassDefinitionWriter* outC
} else { } else {
styleableComment << "Attributes that can be used with a " << className << ".\n"; styleableComment << "Attributes that can be used with a " << className << ".\n";
} }
styleableComment << styleableComment <<
"<p>Includes the following attributes:</p>\n" "<p>Includes the following attributes:</p>\n"
"<table>\n" "<table>\n"
@@ -274,7 +272,7 @@ void JavaClassGenerator::writeStyleableEntryForClass(ClassDefinitionWriter* outC
"<colgroup align=\"left\" />\n" "<colgroup align=\"left\" />\n"
"<tr><th>Attribute</th><th>Description</th></tr>\n"; "<tr><th>Attribute</th><th>Description</th></tr>\n";
for (const auto& entry : sortedAttributes) { for (const StyleableAttr& entry : sortedAttributes) {
const ResourceName& attrName = entry.attrRef->name.value(); const ResourceName& attrName = entry.attrRef->name.value();
styleableComment << "<tr><td>"; styleableComment << "<tr><td>";
styleableComment << "<code>{@link #" styleableComment << "<code>{@link #"
@@ -292,21 +290,22 @@ void JavaClassGenerator::writeStyleableEntryForClass(ClassDefinitionWriter* outC
styleableComment << "</td></tr>\n"; styleableComment << "</td></tr>\n";
} }
styleableComment << "</table>\n"; styleableComment << "</table>\n";
for (const auto& entry : sortedAttributes) {
for (const StyleableAttr& entry : sortedAttributes) {
styleableComment << "@see #" << entry.fieldName << "\n"; styleableComment << "@see #" << entry.fieldName << "\n";
} }
processor->appendComment(styleableComment.str());
styleableArrayDef->getCommentBuilder()->appendComment(styleableComment.str());
} }
auto accessorFunc = [](const StyleableAttr& a) -> ResourceId { // Add the ResourceIds to the array member.
return a.attrRef->id ? a.attrRef->id.value() : ResourceId(0); for (const StyleableAttr& styleableAttr : sortedAttributes) {
}; styleableArrayDef->addElement(
styleableAttr.attrRef->id ? styleableAttr.attrRef->id.value() : ResourceId(0));
}
// First we emit the array containing the IDs of each attribute. // Add the Styleable array to the Styleable class.
outClassDef->addArrayMember(className, processor, outStyleableClassDef->addMember(std::move(styleableArrayDef));
sortedAttributes.begin(),
sortedAttributes.end(),
accessorFunc);
// Now we emit the indices into the array. // Now we emit the indices into the array.
for (size_t i = 0; i < attrCount; i++) { for (size_t i = 0; i < attrCount; i++) {
@@ -318,7 +317,10 @@ void JavaClassGenerator::writeStyleableEntryForClass(ClassDefinitionWriter* outC
packageName = mContext->getCompilationPackage(); packageName = mContext->getCompilationPackage();
} }
AnnotationProcessor attrProcessor; std::unique_ptr<IntMember> indexMember = util::make_unique<IntMember>(
sortedAttributes[i].fieldName, i);
AnnotationProcessor* attrProcessor = indexMember->getCommentBuilder();
StringPiece16 comment = styleableAttr.attrRef->getComment(); StringPiece16 comment = styleableAttr.attrRef->getComment();
if (styleableAttr.attribute && comment.empty()) { if (styleableAttr.attribute && comment.empty()) {
@@ -326,8 +328,8 @@ void JavaClassGenerator::writeStyleableEntryForClass(ClassDefinitionWriter* outC
} }
if (!comment.empty()) { if (!comment.empty()) {
attrProcessor.appendComment("<p>\n@attr description"); attrProcessor->appendComment("<p>\n@attr description");
attrProcessor.appendComment(comment); attrProcessor->appendComment(comment);
} else { } else {
std::stringstream defaultComment; std::stringstream defaultComment;
defaultComment defaultComment
@@ -335,27 +337,29 @@ void JavaClassGenerator::writeStyleableEntryForClass(ClassDefinitionWriter* outC
<< "{@link " << packageName << ".R.attr#" << transform(attrName.entry) << "}\n" << "{@link " << packageName << ".R.attr#" << transform(attrName.entry) << "}\n"
<< "attribute's value can be found in the " << "attribute's value can be found in the "
<< "{@link #" << className << "} array."; << "{@link #" << className << "} array.";
attrProcessor.appendComment(defaultComment.str()); attrProcessor->appendComment(defaultComment.str());
} }
attrProcessor.appendNewLine(); attrProcessor->appendNewLine();
if (styleableAttr.attribute) { if (styleableAttr.attribute) {
addAttributeFormatDoc(&attrProcessor, styleableAttr.attribute.get()); addAttributeFormatDoc(attrProcessor, styleableAttr.attribute.get());
attrProcessor.appendNewLine(); attrProcessor->appendNewLine();
} }
std::stringstream doclavaName; std::stringstream doclavaName;
doclavaName << "@attr name " << packageName << ":" << attrName.entry;; doclavaName << "@attr name " << packageName << ":" << attrName.entry;;
attrProcessor.appendComment(doclavaName.str()); attrProcessor->appendComment(doclavaName.str());
outClassDef->addIntMember(sortedAttributes[i].fieldName, &attrProcessor, i);
outStyleableClassDef->addMember(std::move(indexMember));
} }
} }
bool JavaClassGenerator::writeEntriesForClass(ClassDefinitionWriter* outClassDef, bool JavaClassGenerator::addMembersToTypeClass(const StringPiece16& packageNameToGenerate,
const StringPiece16& packageNameToGenerate,
const ResourceTablePackage* package, const ResourceTablePackage* package,
const ResourceTableType* type) { const ResourceTableType* type,
ClassDefinition* outTypeClassDef) {
for (const auto& entry : type->entries) { for (const auto& entry : type->entries) {
if (skipSymbol(entry->symbolStatus.state)) { if (skipSymbol(entry->symbolStatus.state)) {
continue; continue;
@@ -389,33 +393,41 @@ bool JavaClassGenerator::writeEntriesForClass(ClassDefinitionWriter* outClassDef
return false; return false;
} }
// Build the comments and annotations for this entry. if (type->type == ResourceType::kStyleable) {
assert(!entry->values.empty());
AnnotationProcessor processor; const Styleable* styleable = static_cast<const Styleable*>(
entry->values.front()->value.get());
// Comments are handled within this method.
addMembersToStyleableClass(packageNameToGenerate, unmangledName, styleable,
outTypeClassDef);
} else {
std::unique_ptr<ResourceMember> resourceMember =
util::make_unique<ResourceMember>(transform(unmangledName), id);
// Build the comments and annotations for this entry.
AnnotationProcessor* processor = resourceMember->getCommentBuilder();
// Add the comments from any <public> tags.
if (entry->symbolStatus.state != SymbolState::kUndefined) { if (entry->symbolStatus.state != SymbolState::kUndefined) {
processor.appendComment(entry->symbolStatus.comment); processor->appendComment(entry->symbolStatus.comment);
} }
// Add the comments from all configurations of this entry.
for (const auto& configValue : entry->values) { for (const auto& configValue : entry->values) {
processor.appendComment(configValue->value->getComment()); processor->appendComment(configValue->value->getComment());
} }
// If this is an Attribute, append the format Javadoc. // If this is an Attribute, append the format Javadoc.
if (!entry->values.empty()) { if (!entry->values.empty()) {
if (Attribute* attr = valueCast<Attribute>(entry->values.front()->value.get())) { if (Attribute* attr = valueCast<Attribute>(entry->values.front()->value.get())) {
// We list out the available values for the given attribute. // We list out the available values for the given attribute.
addAttributeFormatDoc(&processor, attr); addAttributeFormatDoc(processor, attr);
} }
} }
if (type->type == ResourceType::kStyleable) { outTypeClassDef->addMember(std::move(resourceMember));
assert(!entry->values.empty());
const Styleable* styleable = static_cast<const Styleable*>(
entry->values.front()->value.get());
writeStyleableEntryForClass(outClassDef, &processor, packageNameToGenerate,
unmangledName, styleable);
} else {
outClassDef->addResourceMember(transform(unmangledName), &processor, id);
} }
} }
return true; return true;
@@ -427,9 +439,8 @@ bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, st
bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate,
const StringPiece16& outPackageName, std::ostream* out) { const StringPiece16& outPackageName, std::ostream* out) {
generateHeader(outPackageName, out);
*out << "public final class R {\n"; ClassDefinition rClass("R", ClassQualifier::None, true);
for (const auto& package : mTable->packages) { for (const auto& package : mTable->packages) {
for (const auto& type : package->types) { for (const auto& type : package->types) {
@@ -437,13 +448,15 @@ bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate,
continue; continue;
} }
ClassDefinitionWriterOptions classOptions; const bool forceCreationIfEmpty =
classOptions.useFinalQualifier = mOptions.useFinal;
classOptions.forceCreationIfEmpty =
(mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic); (mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic);
ClassDefinitionWriter classDef(toString(type->type), classOptions);
bool result = writeEntriesForClass(&classDef, packageNameToGenerate, std::unique_ptr<ClassDefinition> classDef = util::make_unique<ClassDefinition>(
package.get(), type.get()); util::utf16ToUtf8(toString(type->type)), ClassQualifier::Static,
forceCreationIfEmpty);
bool result = addMembersToTypeClass(packageNameToGenerate, package.get(), type.get(),
classDef.get());
if (!result) { if (!result) {
return false; return false;
} }
@@ -452,26 +465,31 @@ bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate,
// Also include private attributes in this same class. // Also include private attributes in this same class.
ResourceTableType* privType = package->findType(ResourceType::kAttrPrivate); ResourceTableType* privType = package->findType(ResourceType::kAttrPrivate);
if (privType) { if (privType) {
result = writeEntriesForClass(&classDef, packageNameToGenerate, result = addMembersToTypeClass(packageNameToGenerate, package.get(), privType,
package.get(), privType); classDef.get());
if (!result) { if (!result) {
return false; return false;
} }
} }
} }
AnnotationProcessor processor;
if (type->type == ResourceType::kStyleable && if (type->type == ResourceType::kStyleable &&
mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic) { mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic) {
// When generating a public R class, we don't want Styleable to be part of the API. // When generating a public R class, we don't want Styleable to be part of the API.
// It is only emitted for documentation purposes. // It is only emitted for documentation purposes.
processor.appendComment("@doconly"); AnnotationProcessor* processor = classDef->getCommentBuilder();
processor->appendComment("@doconly");
} }
classDef.writeToStream(out, " ", &processor);
rClass.addMember(std::move(classDef));
} }
} }
*out << "}\n"; if (!ClassDefinition::writeJavaFile(&rClass, util::utf16ToUtf8(outPackageName),
mOptions.useFinal, out)) {
return false;
}
out->flush(); out->flush();
return true; return true;
} }

View File

@@ -28,7 +28,7 @@
namespace aapt { namespace aapt {
class AnnotationProcessor; class AnnotationProcessor;
class ClassDefinitionWriter; class ClassDefinition;
struct JavaClassGeneratorOptions { struct JavaClassGeneratorOptions {
/* /*
@@ -70,16 +70,15 @@ public:
const std::string& getError() const; const std::string& getError() const;
private: private:
bool writeEntriesForClass(ClassDefinitionWriter* outClassDef, bool addMembersToTypeClass(const StringPiece16& packageNameToGenerate,
const StringPiece16& packageNameToGenerate,
const ResourceTablePackage* package, const ResourceTablePackage* package,
const ResourceTableType* type); const ResourceTableType* type,
ClassDefinition* outTypeClassDef);
void writeStyleableEntryForClass(ClassDefinitionWriter* outClassDef, void addMembersToStyleableClass(const StringPiece16& packageNameToGenerate,
AnnotationProcessor* processor,
const StringPiece16& packageNameToGenerate,
const std::u16string& entryName, const std::u16string& entryName,
const Styleable* styleable); const Styleable* styleable,
ClassDefinition* outStyleableClassDef);
bool skipSymbol(SymbolState state); bool skipSymbol(SymbolState state);

View File

@@ -16,7 +16,7 @@
#include "Source.h" #include "Source.h"
#include "java/AnnotationProcessor.h" #include "java/AnnotationProcessor.h"
#include "java/ClassDefinitionWriter.h" #include "java/ClassDefinition.h"
#include "java/ManifestClassGenerator.h" #include "java/ManifestClassGenerator.h"
#include "util/Maybe.h" #include "util/Maybe.h"
#include "xml/XmlDom.h" #include "xml/XmlDom.h"
@@ -58,8 +58,8 @@ static Maybe<StringPiece16> extractJavaIdentifier(IDiagnostics* diag, const Sour
return result; return result;
} }
static bool writeSymbol(IDiagnostics* diag, ClassDefinitionWriter* outClassDef, const Source& source, static bool writeSymbol(const Source& source, IDiagnostics* diag, xml::Element* el,
xml::Element* el) { ClassDefinition* classDef) {
xml::Attribute* attr = el->findAttribute(xml::kSchemaAndroid, u"name"); xml::Attribute* attr = el->findAttribute(xml::kSchemaAndroid, u"name");
if (!attr) { if (!attr) {
diag->error(DiagMessage(source) << "<" << el->name << "> must define 'android:name'"); diag->error(DiagMessage(source) << "<" << el->name << "> must define 'android:name'");
@@ -72,54 +72,53 @@ static bool writeSymbol(IDiagnostics* diag, ClassDefinitionWriter* outClassDef,
return false; return false;
} }
AnnotationProcessor processor; std::unique_ptr<StringMember> stringMember = util::make_unique<StringMember>(
processor.appendComment(el->comment); util::utf16ToUtf8(result.value()), util::utf16ToUtf8(attr->value));
outClassDef->addStringMember(result.value(), &processor, attr->value); stringMember->getCommentBuilder()->appendComment(el->comment);
classDef->addMember(std::move(stringMember));
return true; return true;
} }
bool ManifestClassGenerator::generate(IDiagnostics* diag, const StringPiece16& package, std::unique_ptr<ClassDefinition> generateManifestClass(IDiagnostics* diag, xml::XmlResource* res) {
xml::XmlResource* res, std::ostream* out) {
xml::Element* el = xml::findRootElement(res->root.get()); xml::Element* el = xml::findRootElement(res->root.get());
if (!el) { if (!el) {
return false; diag->error(DiagMessage(res->file.source) << "no root tag defined");
return {};
} }
if (el->name != u"manifest" && !el->namespaceUri.empty()) { if (el->name != u"manifest" && !el->namespaceUri.empty()) {
diag->error(DiagMessage(res->file.source) << "no <manifest> root tag defined"); diag->error(DiagMessage(res->file.source) << "no <manifest> root tag defined");
return false; return {};
} }
*out << "package " << package << ";\n\n" std::unique_ptr<ClassDefinition> permissionClass =
<< "public final class Manifest {\n"; util::make_unique<ClassDefinition>("permission", ClassQualifier::Static, false);
std::unique_ptr<ClassDefinition> permissionGroupClass =
util::make_unique<ClassDefinition>("permission_group", ClassQualifier::Static, false);
bool error = false; bool error = false;
std::vector<xml::Element*> children = el->getChildElements(); std::vector<xml::Element*> children = el->getChildElements();
ClassDefinitionWriterOptions classOptions;
classOptions.useFinalQualifier = true;
classOptions.forceCreationIfEmpty = false;
// First write out permissions.
ClassDefinitionWriter classDef("permission", classOptions);
for (xml::Element* childEl : children) { for (xml::Element* childEl : children) {
if (childEl->namespaceUri.empty() && childEl->name == u"permission") { if (childEl->namespaceUri.empty()) {
error |= !writeSymbol(diag, &classDef, res->file.source, childEl); if (childEl->name == u"permission") {
error |= !writeSymbol(res->file.source, diag, childEl, permissionClass.get());
} else if (childEl->name == u"permission-group") {
error |= !writeSymbol(res->file.source, diag, childEl, permissionGroupClass.get());
}
} }
} }
classDef.writeToStream(out, " ");
// Next write out permission groups. if (error) {
classDef = ClassDefinitionWriter("permission_group", classOptions); return {};
for (xml::Element* childEl : children) {
if (childEl->namespaceUri.empty() && childEl->name == u"permission-group") {
error |= !writeSymbol(diag, &classDef, res->file.source, childEl);
} }
}
classDef.writeToStream(out, " ");
*out << "}\n"; std::unique_ptr<ClassDefinition> manifestClass =
return !error; util::make_unique<ClassDefinition>("Manifest", ClassQualifier::None, false);
manifestClass->addMember(std::move(permissionClass));
manifestClass->addMember(std::move(permissionGroupClass));
return manifestClass;
} }
} // namespace aapt } // namespace aapt

View File

@@ -18,6 +18,7 @@
#define AAPT_JAVA_MANIFESTCLASSGENERATOR_H #define AAPT_JAVA_MANIFESTCLASSGENERATOR_H
#include "Diagnostics.h" #include "Diagnostics.h"
#include "java/ClassDefinition.h"
#include "util/StringPiece.h" #include "util/StringPiece.h"
#include "xml/XmlDom.h" #include "xml/XmlDom.h"
@@ -25,10 +26,7 @@
namespace aapt { namespace aapt {
struct ManifestClassGenerator { std::unique_ptr<ClassDefinition> generateManifestClass(IDiagnostics* diag, xml::XmlResource* res);
bool generate(IDiagnostics* diag, const StringPiece16& package, xml::XmlResource* res,
std::ostream* out);
};
} // namespace aapt } // namespace aapt

View File

@@ -22,6 +22,23 @@
namespace aapt { namespace aapt {
static ::testing::AssertionResult getManifestClassText(IAaptContext* context, xml::XmlResource* res,
std::string* outStr) {
std::unique_ptr<ClassDefinition> manifestClass = generateManifestClass(
context->getDiagnostics(), res);
if (!manifestClass) {
return ::testing::AssertionFailure() << "manifestClass == nullptr";
}
std::stringstream out;
if (!manifestClass->writeJavaFile(manifestClass.get(), "android", true, &out)) {
return ::testing::AssertionFailure() << "failed to write java file";
}
*outStr = out.str();
return ::testing::AssertionSuccess();
}
TEST(ManifestClassGeneratorTest, NameIsProperlyGeneratedFromSymbol) { TEST(ManifestClassGeneratorTest, NameIsProperlyGeneratedFromSymbol) {
std::unique_ptr<IAaptContext> context = test::ContextBuilder().build(); std::unique_ptr<IAaptContext> context = test::ContextBuilder().build();
std::unique_ptr<xml::XmlResource> manifest = test::buildXmlDom(R"EOF( std::unique_ptr<xml::XmlResource> manifest = test::buildXmlDom(R"EOF(
@@ -32,11 +49,8 @@ TEST(ManifestClassGeneratorTest, NameIsProperlyGeneratedFromSymbol) {
<permission-group android:name="foo.bar.PERMISSION" /> <permission-group android:name="foo.bar.PERMISSION" />
</manifest>)EOF"); </manifest>)EOF");
std::stringstream out; std::string actual;
ManifestClassGenerator generator; ASSERT_TRUE(getManifestClassText(context.get(), manifest.get(), &actual));
ASSERT_TRUE(generator.generate(context->getDiagnostics(), u"android", manifest.get(), &out));
std::string actual = out.str();
const size_t permissionClassPos = actual.find("public static final class permission {"); const size_t permissionClassPos = actual.find("public static final class permission {");
const size_t permissionGroupClassPos = const size_t permissionGroupClassPos =
@@ -87,11 +101,8 @@ TEST(ManifestClassGeneratorTest, CommentsAndAnnotationsArePresent) {
<permission android:name="android.permission.SECRET" /> <permission android:name="android.permission.SECRET" />
</manifest>)EOF"); </manifest>)EOF");
std::stringstream out; std::string actual;
ManifestClassGenerator generator; ASSERT_TRUE(getManifestClassText(context.get(), manifest.get(), &actual));
ASSERT_TRUE(generator.generate(context->getDiagnostics(), u"android", manifest.get(), &out));
std::string actual = out.str();
EXPECT_NE(std::string::npos, actual.find( EXPECT_NE(std::string::npos, actual.find(
R"EOF( /** R"EOF( /**

View File

@@ -762,9 +762,24 @@ public:
return true; return true;
} }
std::unique_ptr<ClassDefinition> manifestClass = generateManifestClass(
mContext->getDiagnostics(), manifestXml);
if (!manifestClass) {
// Something bad happened, but we already logged it, so exit.
return false;
}
if (manifestClass->empty()) {
// Empty Manifest class, no need to generate it.
return true;
}
const std::string packageUtf8 = util::utf16ToUtf8(mContext->getCompilationPackage());
std::string outPath = mOptions.generateJavaClassPath.value(); std::string outPath = mOptions.generateJavaClassPath.value();
file::appendPath(&outPath, file::appendPath(&outPath, file::packageToPath(packageUtf8));
file::packageToPath(util::utf16ToUtf8(mContext->getCompilationPackage())));
if (!file::mkdirs(outPath)) { if (!file::mkdirs(outPath)) {
mContext->getDiagnostics()->error( mContext->getDiagnostics()->error(
DiagMessage() << "failed to create directory '" << outPath << "'"); DiagMessage() << "failed to create directory '" << outPath << "'");
@@ -780,13 +795,7 @@ public:
return false; return false;
} }
ManifestClassGenerator generator; if (!ClassDefinition::writeJavaFile(manifestClass.get(), packageUtf8, true, &fout)) {
if (!generator.generate(mContext->getDiagnostics(), mContext->getCompilationPackage(),
manifestXml, &fout)) {
return false;
}
if (!fout) {
mContext->getDiagnostics()->error( mContext->getDiagnostics()->error(
DiagMessage() << "failed writing to '" << outPath << "': " << strerror(errno)); DiagMessage() << "failed writing to '" << outPath << "': " << strerror(errno));
return false; return false;