Writing Custom Annotations

back to index

Writing custom annotations that integrate in the framework is also very easy. Follow these steps:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import com.planetalia.dynject.metamodel.Accepts;
import com.planetalia.dynject.metamodel.Implementation;


@Target(value={ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Accepts({place here the list of classes you will accept })
@Implementation(Class that implements your annotation)
public @interface MyAnnotation {

Include your custom annotation attributes


/**
* Literal message to use if validation fails.
* @return Literal message to use if validation fails.
*/
String message() default "";
/**
* Error code to use if validation fails.
* @return Error code to use if validation fails.
*/
String errorCode() default "";
/**
* Message key to use if validation fails. Default is {@link Messages#REQUIRED_FIELD}
* @return Message key to use if validation fails.
*/
String messageKey() default Messages.REQUIRED_FIELD;
}

 

A useful starting point would be to look at the source code of some of the existing annotations. Generally speaking in Dynject annotation @X is implemented in XImpl ( @Required in RequiredImpl, @MinInclusive in MinInclusiveImpl and so forth)