DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Filtering Java Collections via Annotation-Driven Introspection
  • Mastering Spring: Synchronizing @Transactional and @Async Annotations With Various Propagation Strategies
  • Implementing Persistence With Clean Architecture
  • Composing Custom Annotations in Spring

Trending

  • Enterprise Data Loss Prevention (DLP) Security Policies and Tuning
  • How Kubernetes Cluster Sizing Affects Performance and Cost Efficiency in Cloud Deployments
  • Data Lake vs. Warehouse vs. Lakehouse vs. Mart: Choosing the Right Architecture for Your Business
  • Hyperparameter Tuning: An Overview and a Real-World Example
  1. DZone
  2. Coding
  3. Languages
  4. Repeatable Annotations in Java 8

Repeatable Annotations in Java 8

Do you use repeating annotations in your code? Here's an alternative approach that could make your annotations more compact and readable.

By 
Alex Soto user avatar
Alex Soto
·
Feb. 14, 18 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
30.1K Views

Join the DZone community and get the full member experience.

Join For Free

With Java 8, you are able to repeat the same annotation to a declaration or type. For example, to register that one class should only be accessible at runtime by specific roles, you could write something like:

@Role("admin")
@Role("manager")
public class AccountResource {
}


Notice that @Role is stated twice. For compatibility reasons, repeating annotations are stored in a container annotation, so instead of writing just one annotation, you need to write two, so in the previous case, you need to create @Role and @Roles annotations.

Notice that you need to create two annotations, one of which is the "plural" part of the annotation where you set the return type of the value method to be an array of the annotation that can be used multiple times. The other annotation can be used multiple times in the scope where it is defined and must be annotated with the @Repeatable annotation.

This is how I did it all the time, since Java 8 allows you to do it. But during a code review last week, my mate George Gastaldi pointed out how they are implementing these repeatable annotations in javax.validation spec. Of course, it is not completely different, but I think that looks pretty clear from the implementation point of view, since everything is implemented within the same archive and also, in my opinion, the name looks much more natural.

@Repeatable(Role.List.class)
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Role {

    String value();

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE})
    @interface List {
        Role[] value();
    }
}


Notice that everything is placed in the same archive. Since, usually, you only need to refer to the @Role class and not the @Roles (now @Role.List) annotation. You can hide this annotation as an inner annotation. Also, in the case of defining several annotations, this approach makes everything look more compact. Instead of populating the hierarchy with "duplicated" classes serving the same purpose, you only create one.

Of course, I am not saying that the approach of having two classes is wrong. At the end of the day, it is about preferences, since both are really similar. But after implementing repeatable annotations in this way, I think that it is cleaner and more compact solution, having everything defined in one class.

Annotation Java (programming language)

Published at DZone with permission of Alex Soto, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Filtering Java Collections via Annotation-Driven Introspection
  • Mastering Spring: Synchronizing @Transactional and @Async Annotations With Various Propagation Strategies
  • Implementing Persistence With Clean Architecture
  • Composing Custom Annotations in Spring

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!