<tfoot id='8rpNi'></tfoot>
  1. <small id='8rpNi'></small><noframes id='8rpNi'>

    1. <legend id='8rpNi'><style id='8rpNi'><dir id='8rpNi'><q id='8rpNi'></q></dir></style></legend>
        <bdo id='8rpNi'></bdo><ul id='8rpNi'></ul>

      <i id='8rpNi'><tr id='8rpNi'><dt id='8rpNi'><q id='8rpNi'><span id='8rpNi'><b id='8rpNi'><form id='8rpNi'><ins id='8rpNi'></ins><ul id='8rpNi'></ul><sub id='8rpNi'></sub></form><legend id='8rpNi'></legend><bdo id='8rpNi'><pre id='8rpNi'><center id='8rpNi'></center></pre></bdo></b><th id='8rpNi'></th></span></q></dt></tr></i><div id='8rpNi'><tfoot id='8rpNi'></tfoot><dl id='8rpNi'><fieldset id='8rpNi'></fieldset></dl></div>

      如何从常量 java 为注解提供值

      时间:2023-07-26

      <tfoot id='7eHJE'></tfoot>
        <tbody id='7eHJE'></tbody>

        <small id='7eHJE'></small><noframes id='7eHJE'>

      • <legend id='7eHJE'><style id='7eHJE'><dir id='7eHJE'><q id='7eHJE'></q></dir></style></legend>

        1. <i id='7eHJE'><tr id='7eHJE'><dt id='7eHJE'><q id='7eHJE'><span id='7eHJE'><b id='7eHJE'><form id='7eHJE'><ins id='7eHJE'></ins><ul id='7eHJE'></ul><sub id='7eHJE'></sub></form><legend id='7eHJE'></legend><bdo id='7eHJE'><pre id='7eHJE'><center id='7eHJE'></center></pre></bdo></b><th id='7eHJE'></th></span></q></dt></tr></i><div id='7eHJE'><tfoot id='7eHJE'></tfoot><dl id='7eHJE'><fieldset id='7eHJE'></fieldset></dl></div>
            • <bdo id='7eHJE'></bdo><ul id='7eHJE'></ul>
                本文介绍了如何从常量 java 为注解提供值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我认为这在 Java 中可能是不可能的,因为注解及其参数是在编译时解析的.我有一个界面如下,

                I am thinking this may not be possible in Java because annotation and its parameters are resolved at compile time. I have an interface as follows,

                public interface FieldValues {
                   String[] FIELD1 = new String[]{"value1", "value2"};
                }
                

                和另一个类,

                @SomeAnnotation(locations = {"value1", "value2"})
                public class MyClass {
                   ....
                }
                

                我用注释标记了许多类,我想知道是否可以避免在我更喜欢使用的每个注释中指定字符串

                I mark many classes with the annotation and I would like to know if I can avoid specifying the strings in every annotation I would instead prefer to use

                @SomeAnnotation(locations = FieldValues.FIELD1)
                public class MyClass {
                   ....
                }
                

                但是这会产生编译错误,例如注释值应该是数组初始化器等.有人知道我如何使用 String 常量或 String[] 常量来为注释提供值吗?

                However this gives compilation errors like annotation value should be an array initializer etc. Does someone know how I can use a String constant or String[] constant to supply value to an annotation?

                推荐答案

                编译常量只能是原语和字符串:

                15.28.常量表达式

                编译时常量表达式是一个表达式,表示原始类型的值或不会突然完成且仅使用以下内容组成的字符串:

                A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

                • 原始类型的文字和 String
                • 类型的文字
                • 转换为原始类型并转换为 String
                • [...] 运算符 [...]
                • 带括号的表达式,其包含的表达式是常量表达式.
                • 引用常量变量的简单名称.
                • TypeName 形式的限定名称.标识符表示常量变量.
                • Literals of primitive type and literals of type String
                • Casts to primitive types and casts to type String
                • [...] operators [...]
                • Parenthesized expressions whose contained expression is a constant expression.
                • Simple names that refer to constant variables.
                • Qualified names of the form TypeName . Identifier that refer to constant variables.

                实际上在java中没有办法保护数组中的项目.在运行时,总是有人可以执行 FieldValues.FIELD1[0]=value3",因此如果我们更深入地观察,数组不可能是真正的常量.

                Actually in java there is no way to protect items in an array. At runtime someone can always do FieldValues.FIELD1[0]="value3", therefore the array cannot be really constant if we look deeper.

                这篇关于如何从常量 java 为注解提供值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:@Transactional 注解属于哪里? 下一篇:Hibernate Annotations - 哪个更好,字段访问还是属性

                相关文章

                最新文章

                  <bdo id='mRl3n'></bdo><ul id='mRl3n'></ul>
              1. <legend id='mRl3n'><style id='mRl3n'><dir id='mRl3n'><q id='mRl3n'></q></dir></style></legend>

                <small id='mRl3n'></small><noframes id='mRl3n'>

                <i id='mRl3n'><tr id='mRl3n'><dt id='mRl3n'><q id='mRl3n'><span id='mRl3n'><b id='mRl3n'><form id='mRl3n'><ins id='mRl3n'></ins><ul id='mRl3n'></ul><sub id='mRl3n'></sub></form><legend id='mRl3n'></legend><bdo id='mRl3n'><pre id='mRl3n'><center id='mRl3n'></center></pre></bdo></b><th id='mRl3n'></th></span></q></dt></tr></i><div id='mRl3n'><tfoot id='mRl3n'></tfoot><dl id='mRl3n'><fieldset id='mRl3n'></fieldset></dl></div>

              2. <tfoot id='mRl3n'></tfoot>