use of org.jf.dexlib2.iface.reference.MethodReference 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.reference.MethodReference 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.reference.MethodReference 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());
}
}
}
}
use of org.jf.dexlib2.iface.reference.MethodReference in project smali by JesusFreke.
the class AccessorTest method testAccessors.
@Test
public void testAccessors() throws IOException {
URL url = AccessorTest.class.getClassLoader().getResource("accessorTest.dex");
Assert.assertNotNull(url);
DexFile f = DexFileFactory.loadDexFile(url.getFile(), Opcodes.getDefault());
SyntheticAccessorResolver sar = new SyntheticAccessorResolver(f.getOpcodes(), f.getClasses());
ClassDef accessorTypesClass = null;
ClassDef accessorsClass = null;
for (ClassDef classDef : f.getClasses()) {
String className = classDef.getType();
if (className.equals("Lorg/jf/dexlib2/AccessorTypes;")) {
accessorTypesClass = classDef;
} else if (className.equals("Lorg/jf/dexlib2/AccessorTypes$Accessors;")) {
accessorsClass = classDef;
}
}
Assert.assertNotNull(accessorTypesClass);
Assert.assertNotNull(accessorsClass);
for (Method method : accessorsClass.getMethods()) {
Matcher m = accessorMethodPattern.matcher(method.getName());
if (!m.matches()) {
continue;
}
String type = m.group(1);
String operation = m.group(2);
MethodImplementation methodImpl = method.getImplementation();
Assert.assertNotNull(methodImpl);
for (Instruction instruction : methodImpl.getInstructions()) {
Opcode opcode = instruction.getOpcode();
if (opcode == Opcode.INVOKE_STATIC || opcode == Opcode.INVOKE_STATIC_RANGE) {
MethodReference accessorMethod = (MethodReference) ((ReferenceInstruction) instruction).getReference();
SyntheticAccessorResolver.AccessedMember accessedMember = sar.getAccessedMember(accessorMethod);
Assert.assertNotNull(String.format("Could not resolve accessor for %s_%s", type, operation), accessedMember);
int operationType = operationTypes.get(operation);
Assert.assertEquals(operationType, accessedMember.accessedMemberType);
Assert.assertEquals(String.format("%s_val", type), ((FieldReference) accessedMember.accessedMember).getName());
}
}
}
}
Aggregations