Target
Stay organized with collections
Save and categorize content based on your preferences.
Indicates the contexts in which an annotation type is applicable. The
declaration contexts and type contexts in which an annotation type may be
applicable are specified in JLS 9.6.4.1, and denoted in source code by enum
constants of java.lang.annotation.ElementType
.
If an @Target
meta-annotation is not present on an annotation type
T
, then an annotation of type T
may be written as a
modifier for any declaration except a type parameter declaration.
If an @Target
meta-annotation is present, the compiler will enforce
the usage restrictions indicated by ElementType
enum constants, in line with JLS 9.7.4.
For example, this @Target
meta-annotation indicates that the
declared type is itself a meta-annotation type. It can only be used on
annotation type declarations:
@Target(ElementType.ANNOTATION_TYPE)
public @interface MetaAnnotationType {
...
}
This @Target
meta-annotation indicates that the declared type is
intended solely for use as a member type in complex annotation type
declarations. It cannot be used to annotate anything directly:
@Target({})
public @interface MemberType {
...
}
It is a compile-time error for a single ElementType
constant to
appear more than once in an @Target
annotation. For example, the
following @Target
meta-annotation is illegal:
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD})
public @interface Bogus {
...
}
Public Method Summary
ElementType[]
|
value()
Returns an array of the kinds of elements an annotation type
can be applied to.
|
Inherited Method Summary
From interface
java.lang.annotation.Annotation
abstract
Class<? extends Annotation>
|
|
abstract
boolean
|
equals( Object obj)
Returns true if the specified object represents an annotation
that is logically equivalent to this one.
|
abstract
int
|
hashCode()
Returns the hash code of this annotation, as defined below:
The hash code of an annotation is the sum of the hash codes
of its members (including those with default values), as defined
below:
The hash code of an annotation member is (127 times the hash code
of the member-name as computed by String.hashCode() ) XOR
the hash code of the member-value, as defined below:
The hash code of a member-value depends on its type:
|
abstract
String
|
toString()
Returns a string representation of this annotation.
|
Public Methods
Returns an array of the kinds of elements an annotation type
can be applied to.
Returns
- an array of the kinds of elements an annotation type
can be applied to
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["The `@Target` meta-annotation specifies the contexts (classes, methods, fields, etc.) where an annotation type can be used."],["If `@Target` is not present, the annotation can be applied to any declaration except type parameters."],["The `value` method within `@Target` returns an array of `ElementType` enum constants, defining the applicable contexts."],["When present, the compiler enforces the usage restrictions defined by the `ElementType` constants within the `@Target` annotation."]]],[]]