This time it is repeatable. It seems that it will be possible to write the same annotation multiple times in the same place. Sample immediately
/**
*Appropriate self-made annotation.
*In the sense of declaring that Sake List will appear repeatedly by the way@Repeatable is attached.
* @author komikcomik
*
*/
@Repeatable(SakeList.class)
public @interface Sake {
String name();
}
/**
*Appropriate self-made annotation.
*I have an array of Sake, which allows me to use the sake annotation repeatedly.
* @author komikcomik
*
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface SakeList {
Sake[] value();
}
As I wrote in the comment, it is an image that declares that multiple Sake will appear on the SakeList side by writing @Repeatable (SakeList.class) on the Sake side. It feels like the SakeList side also has a Sake array corresponding to it. Also, if the method name is not value, I got a compile error.
So, an example of using these.
import java.lang.annotation.Annotation;
@Sake(name = "Midori")
@Sake(name = "Ichigin")
@Sake(name = "Ryusen Yae Sakura")
/**
*Studying repeatable.
* @author komikcomik
*
*/
public class SakeListUser {
public static void main(String[] args) {
Annotation[] anoList = SakeListUser.class.getAnnotationsByType(Sake.class);
for (int i = 0; i < anoList.length; i++) {
System.out.println(anoList[i]);
}
}
}
Execution result
@java8.repeatable.Sake(name=Midori)
@java8.repeatable.Sake(name=Ichigin)
@java8.repeatable.Sake(name=Ryusen Yae Sakura)
I want to take out only the back of name =, but I wasn't sure how to do it ...
2017/10/16 postscript It will be the content that you told me in the comment section as it is, but it seems that you can get it by doing the following.
Sake[] Sakes = SakeListUser.class.getAnnotationsByType(Sake.class);
for (int i = 0; i < Sakes.length; i++) {
// Sake#name()Can be called as a method
System.out.println(Sakes[i].name());
}
When executed, it will be as follows. Nice!
Midori
Ichigin
Ryusen Yae Sakura
Recommended Posts