Search in sources :

Example 56 with DexFile

use of org.jf.dexlib2.iface.DexFile 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 57 with DexFile

use of org.jf.dexlib2.iface.DexFile 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 58 with DexFile

use of org.jf.dexlib2.iface.DexFile 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

DexFile (org.jf.dexlib2.iface.DexFile)28 ClassDef (org.jf.dexlib2.iface.ClassDef)26 Test (org.junit.Test)17 Nonnull (javax.annotation.Nonnull)15 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)15 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)13 File (java.io.File)12 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)12 IOException (java.io.IOException)10 Method (org.jf.dexlib2.iface.Method)10 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)10 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)8 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)8 MethodImplementationBuilder (org.jf.dexlib2.builder.MethodImplementationBuilder)7 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)7 BuilderInstruction21t (org.jf.dexlib2.builder.instruction.BuilderInstruction21t)6 BuilderInstruction22c (org.jf.dexlib2.builder.instruction.BuilderInstruction22c)6 Instruction (org.jf.dexlib2.iface.instruction.Instruction)6 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)6 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)5