Search in sources :

Example 1 with AnnotationsDirectory

use of org.jf.dexlib2.dexbacked.util.AnnotationsDirectory 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 AnnotationsDirectory

use of org.jf.dexlib2.dexbacked.util.AnnotationsDirectory 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 AnnotationsDirectory

use of org.jf.dexlib2.dexbacked.util.AnnotationsDirectory 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 AnnotationsDirectory

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

the class DexBackedClassDef method getSize.

/**
 * Calculate and return the private size of a class definition.
 *
 * Calculated as: class_def_item size + type_id size + interfaces type_list +
 * annotations_directory_item overhead + class_data_item + static values overhead +
 * methods size + fields size
 *
 * @return size in bytes
 */
public int getSize() {
    // class_def_item has 8 uint fields in dex files
    int size = 8 * 4;
    // type_ids size
    size += TypeIdItem.ITEM_SIZE;
    // add interface list size if any
    int interfacesLength = getInterfaces().size();
    if (interfacesLength > 0) {
        // add size of the type_list
        // uint for size
        size += 4;
        // ushort per type_item
        size += interfacesLength * 2;
    }
    // annotations directory size if it exists
    AnnotationsDirectory directory = getAnnotationsDirectory();
    if (!AnnotationsDirectory.EMPTY.equals(directory)) {
        // 4 uints in annotations_directory_item
        size += 4 * 4;
        Set<? extends DexBackedAnnotation> classAnnotations = directory.getClassAnnotations();
        if (!classAnnotations.isEmpty()) {
            // uint for size
            size += 4;
            // uint per annotation_off
            size += classAnnotations.size() * 4;
        // TODO: should we add annotation_item size? what if it's shared?
        }
    }
    // static values and/or metadata
    int staticInitialValuesOffset = dexFile.getBuffer().readSmallUint(classDefOffset + ClassDefItem.STATIC_VALUES_OFFSET);
    if (staticInitialValuesOffset != 0) {
        DexReader reader = dexFile.getDataBuffer().readerAt(staticInitialValuesOffset);
        // encoded_array size field
        size += reader.peekSmallUleb128Size();
    }
    // class_data_item
    int classDataOffset = dexFile.getBuffer().readSmallUint(classDefOffset + ClassDefItem.CLASS_DATA_OFFSET);
    if (classDataOffset > 0) {
        DexReader reader = dexFile.getDataBuffer().readerAt(classDataOffset);
        // staticFieldCount
        reader.readSmallUleb128();
        // instanceFieldCount
        reader.readSmallUleb128();
        // directMethodCount
        reader.readSmallUleb128();
        // virtualMethodCount
        reader.readSmallUleb128();
        size += reader.getOffset() - classDataOffset;
    }
    for (DexBackedField dexBackedField : getFields()) {
        size += dexBackedField.getSize();
    }
    for (DexBackedMethod dexBackedMethod : getMethods()) {
        size += dexBackedMethod.getSize();
    }
    return size;
}
Also used : AnnotationsDirectory(org.jf.dexlib2.dexbacked.util.AnnotationsDirectory)

Example 5 with AnnotationsDirectory

use of org.jf.dexlib2.dexbacked.util.AnnotationsDirectory 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)

Aggregations

AnnotationsDirectory (org.jf.dexlib2.dexbacked.util.AnnotationsDirectory)5 Nonnull (javax.annotation.Nonnull)4 VariableSizeLookaheadIterator (org.jf.dexlib2.dexbacked.util.VariableSizeLookaheadIterator)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 EncodedArrayItemIterator (org.jf.dexlib2.dexbacked.util.EncodedArrayItemIterator)1