alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (AbstractOrMissingHandler.scala)

This example Scala source code file (AbstractOrMissingHandler.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Scala source code examples by using tags.

All credit for the original source code belongs to scala-lang.org; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Scala tags/keywords

abstractmethoderror, abstractormissinghandler, compiler, failed, missingrequirementerror, noclassdeffounderror, nosuchmethoderror, nsc, otherwise, string, t, throwable

The AbstractOrMissingHandler.scala Scala example source code

/* NSC -- new Scala compiler
 * Copyright 2005-2013 LAMP/EPFL
 * @author  Paul Phillips
 */

package scala.tools.nsc
package interpreter

class AbstractOrMissingHandler[T](onError: String => Unit, value: T) extends PartialFunction[Throwable, T] {
  def isDefinedAt(t: Throwable) = t match {
    case _: AbstractMethodError     => true
    case _: NoSuchMethodError       => true
    case _: MissingRequirementError => true
    case _: NoClassDefFoundError    => true
    case _                          => false
  }
  def apply(t: Throwable) = t match {
    case x @ (_: AbstractMethodError | _: NoSuchMethodError | _: NoClassDefFoundError) =>
      onError("""
        |Failed to initialize compiler: %s.
        |This is most often remedied by a full clean and recompile.
        |Otherwise, your classpath may continue bytecode compiled by
        |different and incompatible versions of scala.
        |""".stripMargin.format(x.getClass.getName split '.' last)
      )
      x.printStackTrace()
      value
    case x: MissingRequirementError =>
      onError("""
        |Failed to initialize compiler: %s not found.
        |** Note that as of 2.8 scala does not assume use of the java classpath.
        |** For the old behavior pass -usejavacp to scala, or if using a Settings
        |** object programatically, settings.usejavacp.value = true.""".stripMargin.format(x.req)
      )
      value
  }
}

object AbstractOrMissingHandler {
  def apply[T]() = new AbstractOrMissingHandler[T](Console println _, null.asInstanceOf[T])
}

Other Scala source code examples

Here is a short list of links related to this Scala AbstractOrMissingHandler.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

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.