Java 8 study (repeatable)

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

Java 8 study (repeatable)
Let's study Java
[Java] Study notes
Java study memorandum
Study Java Silver 1
Java Silver Study Day 1
[Java ~ Method ~] Study memo (5)
Java study # 1 (typical type)
[Java ~ Array ~] Study memo 4
My Study Note (Java)
Study Java # 2 (\ mark and operator)
Study java arrays, lists, maps
Java Silver Study Method Memo
[Java ~ Boolean value ~] Study memo (2)
Java study # 7 (branch syntax type)
Java
Java
Study Java with Progate Note 1
How to study Java Silver SE 8
Summary of [Java silver study] package
[In-house study session] Java exception handling (2017/04/26)
[Study session memo] Java Day Tokyo 2017
Java study # 4 (conditional branching / if statement)
Orcacla Java Bronze SE 7/8 Qualification Study
Java study # 5 (iteration and infinite loop)
Study Deep Learning from scratch in Java.
Java learning (0)
Studying Java ―― 3
[Java] array
Summary of in-house newcomer study session [Java]
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java string
java (array)
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
[Java ~ Variable definition, type conversion ~] Study memo
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1