From edadc01df2cbac2c8a00c2e0cc520713690418b9 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Tue, 28 Jan 2014 10:27:59 +0300 Subject: SI-6379 adds MethodSymbol.exception MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Following Simon’s request, this commit introduces a dedicated API that can be used to acquire the list of exceptions thrown by a method, abstracting away from whether it’s a Java or a Scala artifact. --- test/files/run/t6379.check | 14 ++++++++++++++ test/files/run/t6379/Macros_1.scala | 26 ++++++++++++++++++++++++++ test/files/run/t6379/Test_2.scala | 22 ++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 test/files/run/t6379.check create mode 100644 test/files/run/t6379/Macros_1.scala create mode 100644 test/files/run/t6379/Test_2.scala (limited to 'test') diff --git a/test/files/run/t6379.check b/test/files/run/t6379.check new file mode 100644 index 0000000000..3e5dfec623 --- /dev/null +++ b/test/files/run/t6379.check @@ -0,0 +1,14 @@ +compile-time +uninitialized close: List(class IOException) +initialized close: List(class IOException) +uninitialized productElement: List(class IndexOutOfBoundsException) +initialized productElement: List(class IndexOutOfBoundsException) +uninitialized read: List(class IOException) +initialized read: List(class IOException) +runtime +uninitialized close: List(class IOException) +initialized close: List(class IOException) +uninitialized productElement: List(class IndexOutOfBoundsException) +initialized productElement: List(class IndexOutOfBoundsException) +uninitialized read: List(class IOException) +initialized read: List(class IOException) diff --git a/test/files/run/t6379/Macros_1.scala b/test/files/run/t6379/Macros_1.scala new file mode 100644 index 0000000000..a866438f7d --- /dev/null +++ b/test/files/run/t6379/Macros_1.scala @@ -0,0 +1,26 @@ +import scala.reflect.macros.whitebox._ +import scala.language.experimental.macros +import java.io._ + +object Macros { + def impl(c: Context) = { + var messages = List[String]() + def println(msg: String) = messages :+= msg + + import c.universe._ + def test(sym: MethodSymbol): Unit = { + println(s"uninitialized ${sym.name}: ${sym.exceptions}") + sym.typeSignature + println(s"initialized ${sym.name}: ${sym.exceptions}") + } + + println("compile-time") + test(typeOf[Closeable].declaration(TermName("close")).asMethod) + test(typeOf[Product1[_]].declaration(TermName("productElement")).asMethod) + test(c.mirror.staticClass("Reader").typeSignature.declaration(TermName("read")).asMethod) + + q"..${messages.map(msg => q"println($msg)")}" + } + + def foo: Any = macro impl +} \ No newline at end of file diff --git a/test/files/run/t6379/Test_2.scala b/test/files/run/t6379/Test_2.scala new file mode 100644 index 0000000000..af4ec7c6d0 --- /dev/null +++ b/test/files/run/t6379/Test_2.scala @@ -0,0 +1,22 @@ +import java.io._ +import scala.reflect.runtime.universe._ + +class Reader(fname: String) { + private val in = new BufferedReader(new FileReader(fname)) + @throws[IOException]("if the file doesn't exist") + def read() = in.read() +} + +object Test extends App { + def test(sym: MethodSymbol): Unit = { + println(s"uninitialized ${sym.name}: ${sym.exceptions}") + sym.typeSignature + println(s"initialized ${sym.name}: ${sym.exceptions}") + } + + Macros.foo + println("runtime") + test(typeOf[Closeable].declaration(TermName("close")).asMethod) + test(typeOf[Product1[_]].declaration(TermName("productElement")).asMethod) + test(typeOf[Reader].declaration(TermName("read")).asMethod) +} -- cgit v1.2.3