|
| 1 | +/** Test the @throws annotation */ |
| 2 | +import java.io.IOException |
| 3 | + |
| 4 | +object TestThrows { |
| 5 | + |
| 6 | + abstract class Foo { |
| 7 | + |
| 8 | + @throws(classOf[IOException]) |
| 9 | + def read(): Int |
| 10 | + |
| 11 | + @throws(classOf[ClassCastException]) |
| 12 | + @throws(classOf[IOException]) |
| 13 | + def readWith2(): Int |
| 14 | + |
| 15 | + @throws(classOf[IOException]) |
| 16 | + @Deprecated |
| 17 | + @throws(classOf[NullPointerException]) |
| 18 | + def readMixed(): Int |
| 19 | + |
| 20 | + @Deprecated |
| 21 | + @throws(classOf[IOException]) |
| 22 | + @throws(classOf[NullPointerException]) |
| 23 | + def readMixed2(): Int |
| 24 | + |
| 25 | + @Deprecated |
| 26 | + def readNoEx(): Int |
| 27 | + } |
| 28 | + |
| 29 | + def checkMethod(cls: Class[_], name: String): Unit = { |
| 30 | + val method = cls.getMethod(name) |
| 31 | + println(name + " throws: " + method.getExceptionTypes.mkString("", ", ", "")) |
| 32 | + val annots = method.getDeclaredAnnotations.map(_.annotationType) |
| 33 | + println(name + " annotations: " + annots.mkString("", ", ", "")) |
| 34 | + } |
| 35 | + |
| 36 | + def run(cls: Class[_]): Unit = { |
| 37 | + checkMethod(cls, "read") |
| 38 | + checkMethod(cls, "readWith2") |
| 39 | + checkMethod(cls, "readMixed") |
| 40 | + checkMethod(cls, "readMixed2") |
| 41 | + checkMethod(cls, "readNoEx") |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** Test the top-level mirror that is has the annotations. */ |
| 46 | +object TL { |
| 47 | + |
| 48 | + @throws(classOf[IOException]) |
| 49 | + def read(): Int = 0 |
| 50 | + |
| 51 | + @throws(classOf[ClassCastException]) |
| 52 | + @throws(classOf[IOException]) |
| 53 | + def readWith2(): Int = 0 |
| 54 | + |
| 55 | + @throws(classOf[IOException]) |
| 56 | + @Deprecated |
| 57 | + @throws(classOf[NullPointerException]) |
| 58 | + def readMixed(): Int = 0 |
| 59 | + |
| 60 | + @Deprecated |
| 61 | + @throws(classOf[IOException]) |
| 62 | + @throws(classOf[NullPointerException]) |
| 63 | + def readMixed2(): Int = 0 |
| 64 | + |
| 65 | + @Deprecated |
| 66 | + def readNoEx(): Int = 0 |
| 67 | +} |
| 68 | + |
| 69 | +object Test { |
| 70 | + def main(args: Array[String]): Unit = { |
| 71 | + TestThrows.run(classOf[TestThrows.Foo]) |
| 72 | + println("Testing mirror class") |
| 73 | + TestThrows.run(Class.forName("TL")) |
| 74 | + } |
| 75 | +} |
0 commit comments