Search in sources :

Example 6 with MethodReference

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

the class CustomMethodInlineTableTest method testCustomMethodInlineTable_Direct.

@Test
public void testCustomMethodInlineTable_Direct() 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.PRIVATE.getValue(), null, methodImpl);
    ClassDef classDef = new ImmutableClassDef("Lblah;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, null, null, null, ImmutableList.of(method), null);
    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_DIRECT, deodexedInstruction.getOpcode());
    MethodReference methodReference = (MethodReference) ((Instruction35c) deodexedInstruction).getReference();
    Assert.assertEquals(method, methodReference);
}
Also used : ImmutableMethod(org.jf.dexlib2.immutable.ImmutableMethod) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) Instruction(org.jf.dexlib2.iface.instruction.Instruction) ImmutableInstruction(org.jf.dexlib2.immutable.instruction.ImmutableInstruction) ImmutableInstruction10x(org.jf.dexlib2.immutable.instruction.ImmutableInstruction10x) DexFile(org.jf.dexlib2.iface.DexFile) ImmutableDexFile(org.jf.dexlib2.immutable.ImmutableDexFile) ImmutableInstruction35mi(org.jf.dexlib2.immutable.instruction.ImmutableInstruction35mi) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) ClassDef(org.jf.dexlib2.iface.ClassDef) ImmutableInstruction(org.jf.dexlib2.immutable.instruction.ImmutableInstruction) ImmutableMethodImplementation(org.jf.dexlib2.immutable.ImmutableMethodImplementation) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) ImmutableDexFile(org.jf.dexlib2.immutable.ImmutableDexFile) Test(org.junit.Test)

Example 7 with MethodReference

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

the class DexBackedClassDef method getDirectMethods.

@Nonnull
public Iterable<? extends DexBackedMethod> getDirectMethods(final boolean skipDuplicates) {
    if (directMethodCount > 0) {
        DexReader reader = dexFile.readerAt(getDirectMethodsOffset());
        final AnnotationsDirectory annotationsDirectory = getAnnotationsDirectory();
        final int methodsStartOffset = reader.getOffset();
        return new Iterable<DexBackedMethod>() {

            @Nonnull
            @Override
            public Iterator<DexBackedMethod> iterator() {
                final AnnotationsDirectory.AnnotationIterator methodAnnotationIterator = annotationsDirectory.getMethodAnnotationIterator();
                final AnnotationsDirectory.AnnotationIterator parameterAnnotationIterator = annotationsDirectory.getParameterAnnotationIterator();
                return new VariableSizeLookaheadIterator<DexBackedMethod>(dexFile, methodsStartOffset) {

                    private int count;

                    @Nullable
                    private MethodReference previousMethod;

                    private int previousIndex;

                    @Nullable
                    @Override
                    protected DexBackedMethod readNextItem(@Nonnull DexReader reader) {
                        while (true) {
                            if (++count > directMethodCount) {
                                virtualMethodsOffset = reader.getOffset();
                                return endOfData();
                            }
                            DexBackedMethod item = new DexBackedMethod(reader, DexBackedClassDef.this, previousIndex, methodAnnotationIterator, parameterAnnotationIterator);
                            MethodReference currentMethod = previousMethod;
                            MethodReference nextMethod = ImmutableMethodReference.of(item);
                            previousMethod = nextMethod;
                            previousIndex = item.methodIndex;
                            if (skipDuplicates && currentMethod != null && currentMethod.equals(nextMethod)) {
                                continue;
                            }
                            return item;
                        }
                    }
                };
            }
        };
    } else {
        if (directMethodsOffset > 0) {
            virtualMethodsOffset = directMethodsOffset;
        }
        return ImmutableSet.of();
    }
}
Also used : AnnotationsDirectory(org.jf.dexlib2.dexbacked.util.AnnotationsDirectory) Nonnull(javax.annotation.Nonnull) VariableSizeLookaheadIterator(org.jf.dexlib2.dexbacked.util.VariableSizeLookaheadIterator) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) Nonnull(javax.annotation.Nonnull)

Example 8 with MethodReference

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

the class DexBackedClassDef method getVirtualMethods.

@Nonnull
public Iterable<? extends DexBackedMethod> getVirtualMethods(final boolean skipDuplicates) {
    if (virtualMethodCount > 0) {
        DexReader reader = dexFile.readerAt(getVirtualMethodsOffset());
        final AnnotationsDirectory annotationsDirectory = getAnnotationsDirectory();
        final int methodsStartOffset = reader.getOffset();
        return new Iterable<DexBackedMethod>() {

            final AnnotationsDirectory.AnnotationIterator methodAnnotationIterator = annotationsDirectory.getMethodAnnotationIterator();

            final AnnotationsDirectory.AnnotationIterator parameterAnnotationIterator = annotationsDirectory.getParameterAnnotationIterator();

            @Nonnull
            @Override
            public Iterator<DexBackedMethod> iterator() {
                return new VariableSizeLookaheadIterator<DexBackedMethod>(dexFile, methodsStartOffset) {

                    private int count;

                    @Nullable
                    private MethodReference previousMethod;

                    private int previousIndex;

                    @Nullable
                    @Override
                    protected DexBackedMethod readNextItem(@Nonnull DexReader reader) {
                        while (true) {
                            if (++count > virtualMethodCount) {
                                return endOfData();
                            }
                            DexBackedMethod item = new DexBackedMethod(reader, DexBackedClassDef.this, previousIndex, methodAnnotationIterator, parameterAnnotationIterator);
                            MethodReference currentMethod = previousMethod;
                            MethodReference nextMethod = ImmutableMethodReference.of(item);
                            previousMethod = nextMethod;
                            previousIndex = item.methodIndex;
                            if (skipDuplicates && currentMethod != null && currentMethod.equals(nextMethod)) {
                                continue;
                            }
                            return item;
                        }
                    }
                };
            }
        };
    } else {
        return ImmutableSet.of();
    }
}
Also used : AnnotationsDirectory(org.jf.dexlib2.dexbacked.util.AnnotationsDirectory) Nonnull(javax.annotation.Nonnull) VariableSizeLookaheadIterator(org.jf.dexlib2.dexbacked.util.VariableSizeLookaheadIterator) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) Nonnull(javax.annotation.Nonnull)

Example 9 with MethodReference

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

the class MethodDefinition method addInstructionMethodItems.

private void addInstructionMethodItems(List<MethodItem> methodItems) {
    int currentCodeAddress = 0;
    for (int i = 0; i < effectiveInstructions.size(); i++) {
        Instruction instruction = effectiveInstructions.get(i);
        MethodItem methodItem = InstructionMethodItemFactory.makeInstructionFormatMethodItem(this, currentCodeAddress, instruction);
        methodItems.add(methodItem);
        if (i != effectiveInstructions.size() - 1) {
            methodItems.add(new BlankMethodItem(currentCodeAddress));
        }
        if (classDef.options.codeOffsets) {
            methodItems.add(new MethodItem(currentCodeAddress) {

                @Override
                public double getSortOrder() {
                    return -1000;
                }

                @Override
                public boolean writeTo(IndentingWriter writer) throws IOException {
                    writer.write("#@");
                    writer.printUnsignedLongAsHex(codeAddress & 0xFFFFFFFFL);
                    return true;
                }
            });
        }
        if (classDef.options.accessorComments && classDef.options.syntheticAccessorResolver != null && (instruction instanceof ReferenceInstruction)) {
            Opcode opcode = instruction.getOpcode();
            if (opcode.referenceType == ReferenceType.METHOD) {
                MethodReference methodReference = null;
                try {
                    methodReference = (MethodReference) ((ReferenceInstruction) instruction).getReference();
                } catch (InvalidItemIndex ex) {
                // just ignore it for now. We'll deal with it later, when processing the instructions
                // themselves
                }
                if (methodReference != null && SyntheticAccessorResolver.looksLikeSyntheticAccessor(methodReference.getName())) {
                    AccessedMember accessedMember = classDef.options.syntheticAccessorResolver.getAccessedMember(methodReference);
                    if (accessedMember != null) {
                        methodItems.add(new SyntheticAccessCommentMethodItem(accessedMember, currentCodeAddress));
                    }
                }
            }
        }
        currentCodeAddress += instruction.getCodeUnits();
    }
}
Also used : DebugMethodItem(org.jf.baksmali.Adaptors.Debug.DebugMethodItem) InvalidItemIndex(org.jf.dexlib2.dexbacked.DexBackedDexFile.InvalidItemIndex) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) Opcode(org.jf.dexlib2.Opcode) IOException(java.io.IOException) OffsetInstruction(org.jf.dexlib2.iface.instruction.OffsetInstruction) AnalyzedInstruction(org.jf.dexlib2.analysis.AnalyzedInstruction) Instruction(org.jf.dexlib2.iface.instruction.Instruction) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) AccessedMember(org.jf.dexlib2.util.SyntheticAccessorResolver.AccessedMember) IndentingWriter(org.jf.util.IndentingWriter) MethodReference(org.jf.dexlib2.iface.reference.MethodReference)

Example 10 with MethodReference

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

the class AnalyzedInstruction method isInvokeInit.

public boolean isInvokeInit() {
    if (!instruction.getOpcode().canInitializeReference()) {
        return false;
    }
    ReferenceInstruction instruction = (ReferenceInstruction) this.instruction;
    Reference reference = instruction.getReference();
    if (reference instanceof MethodReference) {
        return ((MethodReference) reference).getName().equals("<init>");
    }
    return false;
}
Also used : MethodReference(org.jf.dexlib2.iface.reference.MethodReference) TypeReference(org.jf.dexlib2.iface.reference.TypeReference) Reference(org.jf.dexlib2.iface.reference.Reference) MethodReference(org.jf.dexlib2.iface.reference.MethodReference)

Aggregations

MethodReference (org.jf.dexlib2.iface.reference.MethodReference)21 Instruction (org.jf.dexlib2.iface.instruction.Instruction)12 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)9 ImmutableMethodReference (org.jf.dexlib2.immutable.reference.ImmutableMethodReference)8 Opcode (org.jf.dexlib2.Opcode)6 ClassDef (org.jf.dexlib2.iface.ClassDef)6 TypeReference (org.jf.dexlib2.iface.reference.TypeReference)5 Test (org.junit.Test)5 DexFile (org.jf.dexlib2.iface.DexFile)4 ExceptionWithContext (org.jf.util.ExceptionWithContext)4 ArrayList (java.util.ArrayList)3 BaseMethodReference (org.jf.dexlib2.base.reference.BaseMethodReference)3 InvalidItemIndex (org.jf.dexlib2.dexbacked.DexBackedDexFile.InvalidItemIndex)3 Method (org.jf.dexlib2.iface.Method)3 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)3 FieldReference (org.jf.dexlib2.iface.reference.FieldReference)3 ImmutableInstruction (org.jf.dexlib2.immutable.instruction.ImmutableInstruction)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 Nonnull (javax.annotation.Nonnull)2