Search in sources :

Example 21 with DexBackedDexFile

use of org.jf.dexlib2.dexbacked.DexBackedDexFile in project smali by JesusFreke.

the class DexEntryFinderTest method testNormalStuff.

@Test
public void testNormalStuff() throws Exception {
    Map<String, DexBackedDexFile> entries = Maps.newHashMap();
    DexBackedDexFile dexFile1 = mock(DexBackedDexFile.class);
    entries.put("/system/framework/framework.jar", dexFile1);
    DexBackedDexFile dexFile2 = mock(DexBackedDexFile.class);
    entries.put("/system/framework/framework.jar:classes2.dex", dexFile2);
    DexEntryFinder testFinder = new DexEntryFinder("blah.oat", new TestMultiDexContainer(entries));
    Assert.assertEquals(dexFile1, testFinder.findEntry("/system/framework/framework.jar", true));
    assertEntryNotFound(testFinder, "system/framework/framework.jar", true);
    assertEntryNotFound(testFinder, "/framework/framework.jar", true);
    assertEntryNotFound(testFinder, "framework/framework.jar", true);
    assertEntryNotFound(testFinder, "/framework.jar", true);
    assertEntryNotFound(testFinder, "framework.jar", true);
    Assert.assertEquals(dexFile1, testFinder.findEntry("system/framework/framework.jar", false));
    Assert.assertEquals(dexFile1, testFinder.findEntry("/framework/framework.jar", false));
    Assert.assertEquals(dexFile1, testFinder.findEntry("framework/framework.jar", false));
    Assert.assertEquals(dexFile1, testFinder.findEntry("/framework.jar", false));
    Assert.assertEquals(dexFile1, testFinder.findEntry("framework.jar", false));
    assertEntryNotFound(testFinder, "ystem/framework/framework.jar", false);
    assertEntryNotFound(testFinder, "ssystem/framework/framework.jar", false);
    assertEntryNotFound(testFinder, "ramework/framework.jar", false);
    assertEntryNotFound(testFinder, "ramework.jar", false);
    assertEntryNotFound(testFinder, "framework", false);
    Assert.assertEquals(dexFile2, testFinder.findEntry("/system/framework/framework.jar:classes2.dex", true));
    assertEntryNotFound(testFinder, "system/framework/framework.jar:classes2.dex", true);
    assertEntryNotFound(testFinder, "framework.jar:classes2.dex", true);
    assertEntryNotFound(testFinder, "classes2.dex", true);
    Assert.assertEquals(dexFile2, testFinder.findEntry("system/framework/framework.jar:classes2.dex", false));
    Assert.assertEquals(dexFile2, testFinder.findEntry("/framework/framework.jar:classes2.dex", false));
    Assert.assertEquals(dexFile2, testFinder.findEntry("framework/framework.jar:classes2.dex", false));
    Assert.assertEquals(dexFile2, testFinder.findEntry("/framework.jar:classes2.dex", false));
    Assert.assertEquals(dexFile2, testFinder.findEntry("framework.jar:classes2.dex", false));
    Assert.assertEquals(dexFile2, testFinder.findEntry(":classes2.dex", false));
    Assert.assertEquals(dexFile2, testFinder.findEntry("classes2.dex", false));
    assertEntryNotFound(testFinder, "ystem/framework/framework.jar:classes2.dex", false);
    assertEntryNotFound(testFinder, "ramework.jar:classes2.dex", false);
    assertEntryNotFound(testFinder, "lasses2.dex", false);
    assertEntryNotFound(testFinder, "classes2", false);
}
Also used : DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) DexEntryFinder(org.jf.dexlib2.DexFileFactory.DexEntryFinder) Test(org.junit.Test)

Example 22 with DexBackedDexFile

use of org.jf.dexlib2.dexbacked.DexBackedDexFile in project smali by JesusFreke.

the class DexWriterTest method testAnnotationElementOrder.

@Test
public void testAnnotationElementOrder() {
    // Elements are out of order wrt to the element name
    ImmutableSet<ImmutableAnnotationElement> elements = ImmutableSet.of(new ImmutableAnnotationElement("zabaglione", ImmutableNullEncodedValue.INSTANCE), new ImmutableAnnotationElement("blah", ImmutableNullEncodedValue.INSTANCE));
    ImmutableAnnotation annotation = new ImmutableAnnotation(AnnotationVisibility.RUNTIME, "Lorg/test/anno;", elements);
    ImmutableClassDef classDef = new ImmutableClassDef("Lorg/test/blah;", 0, "Ljava/lang/Object;", null, null, ImmutableSet.of(annotation), null, null);
    MemoryDataStore dataStore = new MemoryDataStore();
    try {
        DexPool.writeTo(dataStore, new ImmutableDexFile(Opcodes.getDefault(), ImmutableSet.of(classDef)));
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
    DexBackedDexFile dexFile = new DexBackedDexFile(Opcodes.getDefault(), dataStore.getData());
    ClassDef dbClassDef = Iterables.getFirst(dexFile.getClasses(), null);
    Assert.assertNotNull(dbClassDef);
    Annotation dbAnnotation = Iterables.getFirst(dbClassDef.getAnnotations(), null);
    Assert.assertNotNull(dbAnnotation);
    List<AnnotationElement> dbElements = Lists.newArrayList(dbAnnotation.getElements());
    // Ensure that the elements were written out in sorted order
    Assert.assertEquals(2, dbElements.size());
    Assert.assertEquals("blah", dbElements.get(0).getName());
    Assert.assertEquals("zabaglione", dbElements.get(1).getName());
}
Also used : DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) ImmutableAnnotationElement(org.jf.dexlib2.immutable.ImmutableAnnotationElement) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) MemoryDataStore(org.jf.dexlib2.writer.io.MemoryDataStore) IOException(java.io.IOException) ImmutableAnnotation(org.jf.dexlib2.immutable.ImmutableAnnotation) Annotation(org.jf.dexlib2.iface.Annotation) ImmutableAnnotation(org.jf.dexlib2.immutable.ImmutableAnnotation) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) ClassDef(org.jf.dexlib2.iface.ClassDef) ImmutableAnnotationElement(org.jf.dexlib2.immutable.ImmutableAnnotationElement) AnnotationElement(org.jf.dexlib2.iface.AnnotationElement) ImmutableDexFile(org.jf.dexlib2.immutable.ImmutableDexFile) Test(org.junit.Test)

Example 23 with DexBackedDexFile

use of org.jf.dexlib2.dexbacked.DexBackedDexFile in project android by JetBrains.

the class DexParser method getDexStats.

@NotNull
private DexFileStats getDexStats() {
    DexBackedDexFile dexFile;
    try {
        dexFile = myDexFileFuture.get();
    } catch (Exception e) {
        return new DexFileStats(-1, -1, -1);
    }
    int definedMethodCount = 0;
    Set<? extends DexBackedClassDef> classes = dexFile.getClasses();
    for (DexBackedClassDef dexBackedClassDef : classes) {
        definedMethodCount += Iterables.size(dexBackedClassDef.getMethods());
    }
    return new DexFileStats(classes.size(), definedMethodCount, dexFile.getMethodCount());
}
Also used : DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull)

Example 24 with DexBackedDexFile

use of org.jf.dexlib2.dexbacked.DexBackedDexFile in project android by JetBrains.

the class DexParserTest method simpleMethodReferenceTree.

@Test
public void simpleMethodReferenceTree() throws IOException {
    DexBackedDexFile dexFile = getTestDexFile();
    PackageTreeNode packageTreeNode = DexParser.constructMethodRefTreeForDex(dexFile);
    StringBuffer sb = new StringBuffer(100);
    dumpTree(sb, packageTreeNode, 0);
    assertEquals("root: 3,6\n" + "  Test: 3,3\n" + "    void <init>(): 1,1\n" + "    java.lang.Integer get(): 1,1\n" + "    java.util.List getList(): 1,1\n" + "  java: 0,3\n" + "    lang: 0,2\n" + "      Integer: 0,1\n" + "        java.lang.Integer valueOf(int): 0,1\n" + "      Object: 0,1\n" + "        void <init>(): 0,1\n" + "    util: 0,1\n" + "      Collections: 0,1\n" + "        java.util.List emptyList(): 0,1\n", sb.toString());
    assertEquals(6, dexFile.getMethodCount());
}
Also used : DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) Test(org.junit.Test)

Aggregations

DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)20 Test (org.junit.Test)9 IOException (java.io.IOException)7 MemoryDataStore (org.jf.dexlib2.writer.io.MemoryDataStore)5 File (java.io.File)4 DexEntryFinder (org.jf.dexlib2.DexFileFactory.DexEntryFinder)4 NotNull (org.jetbrains.annotations.NotNull)3 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)3 ClassDef (org.jf.dexlib2.iface.ClassDef)3 DexFile (org.jf.dexlib2.iface.DexFile)3 DexBuilder (org.jf.dexlib2.writer.builder.DexBuilder)3 FileInputStream (java.io.FileInputStream)2 BuilderInstruction21c (org.jf.dexlib2.builder.instruction.BuilderInstruction21c)2 DexBackedOdexFile (org.jf.dexlib2.dexbacked.DexBackedOdexFile)2 DexBackedMethodReference (org.jf.dexlib2.dexbacked.reference.DexBackedMethodReference)2 Annotation (org.jf.dexlib2.iface.Annotation)2 AnnotationElement (org.jf.dexlib2.iface.AnnotationElement)2 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)2 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)2 ImmutableAnnotation (org.jf.dexlib2.immutable.ImmutableAnnotation)2