summaryrefslogtreecommitdiff
path: root/src/repl/scala/tools/nsc/interpreter/AbstractOrMissingHandler.scala
blob: df49e6a2e4718b75f5d3dcb1772c00700cc4872c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* 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 programmatically, settings.usejavacp.value = true.""".stripMargin.format(x.req)
      )
      value
  }
}

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