Adding code to an annotation

Overview

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.

Comments

  1. Hi, it is a great explanation to add behavior to custom annotation. I want to get some suggestion to add behavior to my custom annotation. Can you help me ?
    I have an annotation name as 'EncryptDemo' at field level. I want to encrypt all the field annotated with this annotation used in mongodb entity. Where should I add behavior of encryption ? I want to encrypt all string annotated with this annotation which directly get store in my mongodb database. Any help will be appreciate. Please guide me towards this.

    ReplyDelete
    Replies
    1. You need to add your functionality so it is called when then object is created or updated. You may also need to call this when it is read. You can alter your framework if you have one or add it to the getter/setters.

      Delete

Post a Comment

Popular posts from this blog

Java is Very Fast, If You Don’t Create Many Objects

System wide unique nanosecond timestamps

Comparing Approaches to Durability in Low Latency Messaging Queues