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

Scala example source code file (JavaStackFrame.scala)

This example Scala source code file (JavaStackFrame.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

array, array, framecontext, framecontext, int, javastackframe, javastackframe, nil, sources, stacktraceelement, string, string, throwable

The Scala JavaStackFrame.scala source code

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

package scala.tools.nsc
package util

import io.{ Fileish, Sources }
import Exceptional._

class FrameContext(frame: JavaStackFrame, codeSources: Sources) {
  val sourceFile = locateSource(codeSources, frame)
  import frame._

  def windowWidth = 3
  def windowSize = windowWidth * 2 + 1
  
  lazy val context = sourceFile collect {
    case f if line > 0 =>
      val start = math.max(0, line - windowWidth)
      f.lines().toList.slice(start, start + windowSize)
  } getOrElse Nil

  protected def fallbackContext = "%s (%s:%s)".format(tag, fileName, line)
  
  private def linestr(index: Int) = {
    val current = line - windowWidth + index
    val marker  = if (current == line) "*" else " "
    marker + current
  }
  private def contextLines = context.zipWithIndex map {
    case (l, i) => linestr(i) + ": " + l + "\n"
  }
  override def toString =
    if (context.isEmpty) fallbackContext
    else contextLines.mkString(tag + "\n", "", "")
}

object FrameContext {
  def apply(elem: StackTraceElement): FrameContext = apply(new JavaStackFrame(elem))
  def apply(frame: JavaStackFrame): FrameContext = new FrameContext(frame, Sources())
}

class JavaStackFrame(val elem: StackTraceElement) {
  def className: String  = elem.getClassName()
  def methodName: String = elem.getMethodName()
  def fileName: String   = elem.getFileName()
  def line: Int          = elem.getLineNumber()
  
  private def segs = className takeWhile (ch => ch != '$' && ch != '(') split '.' toList ;
  lazy val pkgName      = segs.init mkString "."
  lazy val shortName    = segs.last
  lazy val shortestName = if (fileName startsWith (shortName + ".")) "<--" else shortName
  
  private def standardString(which: String) =
    "%s.%s(%s:%s)".format(which, methodName, fileName, line)
  
  def locationString  = fileName + ":" + line
  def javaString      = standardString(className)
  def shortNameString = standardString(shortName)
  def tag             = "[%s.%s]".format(shortName, methodName)
  
  override def toString = shortNameString
}

object JavaStackFrame {
  def apply(elem: StackTraceElement) = new JavaStackFrame(elem)
  def frames(xs: Array[StackTraceElement]): Array[JavaStackFrame] = xs map (x => new JavaStackFrame(x))
  def frames(t: Throwable): Array[JavaStackFrame]                 = frames(Exceptional.unwrap(t).getStackTrace)
}

Other Scala examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.