Search in sources :

Example 21 with Annotation

use of org.jf.dexlib2.iface.Annotation in project smali by JesusFreke.

the class AnnotationPool method intern.

public void intern(@Nonnull Annotation annotation) {
    Integer prev = internedItems.put(annotation, 0);
    if (prev == null) {
        dexPool.typeSection.intern(annotation.getType());
        for (AnnotationElement element : annotation.getElements()) {
            dexPool.stringSection.intern(element.getName());
            dexPool.internEncodedValue(element.getValue());
        }
    }
}
Also used : AnnotationElement(org.jf.dexlib2.iface.AnnotationElement)

Example 22 with Annotation

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

use of org.jf.dexlib2.iface.Annotation in project smali by JesusFreke.

the class SmalideaMethodTest method testArrayData.

public void testArrayData() {
    String text = ".class public LFormat31t;\n" + ".super Ljava/lang/Object;\n" + ".source \"Format31t.smali\"" + "\n" + ".method public test_fill-array-data()V\n" + "    .registers 3\n" + "    .annotation runtime Lorg/junit/Test;\n" + "    .end annotation\n" + "\n" + "    const v0, 6\n" + "    new-array v0, v0, [I\n" + "    fill-array-data v0, :ArrayData\n" + "\n" + "    const v1, 0\n" + "    aget v2, v0, v1\n" + "    const v1, 1\n" + "    invoke-static {v1, v2}, LAssert;->assertEquals(II)V\n" + "\n" + "    const v1, 1\n" + "    aget v2, v0, v1\n" + "    const v1, 2\n" + "    invoke-static {v1, v2}, LAssert;->assertEquals(II)V\n" + "\n" + "    const v1, 2\n" + "    aget v2, v0, v1\n" + "    const v1, 3\n" + "    invoke-static {v1, v2}, LAssert;->assertEquals(II)V\n" + "\n" + "    const v1, 3\n" + "    aget v2, v0, v1\n" + "    const v1, 4\n" + "    invoke-static {v1, v2}, LAssert;->assertEquals(II)V\n" + "\n" + "    const v1, 4\n" + "    aget v2, v0, v1\n" + "    const v1, 5\n" + "    invoke-static {v1, v2}, LAssert;->assertEquals(II)V\n" + "\n" + "    const v1, 5\n" + "    aget v2, v0, v1\n" + "    const v1, 6\n" + "    invoke-static {v1, v2}, LAssert;->assertEquals(II)V\n" + "\n" + "    return-void\n" + "\n" + ":ArrayData\n" + "    .array-data 4\n" + "        1 2 128 -256 65536 0x7fffffff\n" + "    .end array-data\n" + ".end method";
    SmaliFile file = (SmaliFile) myFixture.addFileToProject("my/pkg/blah.smali", text);
    SmaliClass smaliClass = file.getPsiClass();
    SmaliMethod smaliMethod = smaliClass.getMethods()[0];
    SmalideaMethod method = new SmalideaMethod(smaliMethod);
    MethodImplementation impl = method.getImplementation();
    Assert.assertNotNull(impl);
    List<Instruction> instructions = Lists.newArrayList(impl.getInstructions());
    ArrayPayload arrayPayload = (ArrayPayload) instructions.get(28);
    Assert.assertEquals(4, arrayPayload.getElementWidth());
    List<Number> elements = arrayPayload.getArrayElements();
    Assert.assertEquals(6, elements.size());
    Assert.assertEquals(1L, elements.get(0).longValue());
    Assert.assertEquals(2L, elements.get(1).longValue());
    Assert.assertEquals(128L, elements.get(2));
    Assert.assertEquals(-256L, elements.get(3));
    Assert.assertEquals(65536L, elements.get(4));
    Assert.assertEquals(0x7fffffffL, elements.get(5));
}
Also used : SmaliFile(org.jf.smalidea.psi.impl.SmaliFile) MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) SmaliClass(org.jf.smalidea.psi.impl.SmaliClass) SmaliMethod(org.jf.smalidea.psi.impl.SmaliMethod) Instruction(org.jf.dexlib2.iface.instruction.Instruction)

Example 24 with Annotation

use of org.jf.dexlib2.iface.Annotation in project smali by JesusFreke.

the class SmalideaMethodTest method testPackedSwitch.

public void testPackedSwitch() {
    String text = ".class public LFormat31t;\n" + ".super Ljava/lang/Object;\n" + ".source \"Format31t.smali\"" + "\n" + ".method public test_packed-switch()V\n" + "    .registers 1\n" + "    .annotation runtime Lorg/junit/Test;\n" + "    .end annotation\n" + "\n" + "    const v0, 12\n" + "\n" + ":switch\n" + "    packed-switch v0, :PackedSwitch\n" + "\n" + ":Label10\n" + "    invoke-static {}, Lorg/junit/Assert;->fail()V\n" + "    return-void\n" + "\n" + ":Label11\n" + "    invoke-static {}, Lorg/junit/Assert;->fail()V\n" + "    return-void\n" + "\n" + ":Label12\n" + "    return-void\n" + "\n" + ":Label13\n" + "    invoke-static {}, Lorg/junit/Assert;->fail()V\n" + "    return-void\n" + "\n" + ":PackedSwitch\n" + "    .packed-switch 10\n" + "        :Label10\n" + "        :Label11\n" + "        :Label12\n" + "        :Label13\n" + "    .end packed-switch\n" + ".end method";
    SmaliFile file = (SmaliFile) myFixture.addFileToProject("my/pkg/blah.smali", text);
    SmaliClass smaliClass = file.getPsiClass();
    SmaliMethod smaliMethod = smaliClass.getMethods()[0];
    SmalideaMethod method = new SmalideaMethod(smaliMethod);
    MethodImplementation impl = method.getImplementation();
    Assert.assertNotNull(impl);
    List<Instruction> instructions = Lists.newArrayList(impl.getInstructions());
    PackedSwitchPayload packedSwitchPayload = (PackedSwitchPayload) instructions.get(9);
    List<? extends SwitchElement> switchElements = packedSwitchPayload.getSwitchElements();
    Assert.assertEquals(4, switchElements.size());
    checkSwitchElement(switchElements.get(0), 10, 6);
    checkSwitchElement(switchElements.get(1), 11, 14);
    checkSwitchElement(switchElements.get(2), 12, 22);
    checkSwitchElement(switchElements.get(3), 13, 24);
}
Also used : SmaliFile(org.jf.smalidea.psi.impl.SmaliFile) MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) SmaliClass(org.jf.smalidea.psi.impl.SmaliClass) SmaliMethod(org.jf.smalidea.psi.impl.SmaliMethod) Instruction(org.jf.dexlib2.iface.instruction.Instruction)

Aggregations

Annotation (org.jf.dexlib2.iface.Annotation)10 AnnotationElement (org.jf.dexlib2.iface.AnnotationElement)7 EncodedValue (org.jf.dexlib2.iface.value.EncodedValue)7 HashSet (java.util.HashSet)4 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)4 Instruction (org.jf.dexlib2.iface.instruction.Instruction)4 ImmutableAnnotation (org.jf.dexlib2.immutable.ImmutableAnnotation)4 ImmutableAnnotationElement (org.jf.dexlib2.immutable.ImmutableAnnotationElement)4 ArrayList (java.util.ArrayList)3 Nonnull (javax.annotation.Nonnull)3 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)3 ClassDef (org.jf.dexlib2.iface.ClassDef)3 ArrayEncodedValue (org.jf.dexlib2.iface.value.ArrayEncodedValue)3 TypeEncodedValue (org.jf.dexlib2.iface.value.TypeEncodedValue)3 MemoryDataStore (org.jf.dexlib2.writer.io.MemoryDataStore)3 Test (org.junit.Test)3 MethodReplaceAnnotation (com.taobao.android.apatch.annotation.MethodReplaceAnnotation)2 IOException (java.io.IOException)2 Nullable (javax.annotation.Nullable)2 BuilderInstruction21c (org.jf.dexlib2.builder.instruction.BuilderInstruction21c)2