| 
Scala example source code file (throws-annot.scala)
 The Scala throws-annot.scala source code
/** Test the @throws annotation */
import java.io.IOException
object TestThrows {
  abstract class Foo {
    @throws(classOf[IOException])
    def read(): Int
    @throws(classOf[ClassCastException])
    @throws(classOf[IOException])
    def readWith2(): Int
    @throws(classOf[IOException])
    @Deprecated
    @throws(classOf[NullPointerException])
    def readMixed(): Int
    @Deprecated
    @throws(classOf[IOException])
    @throws(classOf[NullPointerException])
    def readMixed2(): Int
    @Deprecated
    def readNoEx(): Int
  }
  def checkMethod(cls: Class[_], name: String) {
    val method = cls.getMethod(name)
    println(name + " throws: " + method.getExceptionTypes.mkString("", ", ", ""))
    println(name + " annotations: " + method.getDeclaredAnnotations.mkString("", ", ", ""))
  }
  def run(cls: Class[_]) {
    checkMethod(cls, "read")
    checkMethod(cls, "readWith2")
    checkMethod(cls, "readMixed")
    checkMethod(cls, "readMixed2")
    checkMethod(cls, "readNoEx")
  }
}
/** Test the top-level mirror that is has the annotations. */
object TL {
  
  @throws(classOf[IOException])
  def read(): Int = 0
  
  @throws(classOf[ClassCastException])
  @throws(classOf[IOException])
  def readWith2(): Int = 0
  
  @throws(classOf[IOException])
  @Deprecated
  @throws(classOf[NullPointerException])
  def readMixed(): Int = 0
  
  @Deprecated
  @throws(classOf[IOException])
  @throws(classOf[NullPointerException])
  def readMixed2(): Int = 0
  
  @Deprecated
  def readNoEx(): Int = 0
}
object Test {
  def main(args: Array[String]) {
    TestThrows.run(classOf[TestThrows.Foo])
    println("Testing mirror class")
    TestThrows.run(Class.forName("TL"))
  }
}
Other Scala examples (source code examples)Here is a short list of links related to this Scala throws-annot.scala source code file:  | 
| ... this post is sponsored by my books ... | |
         
           #1 New Release!  | 
      
         
           FP Best Seller  | 
  
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse 
URI on this website is
paid back to open source projects.