How to Add Type and Repeating Annotations to Code in Java 8
See Java: Tips and Tricks for similar articles.
Java developers typically need to store objects of a given type in a data structure such as an array. An efficient technique to achieve this goal is available
in Java 8 by adding type and repeating annotations to your Java application. The type annotation permits the developer to use an annotation in place of a class reference. The
repeating annotation, @Repeatable, calls out an annotated class that serves as the container for the objects.
To understand how to add type and repeating annotations to Java 8 code, follow these 10 steps.
- Open your text editor and create the Java program that will define the class that we wish to create, in this case a musical instrument.
Type in the following Java statements:
The class is not a class at all! Instead, you have coded an interface that defines the annotation methods that each have default implementations. Notice the
import java.lang.annotation.*; @Repeatable(value=MusicalInstruments.class) public @interface MusicalInstrument { String name() default ""; double price() default 0.0; }@Repeatableannotation indicates repeating instances of musical instrument can be stored in a collection (e.g., an array) returned by thevaluemethod in the referenced interface (in this caseMusicalInstrumentsthat you will code in a later step). - Save your file as
MusicalInstrument.java. - Open a command prompt and navigate to the directory containing your new Java program. Then type in the command to compile
the source and hit Enter:

- Now you will create the interface that was referenced in the
MusicalInstrumentinterface by means of the@Repeatableannotation. In your text editor, create the Java interface namedMusicalInstruments. Type in the following Java statements:Notice that the interface defines one method,import java.lang.annotation.*; @Retention( RetentionPolicy.RUNTIME ) public @interface MusicalInstruments { MusicalInstrument[] value(); }value, that returns an array of typeMusicalInstrument. Recall that this method name was specified in theMusicalInstrumentinterface when the@Repeatableannotation was referenced. The retention policy specified by the@Retentionannotation indicates that the array is to be retained at runtime. - Save your file as
MusicalInstruments.java. - Open a command prompt and navigate to the directory containing your Java program. Then type in the command to compile
the source and hit Enter:

- You are now ready to create your tester program.
In your text editor, type in the following Java statements:
The
import java.lang.annotation.*; @MusicalInstrument(name="Piano", price=1200.00) @MusicalInstrument(name="Trumpet", price=200) @MusicalInstrument(name="Clarinet", price=450) public class AddTypeAndRepeatingAnnotations { public static void main (String args[]) { // Retrieve annotations by type, i.e., MusicalInstrument // Returns an array MusicalInstrument[] annotations=AddTypeAndRepeatingAnnotations.class.getAnnotationsByType(MusicalInstrument.class); for (MusicalInstrument mI : annotations) { System.out.println(mI); } // Retrieve annotations using container class, i.e., MusicalInstruments // Returns an object of type container class MusicalInstruments instruments=AddTypeAndRepeatingAnnotations.class.getAnnotation(MusicalInstruments.class); for (MusicalInstrument mI : instruments.value()) { System.out.println(mI.name() + " cost: " + mI.price()); } } }@MusicalInstrumentannotation is used to create each musical instrument that will be stored in the array. Thenameandpriceattributes correspond to the interface methods and provide the values returned by each method. ThegetAnnotationsByTypemethod is used to display the@MusicalInstrumentannotations used by this class. The classes that result from using the annotation are returned as an array. ThegetAnnotationmethod returns the result of using the@MusicalInstrumentsannotation. This result is a collection of musical instruments. Theforloop is used to display the collection contents. - Save your file as
AddTypeAndRepeatingAnnotations.java. - Open a command prompt and navigate to the directory containing your Java program. Then type in the command to compile
the source and hit Enter:

- You are ready test your Java program. Type in the command to run the Java runtime launcher and hit Enter.
Notice the output shows the musical instruments created by calling the annotation and the musical instruments stored in the array.
