Skip to content

Commit 961e944

Browse files
liachAlex Buckley
andcommitted
8336754: Remodel TypeAnnotation to "has" instead of "be" an Annotation
Co-authored-by: Alex Buckley <[email protected]> Reviewed-by: asotona
1 parent 07352c6 commit 961e944

File tree

20 files changed

+260
-243
lines changed

20 files changed

+260
-243
lines changed

src/java.base/share/classes/java/lang/classfile/Annotation.java

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,43 +37,60 @@
3737
import jdk.internal.javac.PreviewFeature;
3838

3939
/**
40-
* Models an annotation on a declaration.
40+
* Models an {@code annotation} structure (JVMS {@jvms 4.7.16}) or part of a {@code
41+
* type_annotation} structure (JVMS {@jvms 4.7.20}). This model indicates the
42+
* interface of the annotation and a set of element-value pairs.
43+
* <p>
44+
* This model can reconstruct an annotation, given the location of the modeled structure
45+
* in the class file and the definition of the annotation interface.
46+
* <p>
47+
* Two {@code Annotation} objects should be compared using the {@link
48+
* Object#equals(Object) equals} method.
49+
*
50+
* @apiNote
51+
* For Java programs, the location of the modeled structure indicates the source code
52+
* element or type (JLS {@jls 9.7.4}) on which the reconstructed annotation appears,
53+
* and the annotation interface definition determines whether the reconstructed annotation has
54+
* elements with default values (JLS {@jls 9.6.2}), and whether the reconstructed annotation
55+
* is a container annotation for multiple annotations (JLS {@jls 9.7.5}).
4156
*
4257
* @see AnnotationElement
4358
* @see AnnotationValue
59+
* @see TypeAnnotation
4460
* @see RuntimeVisibleAnnotationsAttribute
4561
* @see RuntimeInvisibleAnnotationsAttribute
4662
* @see RuntimeVisibleParameterAnnotationsAttribute
4763
* @see RuntimeInvisibleParameterAnnotationsAttribute
4864
*
49-
* @sealedGraph
5065
* @since 22
5166
*/
5267
@PreviewFeature(feature = PreviewFeature.Feature.CLASSFILE_API)
5368
public sealed interface Annotation
54-
permits TypeAnnotation, AnnotationImpl {
69+
permits AnnotationImpl {
5570

5671
/**
57-
* {@return the class of the annotation}
72+
* {@return the constant pool entry holding the {@linkplain Class#descriptorString
73+
* descriptor string} of the annotation interface}
5874
*/
5975
Utf8Entry className();
6076

6177
/**
62-
* {@return the class of the annotation, as a symbolic descriptor}
78+
* {@return the annotation interface, as a symbolic descriptor}
6379
*/
6480
default ClassDesc classSymbol() {
6581
return ClassDesc.ofDescriptor(className().stringValue());
6682
}
6783

6884
/**
69-
* {@return the elements of the annotation}
85+
* {@return the element-value pairs of the annotation}
7086
*/
7187
List<AnnotationElement> elements();
7288

7389
/**
7490
* {@return an annotation}
75-
* @param annotationClass the class of the annotation
76-
* @param elements the elements of the annotation
91+
* @param annotationClass the constant pool entry holding the descriptor string
92+
* of the annotation interface
93+
* @param elements the element-value pairs of the annotation
7794
*/
7895
static Annotation of(Utf8Entry annotationClass,
7996
List<AnnotationElement> elements) {
@@ -82,8 +99,9 @@ static Annotation of(Utf8Entry annotationClass,
8299

83100
/**
84101
* {@return an annotation}
85-
* @param annotationClass the class of the annotation
86-
* @param elements the elements of the annotation
102+
* @param annotationClass the constant pool entry holding the descriptor string
103+
* of the annotation interface
104+
* @param elements the element-value pairs of the annotation
87105
*/
88106
static Annotation of(Utf8Entry annotationClass,
89107
AnnotationElement... elements) {
@@ -92,8 +110,8 @@ static Annotation of(Utf8Entry annotationClass,
92110

93111
/**
94112
* {@return an annotation}
95-
* @param annotationClass the class of the annotation
96-
* @param elements the elements of the annotation
113+
* @param annotationClass the descriptor of the annotation interface
114+
* @param elements the element-value pairs of the annotation
97115
*/
98116
static Annotation of(ClassDesc annotationClass,
99117
List<AnnotationElement> elements) {
@@ -102,8 +120,8 @@ static Annotation of(ClassDesc annotationClass,
102120

103121
/**
104122
* {@return an annotation}
105-
* @param annotationClass the class of the annotation
106-
* @param elements the elements of the annotation
123+
* @param annotationClass the descriptor of the annotation interface
124+
* @param elements the element-value pairs of the annotation
107125
*/
108126
static Annotation of(ClassDesc annotationClass,
109127
AnnotationElement... elements) {

src/java.base/share/classes/java/lang/classfile/AnnotationElement.java

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@
3232
import jdk.internal.javac.PreviewFeature;
3333

3434
/**
35-
* Models a key-value pair of an annotation.
35+
* Models an element-value pair in the {@code element_value_pairs}
36+
* table in the {@code annotation} structure defined in JVMS
37+
* {@jvms 4.7.16} or the {@code type_annotation} structure defined
38+
* in JVMS {@jvms 4.7.20}.
39+
* <p>
40+
* Two {@code AnnotationElement} objects should be compared using the
41+
* {@link Object#equals(Object) equals} method.
3642
*
3743
* @see Annotation
3844
* @see AnnotationValue
@@ -45,6 +51,12 @@ public sealed interface AnnotationElement
4551

4652
/**
4753
* {@return the element name}
54+
*
55+
* @apiNote
56+
* In Java source code, by convention, the name of the sole element in a
57+
* single-element annotation interface is {@code value}. (JLS {@jls 9.6.1})
58+
* This is the case for single-element annotations (JLS {@jls 9.7.3}) and
59+
* container annotations for multiple annotations (JLS {@jls 9.6.3}).
4860
*/
4961
Utf8Entry name();
5062

@@ -54,7 +66,7 @@ public sealed interface AnnotationElement
5466
AnnotationValue value();
5567

5668
/**
57-
* {@return an annotation key-value pair}
69+
* {@return an element-value pair}
5870
* @param name the name of the key
5971
* @param value the associated value
6072
*/
@@ -64,7 +76,7 @@ static AnnotationElement of(Utf8Entry name,
6476
}
6577

6678
/**
67-
* {@return an annotation key-value pair}
79+
* {@return an element-value pair}
6880
* @param name the name of the key
6981
* @param value the associated value
7082
*/
@@ -74,119 +86,131 @@ static AnnotationElement of(String name,
7486
}
7587

7688
/**
77-
* {@return an annotation key-value pair for a class-valued annotation}
89+
* {@return an element-value pair for a class-valued element}
7890
* @param name the name of the key
7991
* @param value the associated value
92+
* @see AnnotationValue#ofClass(ClassDesc) AnnotationValue::ofClass
8093
*/
8194
static AnnotationElement ofClass(String name,
8295
ClassDesc value) {
8396
return of(name, AnnotationValue.ofClass(value));
8497
}
8598

8699
/**
87-
* {@return an annotation key-value pair for a string-valued annotation}
100+
* {@return an element-value pair for a string-valued element}
88101
* @param name the name of the key
89102
* @param value the associated value
103+
* @see AnnotationValue#ofString(String) AnnotationValue::ofString
90104
*/
91105
static AnnotationElement ofString(String name,
92106
String value) {
93107
return of(name, AnnotationValue.ofString(value));
94108
}
95109

96110
/**
97-
* {@return an annotation key-value pair for a long-valued annotation}
111+
* {@return an element-value pair for a long-valued element}
98112
* @param name the name of the key
99113
* @param value the associated value
114+
* @see AnnotationValue#ofLong(long) AnnotationValue::ofLong
100115
*/
101116
static AnnotationElement ofLong(String name,
102117
long value) {
103118
return of(name, AnnotationValue.ofLong(value));
104119
}
105120

106121
/**
107-
* {@return an annotation key-value pair for an int-valued annotation}
122+
* {@return an element-value pair for an int-valued element}
108123
* @param name the name of the key
109124
* @param value the associated value
125+
* @see AnnotationValue#ofInt(int) AnnotationValue::ofInt
110126
*/
111127
static AnnotationElement ofInt(String name,
112128
int value) {
113129
return of(name, AnnotationValue.ofInt(value));
114130
}
115131

116132
/**
117-
* {@return an annotation key-value pair for a char-valued annotation}
133+
* {@return an element-value pair for a char-valued element}
118134
* @param name the name of the key
119135
* @param value the associated value
136+
* @see AnnotationValue#ofChar(char) AnnotationValue::ofChar
120137
*/
121138
static AnnotationElement ofChar(String name,
122139
char value) {
123140
return of(name, AnnotationValue.ofChar(value));
124141
}
125142

126143
/**
127-
* {@return an annotation key-value pair for a short-valued annotation}
144+
* {@return an element-value pair for a short-valued element}
128145
* @param name the name of the key
129146
* @param value the associated value
147+
* @see AnnotationValue#ofShort(short) AnnotationValue::ofShort
130148
*/
131149
static AnnotationElement ofShort(String name,
132150
short value) {
133151
return of(name, AnnotationValue.ofShort(value));
134152
}
135153

136154
/**
137-
* {@return an annotation key-value pair for a byte-valued annotation}
155+
* {@return an element-value pair for a byte-valued element}
138156
* @param name the name of the key
139157
* @param value the associated value
158+
* @see AnnotationValue#ofByte(byte) AnnotationValue::ofByte
140159
*/
141160
static AnnotationElement ofByte(String name,
142-
byte value) {
161+
byte value) {
143162
return of(name, AnnotationValue.ofByte(value));
144163
}
145164

146165
/**
147-
* {@return an annotation key-value pair for a boolean-valued annotation}
166+
* {@return an element-value pair for a boolean-valued element}
148167
* @param name the name of the key
149168
* @param value the associated value
169+
* @see AnnotationValue#ofBoolean(boolean) AnnotationValue::ofBoolean
150170
*/
151171
static AnnotationElement ofBoolean(String name,
152-
boolean value) {
172+
boolean value) {
153173
return of(name, AnnotationValue.ofBoolean(value));
154174
}
155175

156176
/**
157-
* {@return an annotation key-value pair for a double-valued annotation}
177+
* {@return an element-value pair for a double-valued element}
158178
* @param name the name of the key
159179
* @param value the associated value
180+
* @see AnnotationValue#ofDouble(double) AnnotationValue::ofDouble
160181
*/
161182
static AnnotationElement ofDouble(String name,
162183
double value) {
163184
return of(name, AnnotationValue.ofDouble(value));
164185
}
165186

166187
/**
167-
* {@return an annotation key-value pair for a float-valued annotation}
188+
* {@return an element-value pair for a float-valued element}
168189
* @param name the name of the key
169190
* @param value the associated value
191+
* @see AnnotationValue#ofFloat(float) AnnotationValue::ofFloat
170192
*/
171193
static AnnotationElement ofFloat(String name,
172194
float value) {
173195
return of(name, AnnotationValue.ofFloat(value));
174196
}
175197

176198
/**
177-
* {@return an annotation key-value pair for an annotation-valued annotation}
199+
* {@return an element-value pair for an annotation-valued element}
178200
* @param name the name of the key
179201
* @param value the associated value
202+
* @see AnnotationValue#ofAnnotation AnnotationValue::ofAnnotation
180203
*/
181204
static AnnotationElement ofAnnotation(String name,
182205
Annotation value) {
183206
return of(name, AnnotationValue.ofAnnotation(value));
184207
}
185208

186209
/**
187-
* {@return an annotation key-value pair for an array-valued annotation}
210+
* {@return an element-value pair for an array-valued element}
188211
* @param name the name of the key
189212
* @param values the associated values
213+
* @see AnnotationValue#ofArray(AnnotationValue...) AnnotationValue::ofArray
190214
*/
191215
static AnnotationElement ofArray(String name,
192216
AnnotationValue... values) {

0 commit comments

Comments
 (0)