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
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • How To Create and Edit PDF Annotations in Java
  • Testcontainers With Kotlin and Spring Data R2DBC
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements

Trending

  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • How To Deploy Helidon Application to Kubernetes With Kubernetes Maven Plugin
  • Exploring Edge Computing: Delving Into Amazon and Facebook Use Cases
  • Podman Desktop Review
  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.

Alex Soto user avatar by
Alex Soto
·
Feb. 14, 18 · Tutorial
Like (8)
Save
Tweet
Share
28.07K 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

  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • How To Create and Edit PDF Annotations in Java
  • Testcontainers With Kotlin and Spring Data R2DBC
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • 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: