Bookmark and Share

Custom Annotations

CRest allows you to define your own annotations.

All you need to do is to define an AnnotationHandler implementation and bind it to your custom annotation.

In practice, if you have the following annotation:

@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE, java.lang.annotation.ElementType.METHOD})
public @interface SomeCustomAnnotation {

    (...)

}

And implemented it's handler:

import org.codegist.crest.config.*;
import org.codegist.crest.config.annotate.AnnotationHandler;

public class SomeCustomAnnotationAnnotationHandler implements AnnotationHandler<SomeCustomAnnotation> {

    @Override
    public void handleInterfaceAnnotation(SomeCustomAnnotation annotation, InterfaceConfigBuilder builder)  throws Exception {
        (...)
    }

    @Override
    public void handleMethodAnnotation(SomeCustomAnnotation annotation, MethodConfigBuilder builder)  throws Exception {
        (...)
    }

    @Override
    public void handleParameterAnnotation(SomeCustomAnnotation annotation, ParamConfigBuilder builder) throws Exception {
        (...)
    }

}

Then to let CRest know how to handle it, you'll need to bind it as follow:

CRest crest = new CRestBuilder()
                    .bindAnnotationHandler(SomeCustomAnnotationAnnotationHandler.class, SomeCustomAnnotation.class)
                    .build();

Note that annotation handling is a process that occures only once per REST interface, during build time (see org.codegist.crest.CRest.build(Class)). The handlers won't be invoked at run-time during REST proxies calls.