如何指定在斯卡拉2.8注释的静态数组? [英] How do I specify a static array in a Scala 2.8 annotation?

查看:138
本文介绍了如何指定在斯卡拉2.8注释的静态数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Hibernate注解3.4.0建立了一些注解域类斯卡拉2.8.0。它一直工作正常,但也有它需要一个数组作为参数一定的注解。例如,下面是我想在斯卡拉前preSS一个Java注释:

I've been building out some annotated domain classes in Scala 2.8.0 using Hibernate Annotations 3.4.0. It's been working fine, except that there are certain annotations which take an array as a parameter. For example, here's a Java annotation that I want to express in Scala:

@OneToMany(mappedBy="passport_id", cascade=CascadeType.PERSIST)

不过,注释需要数组/设置为输入:

However, the annotation requires an array/set as input:

[ERROR] .../Passport.scala:50: error: type mismatch; 
[INFO]  found   : javax.persistence.CascadeType(value PERSIST)
[INFO]  required: Array[javax.persistence.CascadeType]
[INFO]     @OneToMany(mappedBy="passport_id", cascade=CascadeType.PERSIST)

我已经试过各种圆括号,方/角度/大括号,并依此类推:

I've tried various parentheses, square/angle/curly brackets, and so on:

@OneToMany(mappedBy="passport_id", cascade=(CascadeType.PERSIST))
@OneToMany(mappedBy="passport_id", cascade=[CascadeType.PERSIST])
@OneToMany(mappedBy="passport_id", cascade=<CascadeType.PERSIST>)
@OneToMany(mappedBy="passport_id", cascade={CascadeType.PERSIST})

...可惜我已经到了我的Scala / Java注解的理解结束。帮助是AP preciated。

... but unfortunately I've reached the end of my understanding of Scala/Java annotations. Help is appreciated.

推荐答案

我会从的规范解释为什么雷克斯的解决方案工作。

I'll add a few snippets from the spec to explain why Rex's solution works.

有关斯卡拉在JVM上,参数,将生成的类中必须保持恒定的前pressions注释:

For Scala on the JVM, arguments to annotations that will be retained within the generated class must be constant expressions:

注释类的实例,从继承特质
  scala.ClassfileAnnotation将存储在生成的类文件。 ......此外,在Java和.NET,所有构造函数的参数必须是常量前pressions。

Instances of an annotation class inheriting from trait scala.ClassfileAnnotation will be stored in the generated class files. ... Additionally, on both Java and .NET, all constructor arguments must be constant expressions.

什么是不变的前pressions?

What are constant expressions?

6.24 恒防爆pressions恒前pressions是前pressions的
  Scala编译器可以计算为
  不变。的常量的定义
  前pression取决于平台,
  但它们至少包括
  下面的表格前pressions:

6.24 Constant Expressions Constant expressions are expressions that the Scala compiler can evaluate to a constant. The definition of "constant expression" depends on the platform, but they include at least the expressions of the following forms:


      
  • 的一个文字值类,如整数

  •   
  • 字符串文字

  •   
  • 以predef.classOf(§12.4)
  • 构建类
      
  • 从底层平台枚举的元素

  •   
  • 字面值数组,形式阵列(C1,。,CN),所有
      词的的本身不变
      前pressions

  •   
  • 将一个恒定值定义(§4.1)。
  • 定义的标识符
      
  • A literal of a value class, such as an integer
  • A string literal
  • A class constructed with Predef.classOf (§12.4)
  • An element of an enumeration from the underlying platform
  • A literal array, of the form Array(c1, . . . , cn), where all of the ci ’s are themselves constant expressions
  • An identifier defined by a constant value definition (§4.1).

您也应该能够参数重构到最后VAL 。这似乎并不不过为阵列工作。我会提出一个错误。

You should also be able to refactor the argument to a final val. This doesn't seem to work for Arrays, however. I'll raise a bug.

class T(value: Any) extends ClassfileAnnotation

object Holder {
   final val as = Array(1, 2, 3)
   final val a = 1
} 

@T(Holder.a)
@T(Holder.as)  // annot.scala:9: error: annotation argument needs to be a constant; found: Holder.as
class Target

这篇关于如何指定在斯卡拉2.8注释的静态数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆