package analysis

import dex.DexClass
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.dexbacked.reference.DexBackedTypeReference
import org.jf.dexlib2.iface.instruction.{Instruction, ReferenceInstruction}

class PreAnalyzer(classes: Seq[DexClass]) {

  /**
    * Pre analysis.
    */
  def analyze: PreAnalysisResult = {
    /*
      Find all the NEW_INSTANCE instructions and create a map
      where the key is the program point and the key the class name.
     */
    val objectCreation: Map[PP, String] = classes
      .flatMap(c => {
        c.methods.flatMap(m => {
          m.instructions
            .filter(i => i.getOpcode == Opcode.NEW_INSTANCE)
            .zipWithIndex
            .map {
              case (i: Instruction, pc: Int) =>
                val objType = i
                  .asInstanceOf[ReferenceInstruction]
                  .getReference
                  .asInstanceOf[DexBackedTypeReference]
                  .getType
                (PP(c, m, pc), objType)
            }
        })
      })
      .toMap
    PreAnalysisResult(objectCreation)
  }

}
