Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set String Array in Java Annotation

I have declared a annotation like this:

public @interface CustomAnnot  {     String[] author() default "me";     String description() default ""; } 

A valid Annotation therefore would be

@CustomAnnot(author="author1", description="test") 

What I can't figure out is, how to set more than one author, since author() has return String[] this should be possible.

@CustomAnnot(author="author1","autor2", description="test") 

doesn't work!

like image 258
Jakob Abfalter Avatar asked Dec 17 '13 11:12

Jakob Abfalter


People also ask

How do you declare a string array?

The String Array is initialized at the same time as it is declared. You can also initialize the String Array as follows: String[] strArray = new String[3]; strArray[0] = “one”; strArray[1] = “two”; strArray[2] = “three”; Here the String Array is declared first.

How do you use an array constant in annotation?

if the compiler expects an "Array Initializer" to be passed to the Annotation, declaring a compile-time constant like private static final String[] AB = { ... }; should do. it's understood that Annotation processing happens before the actual compilation, but then the error message is not accurate.

Can we declare a string array in Java?

It can be declared by the two methods; by specifying the size or without specifying the size. It can be initialized either at the time of declaration or by populating the values after the declaration. The elements can be added to a String Array after declaring it. The String Array can be iterated using the for loop.

How to add elements to a string array in Java?

Adding Elements to a String Array. We can easily add the elements to the String Array just like other data types. It can be done using the following three methods: Using Pre-Allocation of the Array; Using the Array List; By creating a new Array; let's understand the above methods: Using Pre-Allocation of the Array:

Where do you put annotations in Java?

As mentioned above, Java annotations can be placed above class, method, interface, field, and other program element declarations. If the above program is compiled without using the @SuppressWarnings ("unchecked") annotation, the compiler will still compile the program but it will give warnings like:

How to initialize string array in Java?

How to Initialize String Array in Java? To initialize String Array in Java, define a string array and assign a set of elements to the array, or define a string array with specific size and assign values to the array using index. Declare a variable of type String [] and assign set of strings to it using assignment operator.

Is it possible to add an array value to an annotation?

You cannot supply an array value to annotation from constant ( read more ). It is also not possible to create annotation that extends another annotation ( read more ). I don't know the context, but have you considered passing this information into the object itself, and storing it as a field, rather than via annotations?


1 Answers

Do it like this:

public @interface CustomAnnot {      String[] author() default "me";     String description() default "";  } 

And your annotation:

    @CustomAnnot(author={"author1","author2"}, description="test") 
like image 125
Kai Sternad Avatar answered Oct 13 '22 14:10

Kai Sternad