summaryrefslogtreecommitdiffstats
path: root/bundle-plugin/src/main/scala/com/yahoo/container/plugin/classanalysis/AnalyzeMethodVisitor.scala
blob: 5d65b3972c05155518c73272f4aea0a3e2f758c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.plugin.classanalysis

import org.objectweb.asm._

/**
 * Picks up classes used in method bodies.
 * @author  tonytv
 */
private class AnalyzeMethodVisitor(val analyzeClassVisitor : AnalyzeClassVisitor)
  extends MethodVisitor(Opcodes.ASM5) with AnnotationVisitorTrait with AttributeVisitorTrait with SubVisitorTrait {


  override def visitParameterAnnotation(parameter: Int, desc: String, visible: Boolean): AnnotationVisitor = super.visitParameterAnnotation(parameter, desc, visible)
  override def visitAnnotationDefault(): AnnotationVisitor = super.visitAnnotationDefault()
  override def visitAttribute(attribute: Attribute): Unit = super.visitAttribute(attribute)
  override def visitAnnotation(desc: String, visible: Boolean): AnnotationVisitor = super.visitAnnotation(desc, visible)
  override def visitEnd(): Unit = super.visitEnd()

  override def visitMultiANewArrayInsn(desc: String, dims: Int) {
    imports ++= getClassName(Type.getType(desc)).toList
  }


  override def visitMethodInsn(opcode: Int, owner: String, name: String, desc: String, itf: Boolean) {
    imports ++= internalNameToClassName(owner)
    imports ++= Type.getArgumentTypes(desc).flatMap(getClassName)
    imports ++= getClassName(Type.getReturnType(desc))
  }

  override def visitFieldInsn(opcode: Int, owner: String, name: String, desc: String) {
    imports ++= internalNameToClassName(owner) ++ getClassName(Type.getType(desc)).toList

  }

  override def visitTypeInsn(opcode: Int, `type` : String) {
    imports ++= internalNameToClassName(`type`)
  }

  override def visitTryCatchBlock(start: Label, end: Label, handler: Label, `type` : String) {
    if (`type` != null) //null means finally block
      imports ++= internalNameToClassName(`type`)
  }

  override def visitLocalVariable(name: String, desc: String, signature: String, start: Label, end: Label, index: Int) {
    imports += Type.getType(desc).getClassName
  }

  override def visitLdcInsn(constant: AnyRef) {
    constant match {
      case typeConstant: Type =>  imports ++= getClassName(typeConstant)
      case _ =>
    }
  }

  override def visitInvokeDynamicInsn(name: String, desc: String, bootstrapMethod: Handle, bootstrapMethodArgs: AnyRef*) {
    bootstrapMethodArgs.foreach {
      case typeConstant: Type =>
        imports ++= getClassName(typeConstant)
      case handle: Handle =>
        imports ++= internalNameToClassName(handle.getOwner)
        imports ++= Type.getArgumentTypes(desc).flatMap(getClassName)
      case _ : Number =>
      case _ : String =>
      case other => throw new AssertionError(s"Unexpected type ${other.getClass} with value '$other'")
    }
  }

  override def visitMaxs(maxStack: Int, maxLocals: Int) {}
  override def visitLineNumber(line: Int, start: Label) {}
  //only for debugging
  override def visitLookupSwitchInsn(dflt: Label, keys: Array[Int], labels: Array[Label]) {}


  override def visitTableSwitchInsn(min: Int, max: Int, dflt: Label, labels: Label*): Unit = super.visitTableSwitchInsn(min, max, dflt, labels: _*)
  override def visitIincInsn(`var` : Int, increment: Int) {}
  override def visitLabel(label: Label) {}
  override def visitJumpInsn(opcode: Int, label: Label) {}
  override def visitVarInsn(opcode: Int, `var` : Int) {}
  override def visitIntInsn(opcode: Int, operand: Int) {}
  override def visitInsn(opcode: Int) {}
  override def visitFrame(`type` : Int, nLocal: Int, local: Array[AnyRef], nStack: Int, stack: Array[AnyRef]) {}
  override def visitCode() {}
}