Ernest Duodu Ernest is a Java developer. Outside programming, he also enjoys a wide variety of hobbies which includes sky-diving, photography, exercising and listening to music.

Java 8 Repeating Annotation Explained in 5 minutes

1 min read

At IDR Solutions we use Java 8 for the development of our products (a Java PDF Viewer and SDKPDF to HTML5 converter and a Java ImageIO replacement). In this article, we will be looking at Repeating Annotations…

What is Repeating Annotations?

Prior to Java8, it was forbidden to declare more than one annotation of the same type to the same location of a code. Java8 introduces the repeating annotation. This means the same annotations can be repeated as much as you want at the same locations.

Let’s have a look at this code.

Prior to Java8, to have a repeated annotation, will have to group them in a container annotation

@Manufactures({
@Manufacturer(name =”BMW”),
@Manufacturer(name = “Range Rover”)

})
public class Car{
//code goes in here
}

With Java8 repeating annotations, it gives us the flexibility to write the same thing without any container annotation

@Manufacturer(name = “BMW”)
@Manufacturer(name= “Range Rover”)
public class Car{
//code goes in here
}

Though the container annotation was not used here, the Java compiler this time around takes responsibility for wrapping the two annotations into a container.

Now let’s look at how you would use Repeating annotation.

package repeatingannotations;

/**
 *
 * @author imac
 */import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class RepeatingAnnotations {
  
   @Retention( RetentionPolicy.RUNTIME )
    public @interface Cars {
        Manufacturer[] value() default{};
    }
   
    @Manufacturer( "Mercedes Benz")
    @Manufacturer( "Toyota")
    @Manufacturer( "BMW")
    @Manufacturer( "Range Rover")
    public interface Car { 
        
    }
    
    
    @Repeatable(value = Cars.class )
    public @interface Manufacturer {
        String value();
        
    };
  
    
    
    public static void main(String[] args) {
        
        Manufacturer[] a = Car.class.getAnnotationsByType(Manufacturer.class );
        System.out.println("Number of car manufacturers is "+ a.length );
      
        System.out.println("\n-------Printing out Car Manufacturers--------");
        
        Cars cars = Car.class.getAnnotation(Cars.class);
        for(Manufacturer car: cars.value()){
            System.out.println(car.value());
        }
        
    }
}

Output

Number of car manufacturers is 4——-Printing out Car Manufacturers——–
Mercedes Benz
Toyota
BMW
Range Rover

As you can see, the repeating annotation has to be annotated with @Repeatable annotation.

Retrieving Annotation

There are two ways of accessing them.

  1. Accessing them via their container annotation using getDeclaredAnnotation() or getAnnotation().In the code above, we used this to print out the car manufactures
  2. Accessing them via the getAnnotationsByType() or getDeclaredAnnotationsByType() which are new methods in the Reflection API. We also used this method to print out the number of manufacturers in the code above.

Take-Aways

By default, user-defined annotations are not repeatable therefore they have to be annotated with @Repeatable annotation.

If you found this article useful, you may also be interested in our other Java8 posts on Lambda Expression, Streams API, Default MethodsMethod References, Optional and Consumer Suppliers.

We also now have a large collection of articles on what is new other versions of Java, including Java9.



Our software libraries allow you to

Convert PDF to HTML in Java
Convert PDF Forms to HTML5 in Java
Convert PDF Documents to an image in Java
Work with PDF Documents in Java
Read and Write AVIF, HEIC, WEBP and other image formats
Ernest Duodu Ernest is a Java developer. Outside programming, he also enjoys a wide variety of hobbies which includes sky-diving, photography, exercising and listening to music.

One Reply to “Java 8 Repeating Annotation Explained in 5 minutes”

  1. Keep in mind that the Manufacturer annotation must also have a @Retention(RetentionPolicy.RUNTIME) specified on it, to be able to to annotate with only 1 Manufacturer a class, and read it properly.

Comments are closed.