How to Add Type and Repeating Annotations to Code in Java 8

  • google plus

In Brief...

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.

Instructions

  1. 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:
    
    import java.lang.annotation.*;
    @Repeatable(value=MusicalInstruments.class)
    public @interface MusicalInstrument {
    	String  name() default "";
    	double price() default 0.0;
    }
    
    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 @Repeatable annotation indicates repeating instances of musical instrument can be stored in a collection (e.g., an array) returned by the value method in the referenced interface (in this case MusicalInstruments that you will code in a later step).
  2. Save your file as MusicalInstrument.java.
  3. 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:
    Compile program with Musical Instrument annotation

  4. Now you will create the interface that was referenced in the MusicalInstrument interface by means of the @Repeatable annotation. In your text editor, create the Java interface named MusicalInstruments. Type in the following Java statements:
    
    import java.lang.annotation.*;
    @Retention( RetentionPolicy.RUNTIME )
    public @interface MusicalInstruments {
    	MusicalInstrument[] value();
    }
    
    Notice that the interface defines one method, value, that returns an array of type MusicalInstrument. Recall that this method name was specified in the MusicalInstrument interface when the @Repeatable annotation was referenced. The retention policy specified by the @Retention annotation indicates that the array is to be retained at runtime.
  5. Save your file as MusicalInstruments.java.
  6. 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:
    Compile program with Musical Instrument annotations

  7. You are now ready to create your tester program. In your text editor, type in the following Java statements:
    
    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());
    		}
    	}
    }
    
    The @MusicalInstrument annotation is used to create each musical instrument that will be stored in the array. The name and price attributes correspond to the interface methods and provide the values returned by each method. The getAnnotationsByType method is used to display the @MusicalInstrument annotations used by this class. The classes that result from using the annotation are returned as an array. The getAnnotation method returns the result of using the @MusicalInstruments annotation. This result is a collection of musical instruments. The for loop is used to display the collection contents.
  8. Save your file as AddTypeAndRepeatingAnnotations.java.
  9. 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:
    Compile program to Test Musical Instrument annotation

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

Author: Stephen Withrow

Stephen has over 30 years' experience in training, development, and consulting in a variety of technology areas including Java, C, C++, XML, JavaScript, AJAX, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen is a published writer in both technical and non-technical endeavors. Stephen received an undergraduate degree in Computer Science and Physics from Florida State University.

Discuss