1 /*
2  * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package java.lang.annotation;
27 
28 /**
29  * The common interface extended by all annotation interfaces.  Note that an
30  * interface that manually extends this one does <i>not</i> define
31  * an annotation interface.  Also note that this interface does not itself
32  * define an annotation interface.
33  *
34  * More information about annotation interfaces can be found in section
35  * {@jls 9.6} of <cite>The Java Language Specification</cite>.
36  *
37  * The {@link java.lang.reflect.AnnotatedElement} interface discusses
38  * compatibility concerns when evolving an annotation interface from being
39  * non-repeatable to being repeatable.
40  *
41  * @author  Josh Bloch
42  * @since   1.5
43  */
44 public interface Annotation {
45     /**
46      * Returns true if the specified object represents an annotation
47      * that is logically equivalent to this one.  In other words,
48      * returns true if the specified object is an instance of the same
49      * annotation interface as this instance, all of whose members are equal
50      * to the corresponding member of this annotation, as defined below:
51      * <ul>
52      *    <li>Two corresponding primitive typed members whose values are
53      *    {@code x} and {@code y} are considered equal if {@code x == y},
54      *    unless their type is {@code float} or {@code double}.
55      *
56      *    <li>Two corresponding {@code float} members whose values
57      *    are {@code x} and {@code y} are considered equal if
58      *    {@code Float.valueOf(x).equals(Float.valueOf(y))}.
59      *    (Unlike the {@code ==} operator, NaN is considered equal
60      *    to itself, and {@code 0.0f} unequal to {@code -0.0f}.)
61      *
62      *    <li>Two corresponding {@code double} members whose values
63      *    are {@code x} and {@code y} are considered equal if
64      *    {@code Double.valueOf(x).equals(Double.valueOf(y))}.
65      *    (Unlike the {@code ==} operator, NaN is considered equal
66      *    to itself, and {@code 0.0} unequal to {@code -0.0}.)
67      *
68      *    <li>Two corresponding {@code String}, {@code Class}, enum, or
69      *    annotation typed members whose values are {@code x} and {@code y}
70      *    are considered equal if {@code x.equals(y)}.  (Note that this
71      *    definition is recursive for annotation typed members.)
72      *
73      *    <li>Two corresponding array typed members {@code x} and {@code y}
74      *    are considered equal if {@code Arrays.equals(x, y)}, for the
75      *    appropriate overloading of {@link java.util.Arrays#equals Arrays.equals}.
76      * </ul>
77      *
78      * @return true if the specified object represents an annotation
79      *     that is logically equivalent to this one, otherwise false
80      */
equals(Object obj)81     boolean equals(Object obj);
82 
83     /**
84      * Returns the hash code of this annotation.
85      *
86      * <p>The hash code of an annotation is the sum of the hash codes
87      * of its members (including those with default values).
88      *
89      * The hash code of an annotation member is (127 times the hash code
90      * of the member-name as computed by {@link String#hashCode()}) XOR
91      * the hash code of the member-value.
92      * The hash code of a member-value depends on its type as defined below:
93      * <ul>
94      * <li>The hash code of a primitive value <i>{@code v}</i> is equal to
95      *     <code><i>WrapperType</i>.valueOf(<i>v</i>).hashCode()</code>, where
96      *     <i>{@code WrapperType}</i> is the wrapper type corresponding
97      *     to the primitive type of <i>{@code v}</i> ({@link Byte},
98      *     {@link Character}, {@link Double}, {@link Float}, {@link Integer},
99      *     {@link Long}, {@link Short}, or {@link Boolean}).
100      *
101      * <li>The hash code of a string, enum, class, or annotation member-value
102      *     <i>{@code v}</i> is computed as by calling
103      *     <code><i>v</i>.hashCode()</code>.  (In the case of annotation
104      *     member values, this is a recursive definition.)
105      *
106      * <li>The hash code of an array member-value is computed by calling
107      *     the appropriate overloading of
108      *     {@link java.util.Arrays#hashCode(long[]) Arrays.hashCode}
109      *     on the value.  (There is one overloading for each primitive
110      *     type, and one for object reference types.)
111      * </ul>
112      *
113      * @return the hash code of this annotation
114      */
hashCode()115     int hashCode();
116 
117     /**
118      * Returns a string representation of this annotation.  The details
119      * of the representation are implementation-dependent, but the following
120      * may be regarded as typical:
121      * <pre>
122      *   &#064;com.example.Name(first="Duke", middle="of", last="Java")
123      * </pre>
124      *
125      * @return a string representation of this annotation
126      */
toString()127     String toString();
128 
129     /**
130      * Returns the annotation interface of this annotation.
131      *
132      * @apiNote Implementation-dependent classes are used to provide
133      * the implementations of annotations. Therefore, calling {@link
134      * Object#getClass getClass} on an annotation will return an
135      * implementation-dependent class. In contrast, this method will
136      * reliably return the annotation interface of the annotation.
137      *
138      * @return the annotation interface of this annotation
139      * @see Enum#getDeclaringClass
140      */
annotationType()141     Class<? extends Annotation> annotationType();
142 }
143