Java Reference
In-Depth Information
If an annotation
a
corresponds to a type
T
, and
T
has a (meta-)annotation
m
that corresponds
to
java.lang.annotation.Retention
, then:
• If
m
has an element whose value is
java.lang.annotation.RetentionPolicy.SOURCE
, then a
Java compiler must ensure that
a
is not present in the binary representation of the
class or interface in which
a
appears.
• If
m
has an element whose value is
java.lang.annotation.RetentionPolicy.CLASS
or
java.lang.annotation.RetentionPolicy.RUNTIME
, then a Java compiler must ensure that
a
is represented in the binary representation of the class or interface in which
a
ap-
pears, unless
m
annotates a local variable declaration.
An annotation on a local variable declaration is never retained in the binary repres-
entation.
In addition, if
m
has an element whose value is
java.lang.annotation.RetentionPolicy.RUNTIME
, the reflection libraries of the Java SE
platform must make
a
available at run time.
If
T
does not have a (meta-)annotation
m
that corresponds to
java.lang.annotation.Retention
,
then a Java compiler must treat
T
as if it does have such a meta-annotation
m
with an ele-
ment whose value is
java.lang.annotation.RetentionPolicy.CLASS
.
9.6.3.3. @Inherited
The annotation type
java.lang.annotation.Inherited
is used to indicate that annotations on a class
C
corresponding to a given annotation type are inherited by subclasses of
C
.
9.6.3.4. @Override
Programmers occasionally overload a method declaration when they mean to override it,
leading to subtle problems. The annotation type
Override
supports early detection of such
problems.
The classic example concerns the
equals
method. Programmers write the following in
class
Foo
:
public boolean equals(Foo that) { ... }
when they mean to write:
public boolean equals(Object that) { ... }
This is perfectly legal, but class
Foo
inherits the
equals
implementation from
Object
,
which can cause some very subtle bugs.