Scala reflection, finding and instantiating all cl

2019-07-07 02:37发布

问题:

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.

Here's what I want to do:

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" -> Bar0 (), "y" -> Bar1 (), "z" -> Bar2 ())
    // (it's a map of attribute key -> an instance
    // of the type that has that attribute with that key)
    val whatIWant: Map [String, Bar] = ?
  }
}

And, in the hope of being able to explain myself better, here's how I would solve the problem in C#.

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<TAttribute, TValue>(this Type type, Func<TAttribute, TValue> 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<String, Bar> whatIWant = assembly
                .GetTypes()
                .Where (t => Attribute.IsDefined (t, typeof(FooAttribute)))
                .ToDictionary (t => t.GetAttributeValue((FooAttribute f) => f.Id), t => Activator.CreateInstance (t) as Bar);

            whatIWant.Keys.ToList().ForEach (k => Console.WriteLine (k + " ~ " + whatIWant [k]));
        }
    }
}

回答1:

The most practical answer is to use the reflections library to scan the classpath (or a subset of it) for all classes with a particular annotation; you can then instantiate those using either the Java reflection API or scala reflection.

(Note that this is not 100% reliable because e.g. classloaders are allowed to be dynamic, so it's possible to have a class that doesn't show up in the scan. But in practice, for "normal" use cases (i.e. loading classes from ordinary jar files) it works well)