summaryrefslogtreecommitdiff
path: root/test/files/run/classfile-format-51.scala
blob: 81df2f08d9b182f7f516e9902f9463b00130c316 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.io.{File, FileOutputStream}

import scala.tools.nsc.settings.ScalaVersion
import scala.tools.partest._
import scala.tools.asm
import asm.{AnnotationVisitor, ClassWriter, FieldVisitor, Handle, MethodVisitor, Opcodes}
import Opcodes._

// This test ensures that we can read JDK 7 (classfile format 51) files, including those
// with invokeDynamic instructions and associated constant pool entries
// to do that it first uses ASM to generate a class called DynamicInvoker. Then
// it runs a normal compile on the source in the 'code' field that refers to
// DynamicInvoker. Any failure will be dumped to std out.
//
// By its nature the test can only work on JDK 7+ because under JDK 6 some of the
// classes referred to by DynamicInvoker won't be available and DynamicInvoker won't
// verify. So the test includes a version check that short-circuits the whole test
// on JDK 6
object Test extends DirectTest {
  override def extraSettings: String = "-optimise -usejavacp -d " + testOutput.path + " -cp " + testOutput.path

  def generateClass() {
    val invokerClassName =  "DynamicInvoker"
    val bootstrapMethodName = "bootstrap"
    val bootStrapMethodType = "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"
    val targetMethodName = "target"
    val targetMethodType = "()Ljava/lang/String;"

    val cw = new ClassWriter(0)
    cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, invokerClassName, null, "java/lang/Object", null)

    val constructor = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null)
    constructor.visitCode()
    constructor.visitVarInsn(ALOAD, 0)
    constructor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false)
    constructor.visitInsn(RETURN)
    constructor.visitMaxs(1, 1)
    constructor.visitEnd()

    val target = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, targetMethodName, targetMethodType, null, null)
    target.visitCode()
    target.visitLdcInsn("hello")
    target.visitInsn(ARETURN)
    target.visitMaxs(1, 1)
    target.visitEnd()

    val bootstrap = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, bootstrapMethodName, bootStrapMethodType, null, null)
    bootstrap.visitCode()
//  val lookup = MethodHandles.lookup();
    bootstrap.visitMethodInsn(INVOKESTATIC, "java/lang/invoke/MethodHandles", "lookup", "()Ljava/lang/invoke/MethodHandles$Lookup;", false)
    bootstrap.visitVarInsn(ASTORE, 3) // lookup

//  val clazz = lookup.lookupClass();
    bootstrap.visitVarInsn(ALOAD, 3) // lookup
    bootstrap.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandles$Lookup", "lookupClass", "()Ljava/lang/Class;", false)
    bootstrap.visitVarInsn(ASTORE, 4) // clazz

// val methodType = MethodType.fromMethodDescriptorString("()Ljava/lang/String, clazz.getClassLoader()")
    bootstrap.visitLdcInsn("()Ljava/lang/String;")
    bootstrap.visitVarInsn(ALOAD, 4) // CLAZZ
    bootstrap.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getClassLoader", "()Ljava/lang/ClassLoader;", false)
    bootstrap.visitMethodInsn(INVOKESTATIC, "java/lang/invoke/MethodType", "fromMethodDescriptorString", "(Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/lang/invoke/MethodType;", false)
    bootstrap.visitVarInsn(ASTORE, 5) // methodType

//  val methodHandle = lookup.findStatic(thisClass, "target", methodType)
    bootstrap.visitVarInsn(ALOAD, 3) // lookup
    bootstrap.visitVarInsn(ALOAD, 4) // clazz
    bootstrap.visitLdcInsn("target")
    bootstrap.visitVarInsn(ALOAD, 5) // methodType
    bootstrap.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandles$Lookup", "findStatic", "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;", false)
    bootstrap.visitVarInsn(ASTORE, 6) // methodHandle

//  new ConstantCallSite(methodHandle)
    bootstrap.visitTypeInsn(NEW, "java/lang/invoke/ConstantCallSite")
    bootstrap.visitInsn(DUP)
    bootstrap.visitVarInsn(ALOAD, 6) // methodHandle
    bootstrap.visitMethodInsn(INVOKESPECIAL, "java/lang/invoke/ConstantCallSite", "<init>", "(Ljava/lang/invoke/MethodHandle;)V", false)
    bootstrap.visitInsn(ARETURN)
    bootstrap.visitMaxs(4,7)
    bootstrap.visitEnd()

    val test = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "test", s"()Ljava/lang/String;", null, null)
    test.visitCode()
    val bootstrapHandle = new Handle(H_INVOKESTATIC, invokerClassName, bootstrapMethodName, bootStrapMethodType)
    test.visitInvokeDynamicInsn("invoke", targetMethodType, bootstrapHandle)
    test.visitInsn(ARETURN)
    test.visitMaxs(1, 1)
    test.visitEnd()

    cw.visitEnd()
    val bytes = cw.toByteArray()

    val fos = new FileOutputStream(new File(s"${testOutput.path}/$invokerClassName.class"))
    try
      fos write bytes
    finally
      fos.close()

  }

  def code =
"""
object Driver {
  val invoker = new DynamicInvoker()
  println(invoker.test())
}
"""

  override def show(): Unit = {
    // redirect err to out, for logging
    val prevErr = System.err
    System.setErr(System.out)
    try {
      // this test is only valid under JDK 1.7+
      testUnderJavaAtLeast("1.7") {
        generateClass()
        compile()
        ()
      } otherwise {
        ()
      }
    }
    finally
      System.setErr(prevErr)
  }
}