Search in sources :

Example 1 with VariableSizeLookaheadIterator

use of org.jf.dexlib2.dexbacked.util.VariableSizeLookaheadIterator 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.getDataBuffer().readerAt(getDirectMethodsOffset());
        final AnnotationsDirectory annotationsDirectory = getAnnotationsDirectory();
        final int methodsStartOffset = reader.getOffset();
        Iterator<Integer> hiddenApiRestrictionIterator = hiddenApiRestrictionsReader == null ? null : hiddenApiRestrictionsReader.getRestrictionsForDirectMethods();
        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.getDataBuffer(), 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();
                            }
                            int hiddenApiRestrictions = NO_HIDDEN_API_RESTRICTIONS;
                            if (hiddenApiRestrictionIterator != null) {
                                hiddenApiRestrictions = hiddenApiRestrictionIterator.next();
                            }
                            DexBackedMethod item = new DexBackedMethod(dexFile, reader, DexBackedClassDef.this, previousIndex, methodAnnotationIterator, parameterAnnotationIterator, hiddenApiRestrictions);
                            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 2 with VariableSizeLookaheadIterator

use of org.jf.dexlib2.dexbacked.util.VariableSizeLookaheadIterator in project smali by JesusFreke.

the class DexBackedClassDef method getStaticFields.

@Nonnull
public Iterable<? extends DexBackedField> getStaticFields(final boolean skipDuplicates) {
    if (staticFieldCount > 0) {
        DexReader<? extends DexBuffer> reader = dexFile.getDataBuffer().readerAt(staticFieldsOffset);
        final AnnotationsDirectory annotationsDirectory = getAnnotationsDirectory();
        final int staticInitialValuesOffset = dexFile.getBuffer().readSmallUint(classDefOffset + ClassDefItem.STATIC_VALUES_OFFSET);
        final int fieldsStartOffset = reader.getOffset();
        Iterator<Integer> hiddenApiRestrictionIterator = hiddenApiRestrictionsReader == null ? null : hiddenApiRestrictionsReader.getRestrictionsForStaticFields();
        return new Iterable<DexBackedField>() {

            @Nonnull
            @Override
            public Iterator<DexBackedField> iterator() {
                final AnnotationsDirectory.AnnotationIterator annotationIterator = annotationsDirectory.getFieldAnnotationIterator();
                final EncodedArrayItemIterator staticInitialValueIterator = EncodedArrayItemIterator.newOrEmpty(dexFile, staticInitialValuesOffset);
                return new VariableSizeLookaheadIterator<DexBackedField>(dexFile.getDataBuffer(), fieldsStartOffset) {

                    private int count;

                    @Nullable
                    private FieldReference previousField;

                    private int previousIndex;

                    @Nullable
                    @Override
                    protected DexBackedField readNextItem(@Nonnull DexReader reader) {
                        while (true) {
                            if (++count > staticFieldCount) {
                                instanceFieldsOffset = reader.getOffset();
                                return endOfData();
                            }
                            int hiddenApiRestrictions = NO_HIDDEN_API_RESTRICTIONS;
                            if (hiddenApiRestrictionIterator != null) {
                                hiddenApiRestrictions = hiddenApiRestrictionIterator.next();
                            }
                            DexBackedField item = new DexBackedField(dexFile, reader, DexBackedClassDef.this, previousIndex, staticInitialValueIterator, annotationIterator, hiddenApiRestrictions);
                            FieldReference currentField = previousField;
                            FieldReference nextField = ImmutableFieldReference.of(item);
                            previousField = nextField;
                            previousIndex = item.fieldIndex;
                            if (skipDuplicates && currentField != null && currentField.equals(nextField)) {
                                continue;
                            }
                            return item;
                        }
                    }
                };
            }
        };
    } else {
        instanceFieldsOffset = staticFieldsOffset;
        return ImmutableSet.of();
    }
}
Also used : ImmutableFieldReference(org.jf.dexlib2.immutable.reference.ImmutableFieldReference) FieldReference(org.jf.dexlib2.iface.reference.FieldReference) Nonnull(javax.annotation.Nonnull) AnnotationsDirectory(org.jf.dexlib2.dexbacked.util.AnnotationsDirectory) VariableSizeLookaheadIterator(org.jf.dexlib2.dexbacked.util.VariableSizeLookaheadIterator) EncodedArrayItemIterator(org.jf.dexlib2.dexbacked.util.EncodedArrayItemIterator) Nonnull(javax.annotation.Nonnull)

Example 3 with VariableSizeLookaheadIterator

use of org.jf.dexlib2.dexbacked.util.VariableSizeLookaheadIterator 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.getDataBuffer().readerAt(getVirtualMethodsOffset());
        final AnnotationsDirectory annotationsDirectory = getAnnotationsDirectory();
        final int methodsStartOffset = reader.getOffset();
        Iterator<Integer> hiddenApiRestrictionIterator = hiddenApiRestrictionsReader == null ? null : hiddenApiRestrictionsReader.getRestrictionsForVirtualMethods();
        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.getDataBuffer(), 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();
                            }
                            int hiddenApiRestrictions = NO_HIDDEN_API_RESTRICTIONS;
                            if (hiddenApiRestrictionIterator != null) {
                                hiddenApiRestrictions = hiddenApiRestrictionIterator.next();
                            }
                            DexBackedMethod item = new DexBackedMethod(dexFile, reader, DexBackedClassDef.this, previousIndex, methodAnnotationIterator, parameterAnnotationIterator, hiddenApiRestrictions);
                            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 4 with VariableSizeLookaheadIterator

use of org.jf.dexlib2.dexbacked.util.VariableSizeLookaheadIterator in project smali by JesusFreke.

the class DexBackedClassDef method getInstanceFields.

@Nonnull
public Iterable<? extends DexBackedField> getInstanceFields(final boolean skipDuplicates) {
    if (instanceFieldCount > 0) {
        DexReader reader = dexFile.getDataBuffer().readerAt(getInstanceFieldsOffset());
        final AnnotationsDirectory annotationsDirectory = getAnnotationsDirectory();
        final int fieldsStartOffset = reader.getOffset();
        Iterator<Integer> hiddenApiRestrictionIterator = hiddenApiRestrictionsReader == null ? null : hiddenApiRestrictionsReader.getRestrictionsForInstanceFields();
        return new Iterable<DexBackedField>() {

            @Nonnull
            @Override
            public Iterator<DexBackedField> iterator() {
                final AnnotationsDirectory.AnnotationIterator annotationIterator = annotationsDirectory.getFieldAnnotationIterator();
                return new VariableSizeLookaheadIterator<DexBackedField>(dexFile.getDataBuffer(), fieldsStartOffset) {

                    private int count;

                    @Nullable
                    private FieldReference previousField;

                    private int previousIndex;

                    @Nullable
                    @Override
                    protected DexBackedField readNextItem(@Nonnull DexReader reader) {
                        while (true) {
                            if (++count > instanceFieldCount) {
                                directMethodsOffset = reader.getOffset();
                                return endOfData();
                            }
                            int hiddenApiRestrictions = NO_HIDDEN_API_RESTRICTIONS;
                            if (hiddenApiRestrictionIterator != null) {
                                hiddenApiRestrictions = hiddenApiRestrictionIterator.next();
                            }
                            DexBackedField item = new DexBackedField(dexFile, reader, DexBackedClassDef.this, previousIndex, annotationIterator, hiddenApiRestrictions);
                            FieldReference currentField = previousField;
                            FieldReference nextField = ImmutableFieldReference.of(item);
                            previousField = nextField;
                            previousIndex = item.fieldIndex;
                            if (skipDuplicates && currentField != null && currentField.equals(nextField)) {
                                continue;
                            }
                            return item;
                        }
                    }
                };
            }
        };
    } else {
        if (instanceFieldsOffset > 0) {
            directMethodsOffset = instanceFieldsOffset;
        }
        return ImmutableSet.of();
    }
}
Also used : AnnotationsDirectory(org.jf.dexlib2.dexbacked.util.AnnotationsDirectory) ImmutableFieldReference(org.jf.dexlib2.immutable.reference.ImmutableFieldReference) FieldReference(org.jf.dexlib2.iface.reference.FieldReference) Nonnull(javax.annotation.Nonnull) VariableSizeLookaheadIterator(org.jf.dexlib2.dexbacked.util.VariableSizeLookaheadIterator) Nonnull(javax.annotation.Nonnull)

Example 5 with VariableSizeLookaheadIterator

use of org.jf.dexlib2.dexbacked.util.VariableSizeLookaheadIterator in project smali by JesusFreke.

the class DexBackedMethodImplementation method getInstructions.

@Nonnull
@Override
public Iterable<? extends Instruction> getInstructions() {
    // instructionsSize is the number of 16-bit code units in the instruction list, not the number of instructions
    int instructionsSize = getInstructionsSize();
    final int instructionsStartOffset = getInstructionsStartOffset();
    final int endOffset = instructionsStartOffset + (instructionsSize * 2);
    return new Iterable<Instruction>() {

        @Override
        public Iterator<Instruction> iterator() {
            return new VariableSizeLookaheadIterator<Instruction>(dexFile.getDataBuffer(), instructionsStartOffset) {

                @Override
                protected Instruction readNextItem(@Nonnull DexReader reader) {
                    if (reader.getOffset() >= endOffset) {
                        return endOfData();
                    }
                    Instruction instruction = DexBackedInstruction.readFrom(dexFile, reader);
                    // Does the instruction extend past the end of the method?
                    int offset = reader.getOffset();
                    if (offset > endOffset || offset < 0) {
                        throw new ExceptionWithContext("The last instruction in method %s is truncated", method);
                    }
                    return instruction;
                }
            };
        }
    };
}
Also used : ExceptionWithContext(org.jf.util.ExceptionWithContext) Nonnull(javax.annotation.Nonnull) VariableSizeLookaheadIterator(org.jf.dexlib2.dexbacked.util.VariableSizeLookaheadIterator) Instruction(org.jf.dexlib2.iface.instruction.Instruction) DexBackedInstruction(org.jf.dexlib2.dexbacked.instruction.DexBackedInstruction) Nonnull(javax.annotation.Nonnull)

Aggregations

Nonnull (javax.annotation.Nonnull)5 VariableSizeLookaheadIterator (org.jf.dexlib2.dexbacked.util.VariableSizeLookaheadIterator)5 AnnotationsDirectory (org.jf.dexlib2.dexbacked.util.AnnotationsDirectory)4 FieldReference (org.jf.dexlib2.iface.reference.FieldReference)2 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)2 ImmutableFieldReference (org.jf.dexlib2.immutable.reference.ImmutableFieldReference)2 ImmutableMethodReference (org.jf.dexlib2.immutable.reference.ImmutableMethodReference)2 DexBackedInstruction (org.jf.dexlib2.dexbacked.instruction.DexBackedInstruction)1 EncodedArrayItemIterator (org.jf.dexlib2.dexbacked.util.EncodedArrayItemIterator)1 Instruction (org.jf.dexlib2.iface.instruction.Instruction)1 ExceptionWithContext (org.jf.util.ExceptionWithContext)1