Search in sources :

Example 21 with ReferenceInstruction

use of org.jf.dexlib2.iface.instruction.ReferenceInstruction 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());
            }
        }
    }
}
Also used : MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) Matcher(java.util.regex.Matcher) Method(org.jf.dexlib2.iface.Method) Instruction(org.jf.dexlib2.iface.instruction.Instruction) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) URL(java.net.URL) DexFile(org.jf.dexlib2.iface.DexFile) SyntheticAccessorResolver(org.jf.dexlib2.util.SyntheticAccessorResolver) ClassDef(org.jf.dexlib2.iface.ClassDef) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) Test(org.junit.Test)

Aggregations

ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)12 Instruction (org.jf.dexlib2.iface.instruction.Instruction)10 Opcode (org.jf.dexlib2.Opcode)8 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)8 TypeReference (org.jf.dexlib2.iface.reference.TypeReference)7 ExceptionWithContext (org.jf.util.ExceptionWithContext)6 FieldReference (org.jf.dexlib2.iface.reference.FieldReference)5 Reference (org.jf.dexlib2.iface.reference.Reference)5 InvalidItemIndex (org.jf.dexlib2.dexbacked.DexBackedDexFile.InvalidItemIndex)4 ImmutableFieldReference (org.jf.dexlib2.immutable.reference.ImmutableFieldReference)4 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)3 Instruction20bc (org.jf.dexlib2.iface.instruction.formats.Instruction20bc)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 Nonnull (javax.annotation.Nonnull)2 Nullable (javax.annotation.Nullable)2 ReferenceType (org.jf.dexlib2.ReferenceType)2 AnalyzedInstruction (org.jf.dexlib2.analysis.AnalyzedInstruction)2 MutableMethodImplementation (org.jf.dexlib2.builder.MutableMethodImplementation)2 BuilderInstruction21c (org.jf.dexlib2.builder.instruction.BuilderInstruction21c)2