title: Scala reflection, finding and instantiating all classes with a given annotation
body: <p>I want use reflection to find, at runtime, all classes that have a given annotation, however I can't work out how to do so in Scala.  I then want to get the value of the annotation and dynamically instantiate an instance of each annotated class mapped to the value of the associated annotation.</p>
  
  <p>Here's what I want to do:</p>
  
  <pre><code>package problem
  import scala.reflect.runtime._
  
  object Program {
  
    case class Foo (key: String) extends scala.annotation.StaticAnnotation
  
    case class Bar ()
    @Foo ("x")
    case class Bar0 extends Bar
    @Foo ("y")
    case class Bar1 extends Bar
    @Foo ("z")
    case class Bar2 extends Bar
  
    def main (args : Array[String]): Unit = {
  
      // I want to use reflection to build
      // the following dynamically at run time:
      // val whatIWant: Map [String, Bar] =
      //   Map("x" -&gt; Bar0 (), "y" -&gt; Bar1 (), "z" -&gt; Bar2 ())
      // (it's a map of attribute key -&gt; an instance
      // of the type that has that attribute with that key)
      val whatIWant: Map [String, Bar] = ?
    }
  }
  </code></pre>
  
  <p>And, in the hope of being able to explain myself better, here's how I would solve the problem in C#.</p>
  
  <pre><code>using System;
  using System.Linq;
  using System.Reflection;
  using System.Collections.Generic;
  
  namespace scalaproblem
  {
      public class FooAttribute : Attribute
      {
          public FooAttribute (String s) { Id = s; }
          public String Id { get; private set; }
      }
  
      public abstract class Bar {}
  
      [Foo ("x")]
      public class Bar0: Bar {}
  
      [Foo ("y")]
      public class Bar1: Bar {}
  
      [Foo ("z")]
      public class Bar2: Bar {}
  
      public static class AttributeExtensions
      {
          public static TValue GetAttributeValue&lt;TAttribute, TValue&gt;(this Type type, Func&lt;TAttribute, TValue&gt; valueSelector) 
              where TAttribute : Attribute
          {
              var att = type.GetCustomAttributes (typeof(TAttribute), true).FirstOrDefault() as TAttribute;
              if (att != null)
                  return valueSelector(att);
              return default(TValue);
          }
      }
  
      public static class Program
      {
          public static void Main ()
          {
              var assembly = Assembly.GetExecutingAssembly ();
              Dictionary&lt;String, Bar&gt; whatIWant = assembly
                  .GetTypes()
                  .Where (t =&gt; Attribute.IsDefined (t, typeof(FooAttribute)))
                  .ToDictionary (t =&gt; t.GetAttributeValue((FooAttribute f) =&gt; f.Id), t =&gt; Activator.CreateInstance (t) as Bar);
  
              whatIWant.Keys.ToList().ForEach (k =&gt; Console.WriteLine (k + " ~ " + whatIWant [k]));
          }
      }
  }
  </code></pre>