Java Reference
In-Depth Information
LISTING 6‐27:
LongMessage implementation of message interface
@Message(Message.Type.LONG)
@Dependent
public class LongMessage implements
MessageType
{
private String message;
@Override
public String getMessage() {
return message;
}
@Override
public void setMessage(String message) {
this.message = message;
}
}
Each concrete implementation of the
MessageType
interface, as shown in Listing 6-25, is annotated
with an
@Message
qualii er denoting the message type as either
Message.Type.SHORT
or
Message
.Type.LONG
as implemented in Listing 6-26 and Listing 6-27 respectively. The
@Message
qualii er is
implemented in the same manner, as can be seen is Listing 6-28, as the qualii er used in the Custom
Annotation Type example shown earlier.
LISTING 6‐28:
Custom message annotation
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
public @interface
Message
{
Type value();
enum Type{
SHORT
,
LONG
}
}
To create the annotation literal that you use to make the comparison between the type you want
and the type of the dependency, you extend the abstract class
AnnotationLiteral
and implement
Message
as the custom message qualii er. Listing 6-29 shows how this is done.
LISTING 6‐29:
Annotation literal used to retrieve required message type
public class MessageLiteral extends AnnotationLiteral<Message> implements Message {
private static final long serialVersionUID = 1L;
private Type type;
public MessageLiteral(Type type) {
this.type = type;