Scala反射,使用给定的注释查找和实例化所有类

时间:2015-01-29 12:18:15

标签: scala reflection annotations

我想使用反射在运行时查找具有给定注释的所有类,但是我无法知道如何在Scala中执行此操作。然后我想获取注释的值并动态实例化每个注释类的实例,映射到相关注释的值。

这就是我想要做的事情:

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] = ?
  }
}

并且,为了能够更好地解释自己,这就是我将如何解决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 个答案:

答案 0 :(得分:2)

最实用的答案是使用reflections library扫描具有特定注释的所有类的类路径(或其子集);然后,您可以使用Java反射API或scala反射来实例化它们。

(请注意,这并非100%可靠,因为例如允许类加载器是动态的,因此可以在扫描中显示一个不会显示的类。但在实践中,对于& #34;普通&#34;用例(即从普通jar文件加载类)它运行良好)