diff options
author | Yohann Roussel <yroussel@google.com> | 2014-12-12 14:53:33 +0100 |
---|---|---|
committer | Yohann Roussel <yroussel@google.com> | 2015-01-13 19:00:32 +0100 |
commit | 72a69488b2ac109bee04762f66696cf10492bc39 (patch) | |
tree | a8b6658dbfcbe66e89c20f977e2346b187a7b700 | |
parent | f8512fa88990d0e567b0459b7618c67d5c942ddb (diff) | |
download | toolchain_jack-72a69488b2ac109bee04762f66696cf10492bc39.tar.gz toolchain_jack-72a69488b2ac109bee04762f66696cf10492bc39.tar.bz2 toolchain_jack-72a69488b2ac109bee04762f66696cf10492bc39.zip |
Use DexBuffer instead of dexdump in multidex tests
Bug: 18198750
Change-Id: Ic091f405584cf1857eb595fdbf9a97ed9cc8113c
6 files changed, 102 insertions, 69 deletions
diff --git a/dexcomparator/src/com/android/jack/DexComparator.java b/dexcomparator/src/com/android/jack/DexComparator.java index f685049e..5712f00a 100644 --- a/dexcomparator/src/com/android/jack/DexComparator.java +++ b/dexcomparator/src/com/android/jack/DexComparator.java @@ -163,7 +163,7 @@ public class DexComparator { /* build a lookup table for candidate classes */ HashMap<String, ClassDef> candidateClassDefItemLookUpTable = new HashMap<String, ClassDef>(); for (ClassDef classDef : candidateDexFile.classDefs()) { - String typeName = candidateDexFile.typeNames().get(classDef.getTypeIndex()); + String typeName = classDef.getTypeName(); candidateClassDefItemLookUpTable.put(typeName, classDef); } @@ -172,9 +172,9 @@ public class DexComparator { for (ClassDef classDefItem : refClassDefs) { if (!IGNORE_ANONYMOUS_CLASSES - || !isAnomymousTypeName(referenceDexFile.typeNames().get(classDefItem.getTypeIndex()))) { + || !isAnomymousTypeName(classDefItem.getTypeName())) { - String className = getClassName(referenceDexFile, classDefItem); + String className = classDefItem.getTypeName(); ClassDef candidateClassDefItem = candidateClassDefItemLookUpTable.get(className); @@ -192,11 +192,11 @@ public class DexComparator { } else { logger.log( - ERROR_LEVEL, "Class {0} NOK: missing", getClassName(referenceDexFile, classDefItem)); + ERROR_LEVEL, "Class {0} NOK: missing", classDefItem.getTypeName()); if (!TOLERATE_MISSING_SYNTHETICS || !isSynthetic(classDefItem.getAccessFlags())) { throw new DifferenceFoundException("Class " - + getClassName(referenceDexFile, classDefItem) + " was not found in candidate."); + + classDefItem.getTypeName() + " was not found in candidate."); } } } @@ -204,8 +204,8 @@ public class DexComparator { if (strict) { for (ClassDef classDefItem : candidateClassDefItemLookUpTable.values()) { if (!IGNORE_ANONYMOUS_CLASSES || !isAnomymousTypeName( - candidateDexFile.typeNames().get(classDefItem.getTypeIndex()))) { - String className = getClassName(candidateDexFile, classDefItem); + classDefItem.getTypeName())) { + String className = classDefItem.getTypeName(); logger.log( ERROR_LEVEL, "Class {0} NOK: missing", className); @@ -317,7 +317,7 @@ public class DexComparator { private void checkAccessFlags(ClassDef classDefItem, ClassDef candidateClassDefItem) throws DifferenceFoundException { - String className = getClassName(referenceDexFile, classDefItem); + String className = classDefItem.getTypeName(); int candidateAccessFlags = candidateClassDefItem.getAccessFlags(); int refAccessFlags = classDefItem.getAccessFlags(); if (refAccessFlags == candidateAccessFlags) { @@ -336,7 +336,7 @@ public class DexComparator { private void checkClassData(ClassDef classDefItem, ClassDef candidateClassDefItem) throws DifferenceFoundException { - String className = getClassName(referenceDexFile, classDefItem); + String className = classDefItem.getTypeName(); boolean referenceDexFileHasClassData = classDefItem.getClassDataOffset() != 0; boolean candidateDexFileHasClassData = candidateClassDefItem.getClassDataOffset() != 0; @@ -375,7 +375,7 @@ public class DexComparator { private void checkMethods( ClassData classDataItem, ClassData candidateClassDataItem, ClassDef classDefItem) throws DifferenceFoundException { - String className = getClassName(referenceDexFile, classDefItem); + String className = classDefItem.getTypeName(); ClassData.Method[] methods = classDataItem.allMethods(); ClassData.Method[] candidateMethods = candidateClassDataItem.allMethods(); @@ -386,7 +386,7 @@ public class DexComparator { private void checkFields( ClassData classDataItem, ClassData candidateClassDataItem, ClassDef classDefItem) throws DifferenceFoundException { - String className = getClassName(referenceDexFile, classDefItem); + String className = classDefItem.getTypeName(); /* Instance fields */ { @@ -409,7 +409,7 @@ public class DexComparator { private void checkInterfaces(ClassDef classDefItem, ClassDef candidateClassDefItem) throws DifferenceFoundException { - String className = getClassName(referenceDexFile, classDefItem); + String className = classDefItem.getTypeName(); short[] interfaces = classDefItem.getInterfaces(); short[] candidateInterfaces = candidateClassDefItem.getInterfaces(); @@ -442,7 +442,7 @@ public class DexComparator { private void checkSuperclass(ClassDef classDefItem, ClassDef candidateClassDefItem) throws DifferenceFoundException { - String className = getClassName(referenceDexFile, classDefItem); + String className = classDefItem.getTypeName(); String superClass = (classDefItem.getSupertypeIndex() == ClassDef.NO_INDEX) ? ("empty") : (getSuperclassName(referenceDexFile, classDefItem)); String candidateSuperClass = @@ -935,10 +935,6 @@ public class DexComparator { return new DebugInfo(decoder, dex, codeItem, bai.getPosition() - codeItem.getDebugInfoOffset()); } - private static String getClassName(DexBuffer dex, ClassDef classDef) { - return dex.typeNames().get(classDef.getTypeIndex()); - } - private static String getMethodName(DexBuffer dex, int methodIndex) { MethodId methodId = dex.methodIds().get(methodIndex); return dex.strings().get(methodId.getNameIndex()); diff --git a/dx/src/com/android/jack/dx/io/ClassDef.java b/dx/src/com/android/jack/dx/io/ClassDef.java index 7128905f..68506e72 100644 --- a/dx/src/com/android/jack/dx/io/ClassDef.java +++ b/dx/src/com/android/jack/dx/io/ClassDef.java @@ -16,6 +16,8 @@ package com.android.jack.dx.io; +import javax.annotation.Nonnull; + /** * A type definition. */ @@ -62,6 +64,11 @@ public final class ClassDef { return typeIndex; } + @Nonnull + public String getTypeName() { + return buffer.typeNames().get(typeIndex); + } + public int getSupertypeIndex() { return supertypeIndex; } @@ -101,7 +108,7 @@ public final class ClassDef { } StringBuilder result = new StringBuilder(); - result.append(buffer.typeNames().get(typeIndex)); + result.append(getTypeName()); if (supertypeIndex != NO_INDEX) { result.append(" extends ").append(buffer.typeNames().get(supertypeIndex)); } diff --git a/jack-tests/tests/com/android/jack/multidex/MultiDexTests.java b/jack-tests/tests/com/android/jack/multidex/MultiDexTests.java index 88846ded..e462024c 100644 --- a/jack-tests/tests/com/android/jack/multidex/MultiDexTests.java +++ b/jack-tests/tests/com/android/jack/multidex/MultiDexTests.java @@ -23,6 +23,8 @@ import com.android.jack.Options; import com.android.jack.TestTools; import com.android.jack.backend.dex.DexFileWriter; import com.android.jack.backend.dex.MultiDexLegacy; +import com.android.jack.dx.io.ClassDef; +import com.android.jack.dx.io.DexBuffer; import com.android.jack.library.FileType; import com.android.jack.preprocessor.PreProcessor; import com.android.jack.shrob.ListingComparator; @@ -35,7 +37,7 @@ import com.android.jack.test.toolchain.AbstractTestTools; import com.android.jack.test.toolchain.DummyToolchain; import com.android.jack.test.toolchain.IToolchain; import com.android.jack.test.toolchain.JackApiToolchain; -import com.android.jack.util.ExecuteFile; +import com.android.sched.util.TextUtils; import junit.framework.Assert; @@ -43,13 +45,11 @@ import org.junit.BeforeClass; import org.junit.Test; import org.junit.experimental.categories.Category; -import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -179,7 +179,7 @@ public class MultiDexTests { + File.pathSeparator + frameworks.getPath(), out, false); - File outList = getListingOfDex(new File(out, "classes.dex")); + String outList = getListingOfDex(new File(out, "classes.dex")); ListingComparator.compare(new File(testFolder, "ref-list-001.txt"), outList); Assert.assertFalse(new File(out, "classes2.dex").exists()); return; @@ -198,9 +198,9 @@ public class MultiDexTests { + File.pathSeparator + frameworks.getPath(), out, false); - File outList = getListingOfDex(new File(out, "classes.dex")); + String outList = getListingOfDex(new File(out, "classes.dex")); ListingComparator.compare(new File(testFolder, "ref-list-002-1.txt"), outList); - File outList2 = getListingOfDex(new File(out, "classes2.dex")); + String outList2 = getListingOfDex(new File(out, "classes2.dex")); ListingComparator.compare(new File(testFolder, "ref-list-002-2.txt"), outList2); Assert.assertFalse(new File(out, "classes3.dex").exists()); return; @@ -219,9 +219,9 @@ public class MultiDexTests { + File.pathSeparator + frameworks.getPath(), out, false); - File outList = getListingOfDex(new File(out, "classes.dex")); + String outList = getListingOfDex(new File(out, "classes.dex")); ListingComparator.compare(new File(testFolder, "ref-list-003-1.txt"), outList); - File outList2 = getListingOfDex(new File(out, "classes2.dex")); + String outList2 = getListingOfDex(new File(out, "classes2.dex")); ListingComparator.compare(new File(testFolder, "ref-list-003-2.txt"), outList2); Assert.assertFalse(new File(out, "classes3.dex").exists()); return; @@ -242,41 +242,24 @@ public class MultiDexTests { toolchain.addProperty(PreProcessor.FILE.getName(), configFile.getAbsolutePath()); } - private File getListingOfDex(@Nonnull File dex) throws IOException, FileNotFoundException { + private String getListingOfDex(@Nonnull File dex) throws IOException { assert dex.isFile(); - ExecuteFile exec = - new ExecuteFile(new String[]{ - "bash", "-c", AbstractTestTools.getPrebuilt("dexdump").getAbsolutePath() + " " - + dex.getAbsolutePath() + - " | grep \" Class descriptor : \" | cut -d\\' -f2 | sed -e 's/$/:/'"}); - - File outList = TestTools.createTempFile("types", ".txt"); - - exec.setOut(outList); - Assert.assertTrue(exec.run()); - return outList; + StringBuilder sb = new StringBuilder(); + for (ClassDef def : new DexBuffer(dex).classDefs()) { + sb.append(def.getTypeName()); + sb.append(":"); + sb.append(TextUtils.LINE_SEPARATOR); + } + return sb.toString(); } private int getTypeCountInDex(@Nonnull File dex) throws IOException, FileNotFoundException { assert dex.isFile(); - ExecuteFile exec = - new ExecuteFile(new String[]{ - "bash", "-c", AbstractTestTools.getPrebuilt("dexdump").getAbsolutePath() + " " - + dex.getAbsolutePath() + - " | grep \" Class descriptor : \" | wc -l"}); - - File out = TestTools.createTempFile("typeNumber", ".txt"); - - exec.setOut(out); - Assert.assertTrue(exec.run()); - BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(out))); - try { - String readLine = reader.readLine(); - assert readLine != null; - return Integer.parseInt(readLine.trim()); - } finally { - reader.close(); + int count = 0; + for (ClassDef def : new DexBuffer(dex).classDefs()) { + count++; } + return count; } @Nonnull @@ -379,12 +362,12 @@ public class MultiDexTests { /* zipFile = */ false, testFolder); - File outList = getListingOfDex(new File(out, "classes.dex")); + String outList = getListingOfDex(new File(out, "classes.dex")); // The old toolchain is doing a little better than us here it seems to identify when // InterfaceWithEnum.class instance is used or not. ListingComparator.compare( new File(testFolder,"ref-list-002-1.txt"), outList); - File outList2 = getListingOfDex(new File(out, "classes2.dex")); + String outList2 = getListingOfDex(new File(out, "classes2.dex")); ListingComparator.compare( new File(testFolder,"ref-list-002-2.txt"), outList2); Assert.assertFalse(new File(out, "classes3.dex").exists()); @@ -463,12 +446,12 @@ public class MultiDexTests { + File.pathSeparator + frameworks.getPath(), out, false); - File outList = getListingOfDex(new File(out, "classes.dex")); + String outList = getListingOfDex(new File(out, "classes.dex")); // The old toolchain is doing a little better than us here it seems to identify when // InterfaceWithEnum.class instance is used or not. ListingComparator.compare( new File(testFolder,"ref-list-002-1.txt"), outList); - File outList2 = getListingOfDex(new File(out, "classes2.dex")); + String outList2 = getListingOfDex(new File(out, "classes2.dex")); ListingComparator.compare( new File(testFolder,"ref-list-002-2.txt"), outList2); Assert.assertFalse(new File(out, "classes3.dex").exists()); @@ -495,12 +478,12 @@ public class MultiDexTests { + File.pathSeparator + frameworks.getPath(), out, false); - File outList = getListingOfDex(new File(out, "classes.dex")); + String outList = getListingOfDex(new File(out, "classes.dex")); // The old toolchain is doing a little better than us here it seems to identify when // InterfaceWithEnum.class instance is used or not. ListingComparator.compare( new File(testFolder,"ref-list-002-1.txt"), outList); - File outList2 = getListingOfDex(new File(out, "classes2.dex")); + String outList2 = getListingOfDex(new File(out, "classes2.dex")); ListingComparator.compare( new File(testFolder,"ref-list-002-2.txt"), outList2); Assert.assertFalse(new File(out, "classes3.dex").exists()); @@ -524,10 +507,10 @@ public class MultiDexTests { + File.pathSeparator + annotations.getPath() + File.pathSeparator + frameworks.getPath(), out, false); - File outList = getListingOfDex(new File(out, "classes.dex")); + String outList = getListingOfDex(new File(out, "classes.dex")); ListingComparator.compare( new File(testFolder,"ref-list-003-1.txt"), outList); - File outList2 = getListingOfDex(new File(out, "classes2.dex")); + String outList2 = getListingOfDex(new File(out, "classes2.dex")); ListingComparator.compare( new File(testFolder,"ref-list-003-2.txt"), outList2); Assert.assertFalse(new File(out, "classes3.dex").exists()); diff --git a/jack/src/com/android/jack/tools/merger/JackMerger.java b/jack/src/com/android/jack/tools/merger/JackMerger.java index f11c7506..bce76f50 100644 --- a/jack/src/com/android/jack/tools/merger/JackMerger.java +++ b/jack/src/com/android/jack/tools/merger/JackMerger.java @@ -72,7 +72,7 @@ public class JackMerger extends MergerTools { for (ClassDef classDefToMerge : dexToMerge.classDefs()) { List<String> typeNames = dexToMerge.typeNames(); - String typeNameDesc = typeNames.get(classDefToMerge.getTypeIndex()); + String typeNameDesc = classDefToMerge.getTypeName(); CstType superType = null; int supertypeIndex = classDefToMerge.getSupertypeIndex(); if (supertypeIndex != ClassDef.NO_INDEX) { diff --git a/jack/tests/com/android/jack/shrob/AbstractListingComparator.java b/jack/tests/com/android/jack/shrob/AbstractListingComparator.java index a40855ef..eb1b5808 100644 --- a/jack/tests/com/android/jack/shrob/AbstractListingComparator.java +++ b/jack/tests/com/android/jack/shrob/AbstractListingComparator.java @@ -24,6 +24,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; +import java.io.StringReader; import java.util.logging.Level; import java.util.logging.Logger; @@ -31,6 +32,45 @@ import javax.annotation.CheckForNull; import javax.annotation.Nonnull; public abstract class AbstractListingComparator { + protected static interface Readable { + @Nonnull + BufferedReader openReader() throws IOException; + } + + protected static class FileReadable implements Readable { + + @Nonnull + private final File input; + + protected FileReadable(@Nonnull File input) { + this.input = input; + } + + @Nonnull + @Override + public BufferedReader openReader() throws IOException { + return new BufferedReader(new InputStreamReader(new FileInputStream(input))); + } + + } + + protected static class StringReadable implements Readable { + + @Nonnull + private final String input; + + protected StringReadable(@Nonnull String input) { + this.input = input; + } + + @Nonnull + @Override + public BufferedReader openReader() { + return new BufferedReader(new StringReader(input)); + } + + } + protected static class ParseException extends IOException { private static final long serialVersionUID = 1L; diff --git a/jack/tests/com/android/jack/shrob/ListingComparator.java b/jack/tests/com/android/jack/shrob/ListingComparator.java index e781b414..917ca259 100644 --- a/jack/tests/com/android/jack/shrob/ListingComparator.java +++ b/jack/tests/com/android/jack/shrob/ListingComparator.java @@ -35,14 +35,21 @@ public class ListingComparator extends AbstractListingComparator { public static void compare(@Nonnull File reference, @Nonnull File candidate) throws IOException, DifferenceFoundException { - new ListingComparator().compareFiles(reference, candidate); + new ListingComparator().compareReadables(new FileReadable(reference), + new FileReadable(candidate)); } - private void compareFiles(@Nonnull File reference, @Nonnull File candidate) + public static void compare(@Nonnull File reference, @Nonnull String candidate) + throws IOException, DifferenceFoundException { + new ListingComparator().compareReadables(new FileReadable(reference), + new StringReadable(candidate)); + } + + private void compareReadables(@Nonnull Readable reference, @Nonnull Readable candidate) throws IOException, DifferenceFoundException { List<String> candidateTypesList; { - BufferedReader candidateReader = createStreamReader(candidate); + BufferedReader candidateReader = candidate.openReader(); try { candidateTypesList = getTypeList(candidateReader); } finally { @@ -50,7 +57,7 @@ public class ListingComparator extends AbstractListingComparator { } } - BufferedReader referenceReader = createStreamReader(reference); + BufferedReader referenceReader = reference.openReader(); try { String currentReferenceLine = referenceReader.readLine(); while (currentReferenceLine != null) { @@ -61,7 +68,7 @@ public class ListingComparator extends AbstractListingComparator { int typeIndex = candidateTypesList.indexOf(currentReferenceLine); if (typeIndex != -1) { candidateTypesList.remove(currentReferenceLine); - BufferedReader candidateReader = createStreamReader(candidate); + BufferedReader candidateReader = candidate.openReader(); try { if (findLine(currentReferenceLine, candidateReader)) { currentReferenceLine = checkMembers(referenceReader, candidateReader, currentType); |