All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.linkedin.dex.spec.DexFile.kt Maven / Gradle / Ivy

There is a newer version: 2.3.4
Show newest version
/**
 * Copyright (c) LinkedIn Corporation. All rights reserved. Licensed under the BSD-2 Clause license.
 * See LICENSE in the project root for license information.
 */
package com.linkedin.dex.spec

import com.linkedin.dex.parser.ParseUtils
import java.nio.ByteBuffer
import java.nio.ByteOrder

class DexFile(byteBuffer: ByteBuffer) {
    val byteBuffer: ByteBuffer
    val headerItem: HeaderItem
    val stringIds: Array
    val typeIds: Array
    val protoIds: Array
    val fieldIds: Array
    val methodIds: Array
    val classDefs: Array

    companion object {
        val NO_INDEX = -1
    }

    inline fun  parse(count: Int, offset: Int, size: Int, init: (ByteBuffer) -> T): Array {
        return Array(count, { index ->
            byteBuffer.position(offset + (index * size))
            init(byteBuffer)
        })
    }

    init {
        this.byteBuffer = byteBuffer.asReadOnlyBuffer().order(ByteOrder.LITTLE_ENDIAN)
        this.byteBuffer.position(0)
        headerItem = HeaderItem(this.byteBuffer)
        headerItem.validate()
        stringIds = parse(headerItem.stringIdsSize, headerItem.stringIdsOff, StringIdItem.size, ::StringIdItem)
        typeIds = parse(headerItem.typeIdsSize, headerItem.typeIdsOff, TypeIdItem.size, ::TypeIdItem)
        protoIds = parse(headerItem.protoIdsSize, headerItem.protoIdsOff, ProtoIdItem.size, ::ProtoIdItem)
        fieldIds = parse(headerItem.fieldIdsSize, headerItem.fieldIdsOff, FieldIdItem.size, ::FieldIdItem)
        methodIds = parse(headerItem.methodIdsSize, headerItem.methodIdsOff, MethodIdItem.size, ::MethodIdItem)
        classDefs = parse(headerItem.classDefsSize, headerItem.classDefsOff, ClassDefItem.size, ::ClassDefItem)
    }

    val inheritedAnnotationTypeIdIndex: Int? by lazy {
        var result: Int? = null
        typeIds.forEachIndexed { index, typeIdItem ->
            if (ParseUtils.parseDescriptor(byteBuffer, typeIdItem, stringIds) == "Ljava/lang/annotation/Inherited;") {
                result = index
            }
        }

        result
    }

    val typeIdToClassDefMap: Map by lazy {
        val map = mutableMapOf()

        for (classDef in classDefs) {
            map[classDef.classIdx] = classDef
        }
        map.toMap()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy