如何在注释使用数组常量 [英] How to use an array constant in an annotation

查看:105
本文介绍了如何在注释使用数组常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用常量标注值。

interface Client {

    @Retention(RUNTIME)
    @Target(METHOD)
    @interface SomeAnnotation { String[] values(); }

    interface Info {
    	String A = "a";
    	String B = "b";
    	String[] AB = new String[] { A, B };
    }

    @SomeAnnotation(values = { Info.A, Info.B })
    void works();

    @SomeAnnotation(values = Info.AB)
    void doesNotWork();
}

常量 Info.A Info.B 可以在注释中使用,但不是数组 Info.AB ,因为它有可能成为这个地方的数组初始化。注释值被限制在会内联到类的字节code值。这是不可能的阵列恒定,因为它有时被加载信息来建造。有没有一种解决方法这个问题?

The constants Info.A and Info.B can be used in the annotation but not the array Info.AB as it has to be an array initializer in this place. Annotation values are restricted to values that could be inlined into the byte code of a class. This is not possible for the array constant as it has to be constructed when Info is loaded. Is there a workaround for this problem?

推荐答案

没有,有没有解决方法。

No, there is no workaround.

这篇关于如何在注释使用数组常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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