本文介绍了注释案例类参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试注释案例类的参数.
I am trying to annotate a parameter of a case class.
从此问题,然后从 scaladoc 我正在做的事情应该可以,但是由于某些原因,它不能:
Judging from this question, andand from this scaladoc, it looks like what I am doing should work, but for some reason it does not:
class Foo extends java.lang.annotation.Annotation { def annotationType = getClass }
case class Bar(@Foo @getter bar: String)
classOf[Bar].getDeclaredMethods.map(_.getDeclaredAnnotations.length)
res66: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
有什么想法吗?
推荐答案
用Java编写注释.另外, @getter
也应用作元注释.
Write your annotation in Java. Also @getter
should be used as a meta-annotation.
Foo.java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Foo {
}
Bar.scala
case class Bar(@(Foo @getter) bar: String)
然后
classOf[Bar].getDeclaredMethods.map(_.getDeclaredAnnotations.length)
// Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
这篇关于注释案例类参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!