Java Reference
In-Depth Information
The Inherited Annotation Type
The
Inherited
annotation type is a marker meta-annotation type. If an annotation type is annotated with an
Inherited
meta-annotation, its instances are inherited by a subclass declaration. It has no effect if an annotation
type is used to annotate any program elements other than a class declaration. Let's consider two annotation type
declarations:
Ann2
and
Ann3
. Note that
Ann2
is not annotated with an
Inherited
meta-annotation, whereas
Ann3
is
annotated with an
Inherited
meta-annotation.
public @interface Ann2 {
int id();
}
@Inherited
public @interface Ann3 {
int id();
}
Let's declare two classes,
A
and
B,
as follows. Note that class
B
inherits class
A
.
@Ann2(id=505)
@Ann3(id=707)
public class A {
// Code for class A goes here
}
// Class B inherits Ann3(id=707) annotation from the class A
public class B extends A {
// Code for class B goes here
}
In the above snippet of code, class
B
inherits the
@Ann3(id=707)
annotation from class
A
because the
Ann3
annotation type has been annotated with an
Inherited
meta-annotation. Class
B
does not inherit the
@Ann2(id=505)
annotation because the
Ann2
annotation type is not annotated with an
Inherited
meta-annotation.
The Documented Annotation
The
Documented
annotation type is a marker meta-annotation type. If an annotation type is annotated with a
Documented
annotation, the Javadoc tool will generate documentation for all of its instances. Listing 1-13 has the code
for the final version of the
Version
annotation type, which has been annotated with a
Documented
meta-annotation.
Listing 1-13.
The Final Version of the Version Annotation Type
// Version.java
package com.jdojo.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;