Java Annotations with No Target

Perhaps you would expect every Java annotation to have one or multiple targets, indicating where it can be applied, either on a class, method, field, or some combination. It's correct for the most part, but it also turns out some java annotations have no target. Their target() field is an empty array. For example,

  • javax.ejb.ActivationConfigProperty:

    public @interface ActivationConfigProperty
  • javax.persistence.EntityResult

    public @interface EntityResult
Where can we use these targetless annotations? They are to be used as nested annotations inside other enclosing annotations. Maybe we need a new target value NESTED? For example, @ActivationConfigProperty can only be used inside @javax.ejb.MessageDriven to specify properties for the Message-Driven Bean (MDB):
activationConfig = {
propertyValue="name='java' AND bugs < 1")
public class MessageBean implements MessageListener {
@javax.persistence.EntityResult can only be used as nested annotation inside @javax.persistence.SqlResultSetMapping. Here is a more complex example from its java doc, with 3 nested annotations at 3 different levels:
@EntityResult(entityClass=com.acme.Order.class, fields={
@FieldResult(name="id", column="order_id"),
@FieldResult(name="quantity", column="order_quantity"),
@FieldResult(name="item", column="order_item")})},


Anna said...

Great and Useful Article.

Online Java Training

Java Online Training India

Java Online Course

Java EE course

Java EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Course in Chennai

Java Online Training India

Tessa said...

Your work is time-consuming but you know that it is well done!

eMexoT said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

Internet Of Things training in electronic city