summaryrefslogtreecommitdiff
path: root/test/files/run/classfile-format-52.scala
diff options
context:
space:
mode:
authorJason Zaugg <jzaugg@gmail.com>2013-06-10 06:20:37 -0400
committerJason Zaugg <jzaugg@gmail.com>2013-06-10 06:20:37 -0400
commit549cb905aa6a4facb3cb12628a76ab8273658170 (patch)
tree0213972eb9f2fb3f82472063873bf90e18f0446c /test/files/run/classfile-format-52.scala
parent16c31f4923b3714d2780f0a494b3da07c1ac12bc (diff)
parente3c318649c4318412c8d7e3cbee4689489f6ccb1 (diff)
downloadscala-549cb905aa6a4facb3cb12628a76ab8273658170.tar.gz
scala-549cb905aa6a4facb3cb12628a76ab8273658170.tar.bz2
scala-549cb905aa6a4facb3cb12628a76ab8273658170.zip
Merge branch '2.10.x' into topic/merge-v2.10.2-RC1-35-ga54d86b-to-master
Conflicts: src/partest/scala/tools/partest/DirectTest.scala
Diffstat (limited to 'test/files/run/classfile-format-52.scala')
-rw-r--r--test/files/run/classfile-format-52.scala77
1 files changed, 77 insertions, 0 deletions
diff --git a/test/files/run/classfile-format-52.scala b/test/files/run/classfile-format-52.scala
new file mode 100644
index 0000000000..7afa09ae0b
--- /dev/null
+++ b/test/files/run/classfile-format-52.scala
@@ -0,0 +1,77 @@
+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 8 (classfile format 52) files, including those
+// with default methods. To do that it first uses ASM to generate an interface called
+// HasDefaultMethod. Then it runs a normal compile on Scala source that extends that
+// interface. Any failure will be dumped to std out.
+//
+// By it's nature the test can only work on JDK 8+ because under JDK 7- the
+// interface won't verify.
+object Test extends DirectTest {
+ override def extraSettings: String = "-optimise -usejavacp -d " + testOutput.path + " -cp " + testOutput.path
+
+ def generateInterface() {
+ val interfaceName = "HasDefaultMethod"
+ val methodType = "()Ljava/lang/String;"
+
+ val cw = new ClassWriter(0)
+ cw.visit(52, ACC_PUBLIC+ACC_ABSTRACT+ACC_INTERFACE, interfaceName, null, "java/lang/Object", null)
+
+ def createMethod(flags:Int, name: String) {
+ val method = cw.visitMethod(flags, name, methodType, null, null)
+ method.visitCode()
+ method.visitLdcInsn(s"hello from $name")
+ method.visitInsn(ARETURN)
+ method.visitMaxs(1, 1)
+ method.visitEnd()
+ }
+
+ createMethod(ACC_PUBLIC, "publicMethod")
+ createMethod(ACC_PUBLIC+ACC_STATIC, "staticMethod")
+ createMethod(ACC_PRIVATE, "privateMethod")
+
+ cw.visitEnd()
+ val bytes = cw.toByteArray()
+
+ val fos = new FileOutputStream(new File(s"${testOutput.path}/$interfaceName.class"))
+ try
+ fos write bytes
+ finally
+ fos.close()
+
+ }
+
+ def code =
+"""
+class Driver extends HasDefaultMethod {
+ println(publicMethod())
+ println(HasDefaultMethod.staticMethod())
+}
+"""
+
+ 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.8+
+ testUnderJavaAtLeast("1.8") {
+ generateInterface()
+ compile()
+ Class.forName("Driver").newInstance()
+ ()
+ } otherwise {
+ println("hello from publicMethod")
+ println("hello from staticMethod")
+ }
+ }
+ finally
+ System.setErr(prevErr)
+ }
+}