Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.6 Repeating Annotations

If you like annotating your classes, you're going to find Java 8's support for repeating annotations very handy. In this lesson, you'll learn how to create and use such annotations at run time.

2.6 Repeating Annotations

Hello, and welcome back. In this lesson, you're going to learn how create and use repeating annotations in your Android apps. This is a new feature in Java 8, because in Java 7, annotations had a limitation that they couldn't repeat. In other words, if you wanted to provide multiple values for a single annotation, you would have had to use arrays. And doing that is not really concise or intuitive. Before we begin, I want you to understand that this feature is more geared towards Android Library developers, and Android Studio Annotation Processor developers. So don't worry if you can't find a use case for it, all right. Let's start by creating a simple annotation inside our activity. To create a new annotation you must use the at interface symbol, and provide a name for your annotation. I'll call this interface author. Let's say we want to receive a name of type string, and an age of type int from this annotation. And that's it, our simple annotation is ready. To be able to use the annotation, let's also create a class I'll call it book. We don't need to add anything to this class, so we will leave it's body empty. To add the author annotation to it, you just have to say @Author here, and provide a name and an age. So all this, I'm sure you're already familiar with because, this is just plain Java 7 code. Right now, our annotation cannot repeat, and that means this book class cannot have more than one author. So what if you want to support multiple authors? Thanks to Java 8, we can make this annotation repeatable. To do so we must use the @Repeatable annotation. This annotation needs another annotation type, that can store the values of our repeating annotation, as an array. So let's say that annotation is called Authors. The Authors type doesn't exist yet, so we must now create it. Again, use the @Interface symbol to do so. Inside this annotation, all we need to do is create an array of Author annotations. At this point, our annotation can repeat. If I want to, I can now include another author annotation for the same class. I'll just add one more because, you're free to add any number of annotations this way. Now, you can leave this annotations as they are and call them complete. They can serve as code documentation, and they do look better than simple comments. But for the sake of completeness, let me also show you how to use these annotations at run time. Let's say that you want to print all these names and ages, from your activity. To do that, we must first change the retention policy of our repeating annotation. So you must use the at retention annotation here, and say that you want a runtime retention policy. The supporting annotation too must have the same retention policy so, you can simply copy this and paste it here. All right now these annotations will be available to us inside our activity. To get all of them as an array, you can call the book.class.getAnnotationsByType method. This is a new method that is available only in Java 8. We want all the author annotations, so you can pass Author.class as an argument to this method. The method returns an array, so let's convert it into a stream, to be able to use lambdas with it. And now, we can call the for each method. This x here, will contain the name and age of each author that we mentioned above. So to get the name, you can say x.name, And to get the age you can get x.age. If you run your activity now, you should be able to see that it prints the names of all the authors correctly. You now know how to use Java 8's repeating annotations in your Android apps. Thanks for watching.

Back to the top