有没有办法从用作注释的案例类中可靠地找到 arg 值?

问题描述

我正在使用 Scala 编程语言。我们正在使用注解在类字段上应用一些元数据

例如

注解类

case class Address(city: City,zip: Zip)  extends StaticAnnotation //City and Zip are Enumeration types

使用注解的类

case class Person
(
  @Address(City.LA,Zip.someZip)
  Field: String
)

现在我想以字符串形式检索 city(即 LA)和 zip(即 SomeZip)的值,而不考虑参数的顺序。

我试过的是这个

val fields = typeOf[Person].typeSymbol.info.decls
      .find(d => d.isMethod && d.asMethod.isPrimaryConstructor)
      .get.typeSignature.paramLists.head
    
for(annotation <- field.annotations){
    println(annotation.toString) //returns Address(City.LA,Zip.someZip)
}

上面的代码工作并返回字符串,我可以解析和检索所需的值,如上所示。但它仅在以真实顺序提供参数时才有效 -> (City,Zip) 而不是相反。例如

@Address(zip = Zip.someZip,city = City.LA) // returns Address(x$4,x$3)

如何为每个 arg 检索正确的值(如果可能,则为枚举字符串 -> "LA"、"SomeZip")?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)