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.
|
|
|
|
*/
|
|
|
|
|
2015-04-10 19:43:55 -07:00
|
|
|
#include "NameMangler.h"
|
2014-11-14 14:48:12 -08:00
|
|
|
#include "Resource.h"
|
|
|
|
#include "ResourceTable.h"
|
|
|
|
#include "ResourceValues.h"
|
2015-10-30 16:31:42 -07:00
|
|
|
#include "ValueVisitor.h"
|
|
|
|
|
2015-10-21 14:42:43 -07:00
|
|
|
#include "java/AnnotationProcessor.h"
|
2016-03-31 13:33:02 -07:00
|
|
|
#include "java/ClassDefinition.h"
|
2015-10-21 14:42:43 -07:00
|
|
|
#include "java/JavaClassGenerator.h"
|
2016-03-10 21:55:04 -08:00
|
|
|
#include "process/SymbolTable.h"
|
2015-08-14 14:26:04 -07:00
|
|
|
#include "util/StringPiece.h"
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2015-04-03 12:08:26 -07:00
|
|
|
#include <algorithm>
|
2014-11-14 14:48:12 -08:00
|
|
|
#include <ostream>
|
|
|
|
#include <set>
|
|
|
|
#include <sstream>
|
|
|
|
#include <tuple>
|
|
|
|
|
|
|
|
namespace aapt {
|
|
|
|
|
2016-03-10 21:55:04 -08:00
|
|
|
JavaClassGenerator::JavaClassGenerator(IAaptContext* context, ResourceTable* table,
|
|
|
|
const JavaClassGeneratorOptions& options) :
|
|
|
|
mContext(context), mTable(table), mOptions(options) {
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const std::set<StringPiece16> sJavaIdentifiers = {
|
|
|
|
u"abstract", u"assert", u"boolean", u"break", u"byte",
|
|
|
|
u"case", u"catch", u"char", u"class", u"const", u"continue",
|
|
|
|
u"default", u"do", u"double", u"else", u"enum", u"extends",
|
|
|
|
u"final", u"finally", u"float", u"for", u"goto", u"if",
|
|
|
|
u"implements", u"import", u"instanceof", u"int", u"interface",
|
|
|
|
u"long", u"native", u"new", u"package", u"private", u"protected",
|
|
|
|
u"public", u"return", u"short", u"static", u"strictfp", u"super",
|
|
|
|
u"switch", u"synchronized", u"this", u"throw", u"throws",
|
|
|
|
u"transient", u"try", u"void", u"volatile", u"while", u"true",
|
|
|
|
u"false", u"null"
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool isValidSymbol(const StringPiece16& symbol) {
|
|
|
|
return sJavaIdentifiers.find(symbol) == sJavaIdentifiers.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Java symbols can not contain . or -, but those are valid in a resource name.
|
|
|
|
* Replace those with '_'.
|
|
|
|
*/
|
2016-03-03 15:39:50 -08:00
|
|
|
static std::string transform(const StringPiece16& symbol) {
|
|
|
|
std::string output = util::utf16ToUtf8(symbol);
|
|
|
|
for (char& c : output) {
|
|
|
|
if (c == '.' || c == '-') {
|
|
|
|
c = '_';
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2016-03-03 15:39:50 -08:00
|
|
|
/**
|
|
|
|
* Transforms an attribute in a styleable to the Java field name:
|
|
|
|
*
|
|
|
|
* <declare-styleable name="Foo">
|
|
|
|
* <attr name="android:bar" />
|
|
|
|
* <attr name="bar" />
|
|
|
|
* </declare-styleable>
|
|
|
|
*
|
|
|
|
* Foo_android_bar
|
|
|
|
* Foo_bar
|
|
|
|
*/
|
|
|
|
static std::string transformNestedAttr(const ResourceNameRef& attrName,
|
|
|
|
const std::string& styleableClassName,
|
|
|
|
const StringPiece16& packageNameToGenerate) {
|
|
|
|
std::string output = styleableClassName;
|
|
|
|
|
|
|
|
// We may reference IDs from other packages, so prefix the entry name with
|
|
|
|
// the package.
|
|
|
|
if (!attrName.package.empty() && packageNameToGenerate != attrName.package) {
|
|
|
|
output += "_" + transform(attrName.package);
|
|
|
|
}
|
|
|
|
output += "_" + transform(attrName.entry);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2016-03-10 21:55:04 -08:00
|
|
|
static void addAttributeFormatDoc(AnnotationProcessor* processor, Attribute* attr) {
|
|
|
|
const uint32_t typeMask = attr->typeMask;
|
|
|
|
if (typeMask & android::ResTable_map::TYPE_REFERENCE) {
|
|
|
|
processor->appendComment(
|
|
|
|
"<p>May be a reference to another resource, in the form\n"
|
2016-03-31 13:33:02 -07:00
|
|
|
"\"<code>@[+][<i>package</i>:]<i>type</i>/<i>name</i></code>\" or a theme\n"
|
|
|
|
"attribute in the form\n"
|
|
|
|
"\"<code>?[<i>package</i>:]<i>type</i>/<i>name</i></code>\".");
|
2016-03-10 21:55:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeMask & android::ResTable_map::TYPE_STRING) {
|
|
|
|
processor->appendComment(
|
|
|
|
"<p>May be a string value, using '\\\\;' to escape characters such as\n"
|
2016-03-31 13:33:02 -07:00
|
|
|
"'\\\\n' or '\\\\uxxxx' for a unicode character;");
|
2016-03-10 21:55:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeMask & android::ResTable_map::TYPE_INTEGER) {
|
|
|
|
processor->appendComment("<p>May be an integer value, such as \"<code>100</code>\".");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeMask & android::ResTable_map::TYPE_BOOLEAN) {
|
|
|
|
processor->appendComment(
|
|
|
|
"<p>May be a boolean value, such as \"<code>true</code>\" or\n"
|
2016-03-31 13:33:02 -07:00
|
|
|
"\"<code>false</code>\".");
|
2016-03-10 21:55:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeMask & android::ResTable_map::TYPE_COLOR) {
|
|
|
|
processor->appendComment(
|
|
|
|
"<p>May be a color value, in the form of \"<code>#<i>rgb</i></code>\",\n"
|
2016-03-31 13:33:02 -07:00
|
|
|
"\"<code>#<i>argb</i></code>\", \"<code>#<i>rrggbb</i></code\", or \n"
|
|
|
|
"\"<code>#<i>aarrggbb</i></code>\".");
|
2016-03-10 21:55:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeMask & android::ResTable_map::TYPE_FLOAT) {
|
|
|
|
processor->appendComment(
|
|
|
|
"<p>May be a floating point value, such as \"<code>1.2</code>\".");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeMask & android::ResTable_map::TYPE_DIMENSION) {
|
|
|
|
processor->appendComment(
|
|
|
|
"<p>May be a dimension value, which is a floating point number appended with a\n"
|
2016-03-31 13:33:02 -07:00
|
|
|
"unit such as \"<code>14.5sp</code>\".\n"
|
|
|
|
"Available units are: px (pixels), dp (density-independent pixels),\n"
|
|
|
|
"sp (scaled pixels based on preferred font size), in (inches), and\n"
|
|
|
|
"mm (millimeters).");
|
2016-03-10 21:55:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeMask & android::ResTable_map::TYPE_FRACTION) {
|
|
|
|
processor->appendComment(
|
|
|
|
"<p>May be a fractional value, which is a floating point number appended with\n"
|
2016-03-31 13:33:02 -07:00
|
|
|
"either % or %p, such as \"<code>14.5%</code>\".\n"
|
|
|
|
"The % suffix always means a percentage of the base size;\n"
|
|
|
|
"the optional %p suffix provides a size relative to some parent container.");
|
2016-03-10 21:55:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeMask & (android::ResTable_map::TYPE_FLAGS | android::ResTable_map::TYPE_ENUM)) {
|
|
|
|
if (typeMask & android::ResTable_map::TYPE_FLAGS) {
|
|
|
|
processor->appendComment(
|
|
|
|
"<p>Must be one or more (separated by '|') of the following "
|
2016-03-31 13:33:02 -07:00
|
|
|
"constant values.</p>");
|
2016-03-10 21:55:04 -08:00
|
|
|
} else {
|
|
|
|
processor->appendComment("<p>Must be one of the following constant values.</p>");
|
|
|
|
}
|
|
|
|
|
|
|
|
processor->appendComment("<table>\n<colgroup align=\"left\" />\n"
|
2016-03-31 13:33:02 -07:00
|
|
|
"<colgroup align=\"left\" />\n"
|
|
|
|
"<colgroup align=\"left\" />\n"
|
|
|
|
"<tr><th>Constant</th><th>Value</th><th>Description</th></tr>\n");
|
2016-03-10 21:55:04 -08:00
|
|
|
for (const Attribute::Symbol& symbol : attr->symbols) {
|
|
|
|
std::stringstream line;
|
|
|
|
line << "<tr><td>" << symbol.symbol.name.value().entry << "</td>"
|
|
|
|
<< "<td>" << std::hex << symbol.value << std::dec << "</td>"
|
|
|
|
<< "<td>" << util::trimWhitespace(symbol.symbol.getComment()) << "</td></tr>";
|
|
|
|
processor->appendComment(line.str());
|
|
|
|
}
|
|
|
|
processor->appendComment("</table>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-16 14:37:48 -07:00
|
|
|
bool JavaClassGenerator::skipSymbol(SymbolState state) {
|
|
|
|
switch (mOptions.types) {
|
|
|
|
case JavaClassGeneratorOptions::SymbolTypes::kAll:
|
|
|
|
return false;
|
|
|
|
case JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate:
|
|
|
|
return state == SymbolState::kUndefined;
|
|
|
|
case JavaClassGeneratorOptions::SymbolTypes::kPublic:
|
|
|
|
return state != SymbolState::kPublic;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-03 15:39:50 -08:00
|
|
|
struct StyleableAttr {
|
|
|
|
const Reference* attrRef;
|
|
|
|
std::string fieldName;
|
2016-04-07 13:24:59 -07:00
|
|
|
std::unique_ptr<SymbolTable::Symbol> symbol;
|
2016-03-03 15:39:50 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool lessStyleableAttr(const StyleableAttr& lhs, const StyleableAttr& rhs) {
|
|
|
|
const ResourceId lhsId = lhs.attrRef->id ? lhs.attrRef->id.value() : ResourceId(0);
|
|
|
|
const ResourceId rhsId = rhs.attrRef->id ? rhs.attrRef->id.value() : ResourceId(0);
|
|
|
|
if (lhsId < rhsId) {
|
|
|
|
return true;
|
|
|
|
} else if (lhsId > rhsId) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return lhs.attrRef->name.value() < rhs.attrRef->name.value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 13:33:02 -07:00
|
|
|
void JavaClassGenerator::addMembersToStyleableClass(const StringPiece16& packageNameToGenerate,
|
|
|
|
const std::u16string& entryName,
|
|
|
|
const Styleable* styleable,
|
|
|
|
ClassDefinition* outStyleableClassDef) {
|
2016-03-03 15:39:50 -08:00
|
|
|
const std::string className = transform(entryName);
|
|
|
|
|
2016-03-31 13:33:02 -07:00
|
|
|
std::unique_ptr<ResourceArrayMember> styleableArrayDef =
|
|
|
|
util::make_unique<ResourceArrayMember>(className);
|
|
|
|
|
2014-11-14 14:48:12 -08:00
|
|
|
// This must be sorted by resource ID.
|
2016-03-03 15:39:50 -08:00
|
|
|
std::vector<StyleableAttr> sortedAttributes;
|
2015-08-14 14:26:04 -07:00
|
|
|
sortedAttributes.reserve(styleable->entries.size());
|
|
|
|
for (const auto& attr : styleable->entries) {
|
2015-05-04 17:40:56 -07:00
|
|
|
// If we are not encoding final attributes, the styleable entry may have no ID
|
|
|
|
// if we are building a static library.
|
2015-08-14 14:26:04 -07:00
|
|
|
assert((!mOptions.useFinal || attr.id) && "no ID set for Styleable entry");
|
|
|
|
assert(attr.name && "no name set for Styleable entry");
|
2016-03-03 15:39:50 -08:00
|
|
|
|
2016-03-31 13:33:02 -07:00
|
|
|
// 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.
|
2016-03-10 21:55:04 -08:00
|
|
|
StyleableAttr styleableAttr = {};
|
|
|
|
styleableAttr.attrRef = &attr;
|
|
|
|
styleableAttr.fieldName = transformNestedAttr(attr.name.value(), className,
|
|
|
|
packageNameToGenerate);
|
|
|
|
|
|
|
|
Reference mangledReference;
|
|
|
|
mangledReference.id = attr.id;
|
|
|
|
mangledReference.name = attr.name;
|
|
|
|
if (mangledReference.name.value().package.empty()) {
|
|
|
|
mangledReference.name.value().package = mContext->getCompilationPackage();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Maybe<ResourceName> mangledName =
|
|
|
|
mContext->getNameMangler()->mangleName(mangledReference.name.value())) {
|
|
|
|
mangledReference.name = mangledName;
|
|
|
|
}
|
|
|
|
|
2016-03-31 13:33:02 -07:00
|
|
|
// Look up the symbol so that we can write out in the comments what are possible
|
|
|
|
// legal values for this attribute.
|
2016-03-10 21:55:04 -08:00
|
|
|
const SymbolTable::Symbol* symbol = mContext->getExternalSymbols()->findByReference(
|
|
|
|
mangledReference);
|
2016-04-07 13:24:59 -07:00
|
|
|
if (symbol && symbol->attribute) {
|
|
|
|
// Copy the symbol data structure because the returned instance can be destroyed.
|
|
|
|
styleableAttr.symbol = util::make_unique<SymbolTable::Symbol>(*symbol);
|
2016-03-10 21:55:04 -08:00
|
|
|
}
|
|
|
|
sortedAttributes.push_back(std::move(styleableAttr));
|
2016-03-03 15:39:50 -08:00
|
|
|
}
|
2016-03-31 13:33:02 -07:00
|
|
|
|
|
|
|
// Sort the attributes by ID.
|
2016-03-03 15:39:50 -08:00
|
|
|
std::sort(sortedAttributes.begin(), sortedAttributes.end(), lessStyleableAttr);
|
|
|
|
|
|
|
|
const size_t attrCount = sortedAttributes.size();
|
|
|
|
if (attrCount > 0) {
|
|
|
|
// Build the comment string for the Styleable. It includes details about the
|
|
|
|
// child attributes.
|
|
|
|
std::stringstream styleableComment;
|
2016-03-10 21:55:04 -08:00
|
|
|
if (!styleable->getComment().empty()) {
|
|
|
|
styleableComment << styleable->getComment() << "\n";
|
|
|
|
} else {
|
|
|
|
styleableComment << "Attributes that can be used with a " << className << ".\n";
|
|
|
|
}
|
2016-03-31 13:33:02 -07:00
|
|
|
|
2016-03-10 21:55:04 -08:00
|
|
|
styleableComment <<
|
|
|
|
"<p>Includes the following attributes:</p>\n"
|
|
|
|
"<table>\n"
|
|
|
|
"<colgroup align=\"left\" />\n"
|
2016-03-03 15:39:50 -08:00
|
|
|
"<colgroup align=\"left\" />\n"
|
|
|
|
"<tr><th>Attribute</th><th>Description</th></tr>\n";
|
2016-03-10 21:55:04 -08:00
|
|
|
|
2016-03-31 13:33:02 -07:00
|
|
|
for (const StyleableAttr& entry : sortedAttributes) {
|
2016-04-07 13:24:59 -07:00
|
|
|
if (!entry.symbol) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic &&
|
|
|
|
!entry.symbol->isPublic) {
|
|
|
|
// Don't write entries for non-public attributes.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-05-06 17:16:06 +01:00
|
|
|
StringPiece16 attrCommentLine = entry.symbol->attribute->getComment();
|
|
|
|
if (attrCommentLine.contains(StringPiece16(u"@removed"))) {
|
|
|
|
// Removed attributes are public but hidden from the documentation, so don't emit
|
|
|
|
// them as part of the class documentation.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-03-03 15:39:50 -08:00
|
|
|
const ResourceName& attrName = entry.attrRef->name.value();
|
2016-03-10 21:55:04 -08:00
|
|
|
styleableComment << "<tr><td>";
|
|
|
|
styleableComment << "<code>{@link #"
|
|
|
|
<< entry.fieldName << " "
|
|
|
|
<< (!attrName.package.empty()
|
|
|
|
? attrName.package : mContext->getCompilationPackage())
|
|
|
|
<< ":" << attrName.entry
|
|
|
|
<< "}</code>";
|
|
|
|
styleableComment << "</td>";
|
|
|
|
|
|
|
|
styleableComment << "<td>";
|
2016-04-07 13:24:59 -07:00
|
|
|
|
|
|
|
// Only use the comment up until the first '.'. This is to stay compatible with
|
|
|
|
// the way old AAPT did it (presumably to keep it short and to avoid including
|
|
|
|
// annotations like @hide which would affect this Styleable).
|
|
|
|
auto iter = std::find(attrCommentLine.begin(), attrCommentLine.end(), u'.');
|
|
|
|
if (iter != attrCommentLine.end()) {
|
|
|
|
attrCommentLine = attrCommentLine.substr(
|
|
|
|
0, (iter - attrCommentLine.begin()) + 1);
|
2016-03-10 21:55:04 -08:00
|
|
|
}
|
2016-04-07 13:24:59 -07:00
|
|
|
styleableComment << attrCommentLine << "</td></tr>\n";
|
2016-03-03 15:39:50 -08:00
|
|
|
}
|
|
|
|
styleableComment << "</table>\n";
|
2016-03-31 13:33:02 -07:00
|
|
|
|
|
|
|
for (const StyleableAttr& entry : sortedAttributes) {
|
2016-04-07 13:24:59 -07:00
|
|
|
if (!entry.symbol) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic &&
|
|
|
|
!entry.symbol->isPublic) {
|
|
|
|
// Don't write entries for non-public attributes.
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-03 15:39:50 -08:00
|
|
|
styleableComment << "@see #" << entry.fieldName << "\n";
|
|
|
|
}
|
2016-03-31 13:33:02 -07:00
|
|
|
|
|
|
|
styleableArrayDef->getCommentBuilder()->appendComment(styleableComment.str());
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-03-31 13:33:02 -07:00
|
|
|
// Add the ResourceIds to the array member.
|
|
|
|
for (const StyleableAttr& styleableAttr : sortedAttributes) {
|
|
|
|
styleableArrayDef->addElement(
|
|
|
|
styleableAttr.attrRef->id ? styleableAttr.attrRef->id.value() : ResourceId(0));
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-03-31 13:33:02 -07:00
|
|
|
// Add the Styleable array to the Styleable class.
|
|
|
|
outStyleableClassDef->addMember(std::move(styleableArrayDef));
|
2014-11-14 14:48:12 -08:00
|
|
|
|
|
|
|
// Now we emit the indices into the array.
|
|
|
|
for (size_t i = 0; i < attrCount; i++) {
|
2016-03-10 21:55:04 -08:00
|
|
|
const StyleableAttr& styleableAttr = sortedAttributes[i];
|
2016-04-07 13:24:59 -07:00
|
|
|
|
|
|
|
if (!styleableAttr.symbol) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic &&
|
|
|
|
!styleableAttr.symbol->isPublic) {
|
|
|
|
// Don't write entries for non-public attributes.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-05-06 17:16:06 +01:00
|
|
|
StringPiece16 comment = styleableAttr.attrRef->getComment();
|
|
|
|
if (styleableAttr.symbol->attribute && comment.empty()) {
|
|
|
|
comment = styleableAttr.symbol->attribute->getComment();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (comment.contains(StringPiece16(u"@removed"))) {
|
|
|
|
// Removed attributes are public but hidden from the documentation, so don't emit them
|
|
|
|
// as part of the class documentation.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-03-10 21:55:04 -08:00
|
|
|
const ResourceName& attrName = styleableAttr.attrRef->name.value();
|
2015-11-06 15:14:35 -08:00
|
|
|
|
2016-03-10 21:55:04 -08:00
|
|
|
StringPiece16 packageName = attrName.package;
|
|
|
|
if (packageName.empty()) {
|
|
|
|
packageName = mContext->getCompilationPackage();
|
2016-03-03 15:39:50 -08:00
|
|
|
}
|
2015-10-30 16:31:42 -07:00
|
|
|
|
2016-03-31 13:33:02 -07:00
|
|
|
std::unique_ptr<IntMember> indexMember = util::make_unique<IntMember>(
|
2016-04-06 16:09:43 -07:00
|
|
|
sortedAttributes[i].fieldName, static_cast<uint32_t>(i));
|
2016-03-31 13:33:02 -07:00
|
|
|
|
|
|
|
AnnotationProcessor* attrProcessor = indexMember->getCommentBuilder();
|
2015-10-30 16:31:42 -07:00
|
|
|
|
2016-03-10 21:55:04 -08:00
|
|
|
if (!comment.empty()) {
|
2016-03-31 13:33:02 -07:00
|
|
|
attrProcessor->appendComment("<p>\n@attr description");
|
|
|
|
attrProcessor->appendComment(comment);
|
2015-10-30 16:31:42 -07:00
|
|
|
} else {
|
2016-03-10 21:55:04 -08:00
|
|
|
std::stringstream defaultComment;
|
|
|
|
defaultComment
|
|
|
|
<< "<p>This symbol is the offset where the "
|
|
|
|
<< "{@link " << packageName << ".R.attr#" << transform(attrName.entry) << "}\n"
|
|
|
|
<< "attribute's value can be found in the "
|
|
|
|
<< "{@link #" << className << "} array.";
|
2016-03-31 13:33:02 -07:00
|
|
|
attrProcessor->appendComment(defaultComment.str());
|
2015-10-30 16:31:42 -07:00
|
|
|
}
|
|
|
|
|
2016-03-31 13:33:02 -07:00
|
|
|
attrProcessor->appendNewLine();
|
2016-03-10 21:55:04 -08:00
|
|
|
|
2016-04-07 13:24:59 -07:00
|
|
|
addAttributeFormatDoc(attrProcessor, styleableAttr.symbol->attribute.get());
|
|
|
|
attrProcessor->appendNewLine();
|
2016-03-10 21:55:04 -08:00
|
|
|
|
|
|
|
std::stringstream doclavaName;
|
|
|
|
doclavaName << "@attr name " << packageName << ":" << attrName.entry;;
|
2016-03-31 13:33:02 -07:00
|
|
|
attrProcessor->appendComment(doclavaName.str());
|
|
|
|
|
|
|
|
outStyleableClassDef->addMember(std::move(indexMember));
|
2015-10-30 16:31:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 13:33:02 -07:00
|
|
|
bool JavaClassGenerator::addMembersToTypeClass(const StringPiece16& packageNameToGenerate,
|
|
|
|
const ResourceTablePackage* package,
|
|
|
|
const ResourceTableType* type,
|
|
|
|
ClassDefinition* outTypeClassDef) {
|
|
|
|
|
2015-08-14 14:26:04 -07:00
|
|
|
for (const auto& entry : type->entries) {
|
2015-10-16 14:37:48 -07:00
|
|
|
if (skipSymbol(entry->symbolStatus.state)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-18 18:33:06 -08:00
|
|
|
ResourceId id;
|
|
|
|
if (package->id && type->id && entry->id) {
|
|
|
|
id = ResourceId(package->id.value(), type->id.value(), entry->id.value());
|
|
|
|
}
|
2015-04-10 19:43:55 -07:00
|
|
|
|
2015-11-06 15:14:35 -08:00
|
|
|
std::u16string unmangledPackage;
|
|
|
|
std::u16string unmangledName = entry->name;
|
2015-04-10 19:43:55 -07:00
|
|
|
if (NameMangler::unmangle(&unmangledName, &unmangledPackage)) {
|
|
|
|
// The entry name was mangled, and we successfully unmangled it.
|
|
|
|
// Check that we want to emit this symbol.
|
2015-08-14 14:26:04 -07:00
|
|
|
if (package->name != unmangledPackage) {
|
2015-04-10 19:43:55 -07:00
|
|
|
// Skip the entry if it doesn't belong to the package we're writing.
|
|
|
|
continue;
|
|
|
|
}
|
2015-11-06 15:14:35 -08:00
|
|
|
} else if (packageNameToGenerate != package->name) {
|
|
|
|
// We are processing a mangled package name,
|
|
|
|
// but this is a non-mangled resource.
|
|
|
|
continue;
|
2015-04-10 19:43:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isValidSymbol(unmangledName)) {
|
2015-10-16 14:37:48 -07:00
|
|
|
ResourceNameRef resourceName(packageNameToGenerate, type->type, unmangledName);
|
2015-04-10 19:43:55 -07:00
|
|
|
std::stringstream err;
|
|
|
|
err << "invalid symbol name '" << resourceName << "'";
|
|
|
|
mError = err.str();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-06 15:14:35 -08:00
|
|
|
if (type->type == ResourceType::kStyleable) {
|
|
|
|
assert(!entry->values.empty());
|
2016-03-31 13:33:02 -07:00
|
|
|
|
2015-11-06 15:14:35 -08:00
|
|
|
const Styleable* styleable = static_cast<const Styleable*>(
|
2016-02-12 22:18:51 -08:00
|
|
|
entry->values.front()->value.get());
|
2016-03-31 13:33:02 -07:00
|
|
|
|
|
|
|
// Comments are handled within this method.
|
|
|
|
addMembersToStyleableClass(packageNameToGenerate, unmangledName, styleable,
|
|
|
|
outTypeClassDef);
|
2015-11-06 15:14:35 -08:00
|
|
|
} else {
|
2016-03-31 13:33:02 -07:00
|
|
|
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) {
|
|
|
|
processor->appendComment(entry->symbolStatus.comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the comments from all configurations of this entry.
|
|
|
|
for (const auto& configValue : entry->values) {
|
|
|
|
processor->appendComment(configValue->value->getComment());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is an Attribute, append the format Javadoc.
|
|
|
|
if (!entry->values.empty()) {
|
|
|
|
if (Attribute* attr = valueCast<Attribute>(entry->values.front()->value.get())) {
|
|
|
|
// We list out the available values for the given attribute.
|
|
|
|
addAttributeFormatDoc(processor, attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
outTypeClassDef->addMember(std::move(resourceMember));
|
2015-04-10 19:43:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-14 14:26:04 -07:00
|
|
|
bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate, std::ostream* out) {
|
2015-10-16 14:37:48 -07:00
|
|
|
return generate(packageNameToGenerate, packageNameToGenerate, out);
|
|
|
|
}
|
|
|
|
|
2016-04-01 19:19:24 -07:00
|
|
|
static void appendJavaDocAnnotations(const std::vector<std::string>& annotations,
|
|
|
|
AnnotationProcessor* processor) {
|
|
|
|
for (const std::string& annotation : annotations) {
|
|
|
|
std::string properAnnotation = "@";
|
|
|
|
properAnnotation += annotation;
|
|
|
|
processor->appendComment(properAnnotation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-16 14:37:48 -07:00
|
|
|
bool JavaClassGenerator::generate(const StringPiece16& packageNameToGenerate,
|
|
|
|
const StringPiece16& outPackageName, std::ostream* out) {
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-03-31 13:33:02 -07:00
|
|
|
ClassDefinition rClass("R", ClassQualifier::None, true);
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2015-08-14 14:26:04 -07:00
|
|
|
for (const auto& package : mTable->packages) {
|
|
|
|
for (const auto& type : package->types) {
|
2015-10-16 14:37:48 -07:00
|
|
|
if (type->type == ResourceType::kAttrPrivate) {
|
2015-11-06 15:14:35 -08:00
|
|
|
continue;
|
2015-10-16 14:37:48 -07:00
|
|
|
}
|
2015-11-06 15:14:35 -08:00
|
|
|
|
2016-03-31 13:33:02 -07:00
|
|
|
const bool forceCreationIfEmpty =
|
2015-11-06 15:14:35 -08:00
|
|
|
(mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic);
|
2016-03-31 13:33:02 -07:00
|
|
|
|
|
|
|
std::unique_ptr<ClassDefinition> classDef = util::make_unique<ClassDefinition>(
|
|
|
|
util::utf16ToUtf8(toString(type->type)), ClassQualifier::Static,
|
|
|
|
forceCreationIfEmpty);
|
|
|
|
|
|
|
|
bool result = addMembersToTypeClass(packageNameToGenerate, package.get(), type.get(),
|
|
|
|
classDef.get());
|
2015-11-06 15:14:35 -08:00
|
|
|
if (!result) {
|
2015-08-14 14:26:04 -07:00
|
|
|
return false;
|
|
|
|
}
|
2015-11-06 15:14:35 -08:00
|
|
|
|
|
|
|
if (type->type == ResourceType::kAttr) {
|
|
|
|
// Also include private attributes in this same class.
|
2016-02-12 22:18:51 -08:00
|
|
|
ResourceTableType* privType = package->findType(ResourceType::kAttrPrivate);
|
|
|
|
if (privType) {
|
2016-03-31 13:33:02 -07:00
|
|
|
result = addMembersToTypeClass(packageNameToGenerate, package.get(), privType,
|
|
|
|
classDef.get());
|
2015-11-06 15:14:35 -08:00
|
|
|
if (!result) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type->type == ResourceType::kStyleable &&
|
|
|
|
mOptions.types == JavaClassGeneratorOptions::SymbolTypes::kPublic) {
|
|
|
|
// When generating a public R class, we don't want Styleable to be part of the API.
|
|
|
|
// It is only emitted for documentation purposes.
|
2016-04-01 19:19:24 -07:00
|
|
|
classDef->getCommentBuilder()->appendComment("@doconly");
|
2015-11-06 15:14:35 -08:00
|
|
|
}
|
2016-03-31 13:33:02 -07:00
|
|
|
|
2016-04-01 19:19:24 -07:00
|
|
|
appendJavaDocAnnotations(mOptions.javadocAnnotations, classDef->getCommentBuilder());
|
|
|
|
|
2016-03-31 13:33:02 -07:00
|
|
|
rClass.addMember(std::move(classDef));
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 19:19:24 -07:00
|
|
|
appendJavaDocAnnotations(mOptions.javadocAnnotations, rClass.getCommentBuilder());
|
|
|
|
|
2016-03-31 13:33:02 -07:00
|
|
|
if (!ClassDefinition::writeJavaFile(&rClass, util::utf16ToUtf8(outPackageName),
|
|
|
|
mOptions.useFinal, out)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-14 14:26:04 -07:00
|
|
|
out->flush();
|
2014-11-14 14:48:12 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aapt
|