注释案例类参数 [英] Annotating case class parameters

查看:48
本文介绍了注释案例类参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试注释案例类的参数.

I am trying to annotate a parameter of a case class.

此问题,然后从 scaladoc 我正在做的事情应该可以,但是由于某些原因,它不能:

Judging from this question, and and 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)

为什么无法在以下位置访问用Scala编写的注释运行时?

这篇关于注释案例类参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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