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

Scala example source code file (Show.scala)

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

a, a, illegalaccessexception, illegalargumentexception, list, list, show, string, string, symapply, symbol, symbol

The Scala Show.scala source code

/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2011, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */



package scala.testing

/** <p>
 *    Classes inheriting trait <code>Show can test their member methods
 *    using the notattion <code>meth(arg1, ..., argn),
 *    where <code>meth is the name of the method and
 *    <code>arg1,...,argn are the arguments.
 *    The only difference to a normal method call is the leading quote
 *    character ('). A quoted method call like the one above will produces a
 *    legible diagnostic to be printed on <a href="../Console.html"
 *    target="ContentFrame"><code>Console. It is of the form
 *  </p>
 *    meth(arg<sub>1, ..., argn)  gives  <result>
* <p> * where <code><result> is the result of evaluating the call. * </p> */ trait Show { /** The result class of wrapper <code>symApply. * Prints out diagnostics of method applications. */ class SymApply(f: Symbol) { def apply[A](args: A*) { println(test(f, args: _*)) } } /** An implicit definition that adds an apply method to Symbol which forwards to `test`. */ implicit def symApply(sym: Symbol) = new SymApply(sym) /** Apply method with name of given symbol `f` to given arguments and return * a result diagnostics. */ def test[A](f: Symbol, args: A*): String = { val args1 = args map (_.asInstanceOf[AnyRef]) def testMethod(meth: java.lang.reflect.Method): String = f.name+"("+(args mkString ",")+") gives "+ { try { meth.invoke(this, args1: _*) } catch { case ex: IllegalAccessException => ex case ex: IllegalArgumentException => ex case ex: java.lang.reflect.InvocationTargetException => ex } } getClass.getMethods.toList filter (_.getName == f.name) match { case List() => f.name+" is not defined" case List(m) => testMethod(m) case ms => // multiple methods, disambiguate by number of arguments ms filter (_.getParameterTypes.length == args.length) match { case List() => testMethod(ms.head) // go ahead anyway, to get an exception case List(m) => testMethod(m) case ms => "cannot disambiguate between multiple implementations of "+f.name } } } }

Other Scala examples (source code examples)

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