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."],[[["\u003cp\u003eThe \u003ccode\u003e@Target\u003c/code\u003e meta-annotation specifies the contexts (classes, methods, fields, etc.) where an annotation type can be used.\u003c/p\u003e\n"],["\u003cp\u003eIf \u003ccode\u003e@Target\u003c/code\u003e is not present, the annotation can be applied to any declaration except type parameters.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003evalue\u003c/code\u003e method within \u003ccode\u003e@Target\u003c/code\u003e returns an array of \u003ccode\u003eElementType\u003c/code\u003e enum constants, defining the applicable contexts.\u003c/p\u003e\n"],["\u003cp\u003eWhen present, the compiler enforces the usage restrictions defined by the \u003ccode\u003eElementType\u003c/code\u003e constants within the \u003ccode\u003e@Target\u003c/code\u003e annotation.\u003c/p\u003e\n"]]],[],null,["public abstract @interface **Target** implements [Annotation](../../../../reference/java/lang/annotation/Annotation.html) \nIndicates the contexts in which an annotation type is applicable. The\ndeclaration contexts and type contexts in which an annotation type may be\napplicable are specified in JLS 9.6.4.1, and denoted in source code by enum\nconstants of [java.lang.annotation.ElementType](../../../../reference/java/lang/annotation/ElementType.html).\n\nIf an `@Target` meta-annotation is not present on an annotation type\n`T` , then an annotation of type `T` may be written as a\nmodifier for any declaration except a type parameter declaration.\n\nIf an `@Target` meta-annotation is present, the compiler will enforce\nthe usage restrictions indicated by `ElementType`\nenum constants, in line with JLS 9.7.4.\n\nFor example, this `@Target` meta-annotation indicates that the\ndeclared type is itself a meta-annotation type. It can only be used on\nannotation type declarations: \n\n```\n @Target(ElementType.ANNOTATION_TYPE)\n public @interface MetaAnnotationType {\n ...\n }\n \n```\n\nThis `@Target` meta-annotation indicates that the declared type is\nintended solely for use as a member type in complex annotation type\ndeclarations. It cannot be used to annotate anything directly: \n\n```\n @Target({})\n public @interface MemberType {\n ...\n }\n \n```\n\nIt is a compile-time error for a single `ElementType` constant to\nappear more than once in an `@Target` annotation. For example, the\nfollowing `@Target` meta-annotation is illegal: \n\n```\n @Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD})\n public @interface Bogus {\n ...\n }\n \n```\n\n\u003cbr /\u003e\n\nPublic Method Summary\n\n|--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [ElementType\\[\\]](../../../../reference/java/lang/annotation/ElementType.html) | [value](../../../../reference/java/lang/annotation/Target.html#value())() Returns an array of the kinds of elements an annotation type can be applied to. |\n\nInherited Method Summary \nFrom interface [java.lang.annotation.Annotation](../../../../reference/java/lang/annotation/Annotation.html) \n\n|----------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract [Class](../../../../reference/java/lang/Class.html)\\\u003c? extends [Annotation](../../../../reference/java/lang/annotation/Annotation.html)\\\u003e | [annotationType](../../../../reference/java/lang/annotation/Annotation.html#annotationType())() Returns the annotation type of this annotation. |\n| abstract boolean | [equals](../../../../reference/java/lang/annotation/Annotation.html#equals(java.lang.Object))([Object](../../../../reference/java/lang/Object.html) obj) Returns true if the specified object represents an annotation that is logically equivalent to this one. |\n| abstract int | [hashCode](../../../../reference/java/lang/annotation/Annotation.html#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()](../../../../reference/java/lang/String.html#hashCode())) XOR the hash code of the member-value, as defined below: The hash code of a member-value depends on its type: - The hash code of a primitive value *v* is equal to *WrapperType* .valueOf(*v*).hashCode(), where *WrapperType* is the wrapper type corresponding to the primitive type of *v* ([Byte](../../../../reference/java/lang/Byte.html), [Character](../../../../reference/java/lang/Character.html), [Double](../../../../reference/java/lang/Double.html), [Float](../../../../reference/java/lang/Float.html), [Integer](../../../../reference/java/lang/Integer.html), [Long](../../../../reference/java/lang/Long.html), [Short](../../../../reference/java/lang/Short.html), or [Boolean](../../../../reference/java/lang/Boolean.html)). |\n| abstract [String](../../../../reference/java/lang/String.html) | [toString](../../../../reference/java/lang/annotation/Annotation.html#toString())() Returns a string representation of this annotation. |\n\nPublic Methods \n\npublic [ElementType\\[\\]](../../../../reference/java/lang/annotation/ElementType.html)\n**value**\n() \nReturns an array of the kinds of elements an annotation type\ncan be applied to. \n\nReturns\n\n- an array of the kinds of elements an annotation type can be applied to"]]