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

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

问题描述

我想为注释值使用常量.

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天全站免登陆