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

Scala example source code file (VerifyClass.scala)

This example Scala source code file (VerifyClass.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

array, as, classloader, compiler, exceptionininitializationerror, jar, none, nsc, option, string, throwable, todo, utilities

The VerifyClass.scala Scala example source code

package scala.tools.util

import scala.tools.nsc.io._
import java.net.URLClassLoader
import scala.collection.JavaConverters._
import scala.language.postfixOps

object VerifyClass {

  // Returns the error if there's a failure
  private def checkClass(name : String, cl: ClassLoader) : (String, Option[String]) = {
    try   {
      Class.forName(name, true, cl)
      (name, None)
    } catch {
      case x: Throwable => // TODO: only catch VerifyError (and related) + ExceptionInInitializationError (for static objects that bomb on classload)
        (name, Some(x.toString))
    }
  }

  def checkClassesInJar(name: String, cl: ClassLoader) = new Jar(File(name)) filter (_.getName.endsWith(".class")) map { x =>
    checkClass(x.getName.stripSuffix(".class").replace('/', '.'), cl)
  } toMap

  def checkClassesInDir(name: String, cl: ClassLoader) = (for {
    file <- Path(name).walk
    if file.name endsWith ".class"
  } yield checkClass(name, cl)) toMap

  def checkClasses(name: String, cl: ClassLoader) =
    if (name endsWith ".jar")  checkClassesInJar(name, cl)
    else checkClassesInDir(name, cl)

  /** Attempts to load all classes on the classpath defined in the args string array.  This method is meant to be used via reflection from tools like SBT or Ant. */
  def run(args: Array[String]): java.util.Map[String, String] = {
    val urls = args.map(Path.apply).map(_.toFile.toURI.toURL).toArray
    println("As urls: " + urls.mkString(","))
    val cl = URLClassLoader.newInstance(urls, null)
    val results = args.flatMap(n => checkClasses(n, cl)).toMap
    (for { (name, result) <- results } yield (name, result.getOrElse(null))).asJava
  }


  def main(args: Array[String]): Unit = {
    val results = run(args).asScala
    println("Processed " + results.size + " classes.")
    val errors = results.filter(_._2 != null)
    for( (name, result) <- results; if result != null) {
      println(name + " had error: " + result)
    }
    System.exit(if(errors.size > 0) 1 else 0)
  }
}

Other Scala source code examples

Here is a short list of links related to this Scala VerifyClass.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.