Not sure if the title is misleading, but the requirement is lower.
I need to use a string value as input for a custom annotation. When using the enum value, the IDE gives
The value of the java attribute must be constant.
@test("test") // works @test(Const.myEnum.test.toString()) //java attribute value must be constant
I read about the importance of immutable string value. Is it possible to achieve the result through enum (and not public static final hacking of the string).
thank.
If the annotation is inside your control, enter the attribute type enuminstead String. Otherwise, this is not possible.
enum
String
, , java, (.. Test, Test):
Test
// retention, target here public @interface Test { YourEnum value(); }
. :
@test(Const.myEnum.test)
, :
package Const; public enum myEnum { test; }
:
public @interface test { myEnum value(); }
enum, , . , - .
@Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.METHOD }) public @interface MyAnnotation { MyEnum value(); public enum MyEnum { ONE, TWO, THREE, FOUR } } public class AnnotationTest { @MyAnnotation(MyEnum.ONE) public void someMethod() { //... } }
, , , String. - , , "toString" "" .
, toString()
But you should be able to use enumeration constants.
Source: https://habr.com/ru/post/1758011/More articles:Convert date string to string format of desired date - iphoneHow to tag header for HTML lists - htmlHow to specify paths in PHP? - phpbarcode scanner detection from web browser - browserMicrosoft ReportViewer - ASP.NET session expired - reportviewerScala среда программирования - scalaKext low-level hotkey to kill WindowServer? - low-leveljquery с панелью обновления - javascriptAndroid sdk & timers (Chronometer vs. Thread) - javaNeed help getting an HTML website in Java - javaAll Articles