Scala反射:查找并实例化所有具有特定注解的类

5

我希望在运行时使用反射来查找所有具有给定注释的类,但是我不知道如何在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个回答

3
最实际的方法是使用reflections库来扫描类路径(或其子集)中所有带有特定注解的类;然后您可以使用Java反射API或Scala反射实例化这些类。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接