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());
}
}
}
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());
}
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));
}
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);
}
Aggregations