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 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
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Adding Code to an Annotation

Adding Code to an Annotation

Peter Lawrey user avatar by
Peter Lawrey
·
Mar. 15, 12 · Interview
Like (0)
Save
Tweet
Share
6.92K Views

Join the DZone community and get the full member experience.

Join For Free
Sometimes you have a framework you would like to be able to add custom annotations to. The problem is; how do you define what this custom annotation does. A possible solution is to use a nested enum.

Nested types

You can nest an interface, a class, an annotation or an enum inside any of these types. e.g.
public class A {
    public interface B {
        public enum C {;
            public @interface D {
                public class E {
                    // etc etc
                }
            }
        }
    }
}
But is this useful or just a novelty?

Adding functionality to an annotation

You can define an interface the framework can call to determine how your custom annotation should behave.
import java.lang.reflect.Member;

/**
* @author peter.lawrey
*/
public interface AnnotationHandler<A> {
    void process(Model model, Member member, A annotation);
}
One way of associating the implementation with the annotation itself is to provide a Singleton enum which implements this interface.
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Member;

/**
* @author peter.lawrey
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation {
    public int value() default 0;
    
    enum HandlesAnnotation implements AnnotationHandler<CustomAnnotation> {
        INSTANCE;

        @Override
        public void process(Model model, Member member, CustomAnnotation ca) {
            // do something with a model based on the members details and the annotation state.
        }
    }
}
If an annotation could be handled a number of different ways (depending on what process you are performing), you could provide more than one implementation. Using this approach, a framework which uses annotations can allow developers to add custom annotations is a simple manner. The framework can also make it clear how an annotation behaves, making it easier to create annotations which are similar but need to be different in some way.
Annotation

Published at DZone with permission of Peter Lawrey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Event Driven 2.0
  • Integrate AWS Secrets Manager in Spring Boot Application
  • Distributed Tracing: A Full Guide
  • 5 Best Python Testing Frameworks

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

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: