Search in sources :

Example 71 with ClassDef

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

Aggregations

ClassDef (org.jf.dexlib2.iface.ClassDef)47 DexFile (org.jf.dexlib2.iface.DexFile)23 Test (org.junit.Test)21 Method (org.jf.dexlib2.iface.Method)18 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)15 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)14 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)14 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)13 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)12 IndentingWriter (org.jf.util.IndentingWriter)11 File (java.io.File)10 IOException (java.io.IOException)10 HashSet (java.util.HashSet)8 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)8 Instruction (org.jf.dexlib2.iface.instruction.Instruction)8 Nonnull (javax.annotation.Nonnull)7 MethodImplementationBuilder (org.jf.dexlib2.builder.MethodImplementationBuilder)7 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)7 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)7 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)6