summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorMarcin Kosiba <mkosiba@google.com>2014-09-11 18:34:31 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2014-09-11 18:34:33 +0000
commita0a946647611ea0477c9a25b3d9621fab4cfaaf3 (patch)
tree0b947292cf390eba3990652671a3554af815dc10 /tools
parent3e42b339800f5a3ac840e9545c36f3445b5c64b3 (diff)
parent0f3a5a6559a955ea20f1ae2d3e0995f3fa821c90 (diff)
downloadframeworks_base-a0a946647611ea0477c9a25b3d9621fab4cfaaf3.zip
frameworks_base-a0a946647611ea0477c9a25b3d9621fab4cfaaf3.tar.gz
frameworks_base-a0a946647611ea0477c9a25b3d9621fab4cfaaf3.tar.bz2
Merge "Don't use reflection when generating the onResourcesLoaded callback." into lmp-dev
Diffstat (limited to 'tools')
-rw-r--r--tools/aapt/Command.cpp3
-rw-r--r--tools/aapt/Resource.cpp174
2 files changed, 73 insertions, 104 deletions
diff --git a/tools/aapt/Command.cpp b/tools/aapt/Command.cpp
index 27e60f3..41d8502 100644
--- a/tools/aapt/Command.cpp
+++ b/tools/aapt/Command.cpp
@@ -2390,7 +2390,8 @@ int doPackage(Bundle* bundle)
char* packageString = strtok(libs.lockBuffer(libs.length()), ":");
while (packageString != NULL) {
// Write the R.java file out with the correct package name
- err = writeResourceSymbols(bundle, assets, String8(packageString), true, false);
+ err = writeResourceSymbols(bundle, assets, String8(packageString), true,
+ bundle->getBuildSharedLibrary());
if (err < 0) {
goto bail;
}
diff --git a/tools/aapt/Resource.cpp b/tools/aapt/Resource.cpp
index 5deeca2..afec5ed 100644
--- a/tools/aapt/Resource.cpp
+++ b/tools/aapt/Resource.cpp
@@ -1926,110 +1926,75 @@ static String16 getAttributeComment(const sp<AaptAssets>& assets,
return String16();
}
-static void writeResourceLoadedCallback(FILE* fp, int indent) {
- IndentPrinter p(fp, 4);
- p.indent(indent);
- p.println("private static void rewriteIntArrayField(java.lang.reflect.Field field, int packageId) throws IllegalAccessException {");
- {
- p.indent();
- p.println("int requiredModifiers = java.lang.reflect.Modifier.STATIC | java.lang.reflect.Modifier.PUBLIC;");
- p.println("if ((field.getModifiers() & requiredModifiers) != requiredModifiers) {");
- {
- p.indent();
- p.println("throw new IllegalArgumentException(\"Field \" + field.getName() + \" is not rewritable\");");
- p.indent(-1);
- }
- p.println("}");
- p.println("if (field.getType() != int[].class) {");
- {
- p.indent();
- p.println("throw new IllegalArgumentException(\"Field \" + field.getName() + \" is not an int array\");");
- p.indent(-1);
- }
- p.println("}");
- p.println("int[] array = (int[]) field.get(null);");
- p.println("for (int i = 0; i < array.length; i++) {");
- {
- p.indent();
- p.println("array[i] = (array[i] & 0x00ffffff) | (packageId << 24);");
- p.indent(-1);
- }
- p.println("}");
- p.indent(-1);
- }
- p.println("}");
- p.println();
- p.println("private static void rewriteIntField(java.lang.reflect.Field field, int packageId) throws IllegalAccessException {");
- {
- p.indent();
- p.println("int requiredModifiers = java.lang.reflect.Modifier.STATIC | java.lang.reflect.Modifier.PUBLIC;");
- p.println("int bannedModifiers = java.lang.reflect.Modifier.FINAL;");
- p.println("int mod = field.getModifiers();");
- p.println("if ((mod & requiredModifiers) != requiredModifiers || (mod & bannedModifiers) != 0) {");
- {
- p.indent();
- p.println("throw new IllegalArgumentException(\"Field \" + field.getName() + \" is not rewritable\");");
- p.indent(-1);
- }
- p.println("}");
- p.println("if (field.getType() != int.class && field.getType() != Integer.class) {");
- {
- p.indent();
- p.println("throw new IllegalArgumentException(\"Field \" + field.getName() + \" is not an int\");");
- p.indent(-1);
- }
- p.println("}");
- p.println("int resId = field.getInt(null);");
- p.println("field.setInt(null, (resId & 0x00ffffff) | (packageId << 24));");
- p.indent(-1);
- }
- p.println("}");
- p.println();
- p.println("public static void onResourcesLoaded(int assignedPackageId) throws Exception {");
- {
- p.indent();
- p.println("Class<?>[] declaredClasses = R.class.getDeclaredClasses();");
- p.println("for (Class<?> clazz : declaredClasses) {");
- {
- p.indent();
- p.println("if (clazz.getSimpleName().equals(\"styleable\")) {");
- {
- p.indent();
- p.println("for (java.lang.reflect.Field field : clazz.getDeclaredFields()) {");
- {
- p.indent();
- p.println("if (field.getType() == int[].class) {");
- {
- p.indent();
- p.println("rewriteIntArrayField(field, assignedPackageId);");
- p.indent(-1);
- }
- p.println("}");
- p.indent(-1);
- }
- p.println("}");
- p.indent(-1);
- }
- p.println("} else {");
- {
- p.indent();
- p.println("for (java.lang.reflect.Field field : clazz.getDeclaredFields()) {");
- {
- p.indent();
- p.println("rewriteIntField(field, assignedPackageId);");
- p.indent(-1);
- }
- p.println("}");
- p.indent(-1);
- }
- p.println("}");
- p.indent(-1);
+static status_t writeResourceLoadedCallbackForLayoutClasses(
+ FILE* fp, const sp<AaptAssets>& assets,
+ const sp<AaptSymbols>& symbols, int indent, bool includePrivate)
+{
+ String16 attr16("attr");
+ String16 package16(assets->getPackage());
+
+ const char* indentStr = getIndentSpace(indent);
+ bool hasErrors = false;
+
+ size_t i;
+ size_t N = symbols->getNestedSymbols().size();
+ for (i=0; i<N; i++) {
+ sp<AaptSymbols> nsymbols = symbols->getNestedSymbols().valueAt(i);
+ String8 realClassName(symbols->getNestedSymbols().keyAt(i));
+ String8 nclassName(flattenSymbol(realClassName));
+
+ fprintf(fp,
+ "%sfor(int i = 0; i < styleable.%s.length; ++i) {\n"
+ "%sstyleable.%s[i] = (styleable.%s[i] & 0x00ffffff) | (packageId << 24);\n"
+ "%s}\n",
+ indentStr, nclassName.string(),
+ getIndentSpace(indent+1), nclassName.string(), nclassName.string(),
+ indentStr);
+ }
+
+ return hasErrors ? UNKNOWN_ERROR : NO_ERROR;
+}
+
+static status_t writeResourceLoadedCallback(
+ FILE* fp, const sp<AaptAssets>& assets, bool includePrivate,
+ const sp<AaptSymbols>& symbols, const String8& className, int indent)
+{
+ size_t i;
+ status_t err = NO_ERROR;
+
+ size_t N = symbols->getSymbols().size();
+ for (i=0; i<N; i++) {
+ const AaptSymbolEntry& sym = symbols->getSymbols().valueAt(i);
+ if (sym.typeCode == AaptSymbolEntry::TYPE_UNKNOWN) {
+ continue;
+ }
+ if (!assets->isJavaSymbol(sym, includePrivate)) {
+ continue;
+ }
+ String8 flat_name(flattenSymbol(sym.name));
+ fprintf(fp,
+ "%s%s.%s = (%s.%s & 0x00ffffff) | (packageId << 24);\n",
+ getIndentSpace(indent), className.string(), flat_name.string(),
+ className.string(), flat_name.string());
+ }
+
+ N = symbols->getNestedSymbols().size();
+ for (i=0; i<N; i++) {
+ sp<AaptSymbols> nsymbols = symbols->getNestedSymbols().valueAt(i);
+ String8 nclassName(symbols->getNestedSymbols().keyAt(i));
+ if (nclassName == "styleable") {
+ err = writeResourceLoadedCallbackForLayoutClasses(
+ fp, assets, nsymbols, indent, includePrivate);
+ } else {
+ err = writeResourceLoadedCallback(fp, assets, includePrivate, nsymbols,
+ nclassName, indent);
+ }
+ if (err != NO_ERROR) {
+ return err;
}
- p.println("}");
- p.indent(-1);
}
- p.println("}");
- p.println();
+
+ return NO_ERROR;
}
static status_t writeLayoutClasses(
@@ -2485,7 +2450,10 @@ static status_t writeSymbolClass(
}
if (emitCallback) {
- writeResourceLoadedCallback(fp, indent);
+ fprintf(fp, "%spublic static void onResourcesLoaded(int packageId) {\n",
+ getIndentSpace(indent));
+ writeResourceLoadedCallback(fp, assets, includePrivate, symbols, className, indent + 1);
+ fprintf(fp, "%s}\n", getIndentSpace(indent));
}
indent--;