Java Programming/Annotations/Meta-Annotations


There are five annotation types in the java.lang.annotation package called meta-annotations. These annotation types are used to annotate other annotation types.

Documented

If a member is annotated with a type itself marked as @Documented, then that member will be documented as annotating that type.

Computer code Code listing 1.1: Use of @Documented
@interface Secret { }

@Documented
@interface NotSecret { }

@Secret
@NotSecret
public class Example {
}

In the documentation for the Example class, such as the JavaDoc, Example will be shown as annotated with @NotSecret, but not @Secret.

Clipboard

To do:
Add the render of a Javadoc.

Inherited

Exactly as the name sounds, an @Inherited annotation type is inherited by subclasses of an annotated type.

Computer code Code listing 1.2: Use of @Inherited
@Inherited
@interface ForEveryone { }

@interface JustForMe { }

@ForEveryone
@JustForMe
class Superclass { }

class Subclass extends Superclass { }

In this example, Superclass has been explicitly annotated with both @ForEveryone and @JustForMe. Subclass hasn't been explicitly marked with either one; however, it inherits @ForEveryone because the latter is annotated with @Inherited. @JustForMe isn't annotated, so it isn't inherited by Subclass.

Repeatable

A @Repeatable annotation type is repeatable - i.e. can be specified multiple times on the same class. This meta-annotation was added in Java 8.

Retention

Different annotation types have different purposes. Some are intended for use with the compiler; others are meant to be reflected dynamically at runtime. There's no reason for a compiler annotation to be available at runtime, so the @Retention meta-annotation specifies how long an annotation type should be retained. The value attribute is one of the java.lang.annotation.RetentionPolicy enum constants. The possible values, in order from shortest to longest retention, are as follows:

RetentionPolicy.SOURCE
The annotation will not be included in the class file. This is useful for annotations which are intended for the compiler only.
RetentionPolicy.CLASS
The annotation will be included in the class file, but cannot be read reflectively.
RetentionPolicy.RUNTIME
The annotation can be reflected at runtime.

If no @Retention policy is specified, it defaults to RetentionPolicy.CLASS.

Target

The @Target meta-annotation determines what may be marked by the annotation. The value attribute is one or more of the java.lang.annotation.ElementType enum constants. Those constants are ElementType.ANNOTATION_TYPE, CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, and TYPE.

If @Target is not specified, the annotation may be used on any program element.


Clipboard

To do:
Add some exercises like the ones in Variables


  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.

Java_Programming/Annotations/Meta-Annotations
 



 

Connect with defaultLogic
What We've Done
Led Digital Marketing Efforts of Top 500 e-Retailers.
Worked with Top Brands at Leading Agencies.
Successfully Managed Over $50 million in Digital Ad Spend.
Developed Strategies and Processes that Enabled Brands to Grow During an Economic Downturn.
Taught Advanced Internet Marketing Strategies at the graduate level.


Manage research, learning and skills at defaultLogic. Create an account using LinkedIn or facebook to manage and organize your Digital Marketing and Technology knowledge. defaultLogic works like a shopping cart for information -- helping you to save, discuss and share.

Visit defaultLogic's partner sites below:
PopFlock.com : Music Genres | Musicians | Musical Instruments | Music Industry
NCR Works : Retail Banking | Restaurant Industry | Retail Industry | Hospitality Industry

  Contact Us