use of org.jf.dexlib2.iface.instruction.Instruction in project smali by JesusFreke.
the class ImplicitReferenceTest method testImplicitMethodReference.
@Test
public void testImplicitMethodReference() throws RecognitionException, IOException {
ClassDef classDef = SmaliTestUtils.compileSmali("" + ".class public LHelloWorld;\n" + ".super Ljava/lang/Object;\n" + ".method public static main([Ljava/lang/String;)V\n" + " .registers 1\n" + " invoke-static {p0}, toString()V\n" + " invoke-static {p0}, V()V\n" + " invoke-static {p0}, I()V\n" + " return-void\n" + ".end method");
Method mainMethod = null;
for (Method method : classDef.getMethods()) {
if (method.getName().equals("main")) {
mainMethod = method;
}
}
Assert.assertNotNull(mainMethod);
MethodImplementation methodImpl = mainMethod.getImplementation();
Assert.assertNotNull(methodImpl);
List<Instruction> instructions = Lists.newArrayList(methodImpl.getInstructions());
Instruction35c instruction = (Instruction35c) instructions.get(0);
Assert.assertNotNull(instruction);
Assert.assertEquals(Opcode.INVOKE_STATIC, instruction.getOpcode());
MethodReference method = (MethodReference) instruction.getReference();
Assert.assertEquals(classDef.getType(), method.getDefiningClass());
Assert.assertEquals("toString", method.getName());
instruction = (Instruction35c) instructions.get(1);
Assert.assertNotNull(instruction);
Assert.assertEquals(Opcode.INVOKE_STATIC, instruction.getOpcode());
method = (MethodReference) instruction.getReference();
Assert.assertEquals(classDef.getType(), method.getDefiningClass());
Assert.assertEquals("V", method.getName());
instruction = (Instruction35c) instructions.get(2);
Assert.assertNotNull(instruction);
Assert.assertEquals(Opcode.INVOKE_STATIC, instruction.getOpcode());
method = (MethodReference) instruction.getReference();
Assert.assertEquals(classDef.getType(), method.getDefiningClass());
Assert.assertEquals("I", method.getName());
}
use of org.jf.dexlib2.iface.instruction.Instruction in project smali by JesusFreke.
the class CustomMethodInlineTableTest method testCustomMethodInlineTable_Virtual.
@Test
public void testCustomMethodInlineTable_Virtual() throws IOException {
List<ImmutableInstruction> instructions = Lists.newArrayList(new ImmutableInstruction35mi(Opcode.EXECUTE_INLINE, 1, 0, 0, 0, 0, 0, 0), new ImmutableInstruction10x(Opcode.RETURN_VOID));
ImmutableMethodImplementation methodImpl = new ImmutableMethodImplementation(1, instructions, null, null);
ImmutableMethod method = new ImmutableMethod("Lblah;", "blah", null, "V", AccessFlags.PUBLIC.getValue(), null, methodImpl);
ClassDef classDef = new ImmutableClassDef("Lblah;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, null, null, null, null, ImmutableList.of(method));
DexFile dexFile = new ImmutableDexFile(Opcodes.getDefault(), ImmutableList.of(classDef));
ClassPathResolver resolver = new ClassPathResolver(ImmutableList.<String>of(), ImmutableList.<String>of(), ImmutableList.<String>of(), dexFile);
ClassPath classPath = new ClassPath(resolver.getResolvedClassProviders(), false, ClassPath.NOT_ART);
InlineMethodResolver inlineMethodResolver = new CustomInlineMethodResolver(classPath, "Lblah;->blah()V");
MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, inlineMethodResolver, false);
Instruction deodexedInstruction = methodAnalyzer.getInstructions().get(0);
Assert.assertEquals(Opcode.INVOKE_VIRTUAL, deodexedInstruction.getOpcode());
MethodReference methodReference = (MethodReference) ((Instruction35c) deodexedInstruction).getReference();
Assert.assertEquals(method, methodReference);
}
use of org.jf.dexlib2.iface.instruction.Instruction in project smali by JesusFreke.
the class InstructionWriter method write.
public void write(@Nonnull PackedSwitchPayload instruction) {
try {
writer.writeUbyte(0);
writer.writeUbyte(getOpcodeValue(instruction.getOpcode()) >> 8);
List<? extends SwitchElement> elements = instruction.getSwitchElements();
writer.writeUshort(elements.size());
if (elements.size() == 0) {
writer.writeInt(0);
} else {
writer.writeInt(elements.get(0).getKey());
for (SwitchElement element : elements) {
writer.writeInt(element.getOffset());
}
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
use of org.jf.dexlib2.iface.instruction.Instruction in project smali by JesusFreke.
the class InstructionWriter method write.
public void write(@Nonnull SparseSwitchPayload instruction) {
try {
writer.writeUbyte(0);
writer.writeUbyte(getOpcodeValue(instruction.getOpcode()) >> 8);
List<? extends SwitchElement> elements = Ordering.from(switchElementComparator).immutableSortedCopy(instruction.getSwitchElements());
writer.writeUshort(elements.size());
for (SwitchElement element : elements) {
writer.writeInt(element.getKey());
}
for (SwitchElement element : elements) {
writer.writeInt(element.getOffset());
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
use of org.jf.dexlib2.iface.instruction.Instruction in project smali by JesusFreke.
the class ClassPool method internCode.
private void internCode(@Nonnull Method method) {
// this also handles parameter names, which aren't directly tied to the MethodImplementation, even though the debug items are
boolean hasInstruction = false;
MethodImplementation methodImpl = method.getImplementation();
if (methodImpl != null) {
for (Instruction instruction : methodImpl.getInstructions()) {
hasInstruction = true;
if (instruction instanceof ReferenceInstruction) {
Reference reference = ((ReferenceInstruction) instruction).getReference();
switch(instruction.getOpcode().referenceType) {
case ReferenceType.STRING:
dexPool.stringSection.intern((StringReference) reference);
break;
case ReferenceType.TYPE:
dexPool.typeSection.intern((TypeReference) reference);
break;
case ReferenceType.FIELD:
dexPool.fieldSection.intern((FieldReference) reference);
break;
case ReferenceType.METHOD:
dexPool.methodSection.intern((MethodReference) reference);
break;
default:
throw new ExceptionWithContext("Unrecognized reference type: %d", instruction.getOpcode().referenceType);
}
}
}
List<? extends TryBlock> tryBlocks = methodImpl.getTryBlocks();
if (!hasInstruction && tryBlocks.size() > 0) {
throw new ExceptionWithContext("Method %s has no instructions, but has try blocks.", ReferenceUtil.getMethodDescriptor(method));
}
for (TryBlock<? extends ExceptionHandler> tryBlock : methodImpl.getTryBlocks()) {
for (ExceptionHandler handler : tryBlock.getExceptionHandlers()) {
dexPool.typeSection.internNullable(handler.getExceptionType());
}
}
}
}
Aggregations