Search in sources :

Example 1 with StaticInitialValueIterator

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

the class DexBackedClassDef method getStaticFields.

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

            @Nonnull
            @Override
            public Iterator<DexBackedField> iterator() {
                final AnnotationsDirectory.AnnotationIterator annotationIterator = annotationsDirectory.getFieldAnnotationIterator();
                final StaticInitialValueIterator staticInitialValueIterator = StaticInitialValueIterator.newOrEmpty(dexFile, staticInitialValuesOffset);
                return new VariableSizeLookaheadIterator<DexBackedField>(dexFile, 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();
                            }
                            DexBackedField item = new DexBackedField(reader, DexBackedClassDef.this, previousIndex, staticInitialValueIterator, annotationIterator);
                            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 : 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) StaticInitialValueIterator(org.jf.dexlib2.dexbacked.util.StaticInitialValueIterator) Nonnull(javax.annotation.Nonnull)

Aggregations

Nonnull (javax.annotation.Nonnull)1 AnnotationsDirectory (org.jf.dexlib2.dexbacked.util.AnnotationsDirectory)1 StaticInitialValueIterator (org.jf.dexlib2.dexbacked.util.StaticInitialValueIterator)1 VariableSizeLookaheadIterator (org.jf.dexlib2.dexbacked.util.VariableSizeLookaheadIterator)1 FieldReference (org.jf.dexlib2.iface.reference.FieldReference)1 ImmutableFieldReference (org.jf.dexlib2.immutable.reference.ImmutableFieldReference)1