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

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

问题描述

我想对注释值使用常量.

I would like to use constants for annotation values.

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.AInfo.B 可以在注解中使用,但不能在数组 Info.AB 中使用,因为它必须在这个地方成为一个数组初始值设定项.注释值仅限于可以内联到类的字节码中的值.这对于数组常量是不可能的,因为它必须在加载 Info 时构造.是否有解决此问题的方法?

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?

推荐答案

不,没有解决方法.

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

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