/
ValueDescriptor.java
321 lines (295 loc) · 10.6 KB
/
ValueDescriptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.jfr;
import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import jdk.jfr.internal.AnnotationConstruct;
import jdk.jfr.internal.Type;
import jdk.jfr.internal.Utils;
/**
* Describes the event fields and annotation elements.
*
* @since 9
*/
public final class ValueDescriptor {
private final AnnotationConstruct annotationConstruct;
private final Type type;
private final String name;
private final boolean isArray;
private final boolean constantPool;
private final String javaFieldName;
// package private, invoked by jdk.internal.
ValueDescriptor(Type type, String name, List<AnnotationElement> annotations, int dimension, boolean constantPool, String fieldName) {
Objects.requireNonNull(annotations);
if (dimension < 0) {
throw new IllegalArgumentException("Dimension must be positive");
}
this.name = Objects.requireNonNull(name, "Name of value descriptor can't be null");
this.type = Objects.requireNonNull(type);
this.isArray = dimension > 0;
this.constantPool = constantPool;
this.annotationConstruct = new AnnotationConstruct(annotations);
this.javaFieldName = fieldName;
}
/**
* <p>
* Constructs a value descriptor, useful for dynamically creating event types and
* annotations.
* <P>
* The following types are supported:
* <ul>
* <li>{@code byte.class}
* <li>{@code short.class}
* <li>{@code int.class}
* <li>{@code long.class}
* <li>{@code char.class}
* <li>{@code float.class}
* <li>{@code double.class}
* <li>{@code boolean.class}
* <li>{@code String.class}
* <li>{@code Class.class}
* <li>{@code Thread.class}
* </ul>
*
* <p>
* The name must be a valid Java identifier (for example, {@code "maxThroughput"}). See 3.8
* Java Language Specification for more information.
*
* @param type the type, not {@code null}
* @param name the name, not {@code null}
*
* @throws SecurityException if a security manager is present and the caller
* doesn't have {@code FlightRecorderPermission("registerEvent")}
*
*/
public ValueDescriptor(Class<?> type, String name) {
this(type, name, Collections.<AnnotationElement> emptyList());
}
/**
* <p>
* Constructs a value descriptor, useful for dynamically creating event types and
* annotations.
* <P>
* The following types are supported:
* <ul>
* <li>{@code byte.class}
* <li>{@code short.class}
* <li>{@code int.class}
* <li>{@code long.class}
* <li>{@code char.class}
* <li>{@code float.class}
* <li>{@code double.class}
* <li>{@code boolean.class}
* <li>{@code String.class}
* <li>{@code Class.class}
* <li>{@code Thread.class}
* </ul>
*
* <p>
* The name must be a valid Java identifier (for example, {@code "maxThroughput"}). See 3.8
* Java Language Specification for more information.
*
* @param type the type, not {@code null}
* @param name the name, not {@code null}
* @param annotations the annotations on the value descriptors, not
* {@code null}
*
* @throws SecurityException if a security manager is present and the caller
* doesn't have {@code FlightRecorderPermission("registerEvent")}
*/
public ValueDescriptor(Class<?> type, String name, List<AnnotationElement> annotations) {
this(type, name, List.copyOf(annotations), false);
}
ValueDescriptor(Class<?> type, String name, List<AnnotationElement> annotations, boolean allowArray) {
Objects.requireNonNull(annotations);
Utils.checkRegisterPermission();
if (!allowArray) {
if (type.isArray()) {
throw new IllegalArgumentException("Array types are not allowed");
}
}
this.name = Objects.requireNonNull(name, "Name of value descriptor can't be null");
this.type = Objects.requireNonNull(Utils.getValidType(Objects.requireNonNull(type), Objects.requireNonNull(name)));
this.annotationConstruct = new AnnotationConstruct(annotations);
this.javaFieldName = name; // Needed for dynamic events
this.isArray = type.isArray();
// Assume we always want to store String and Thread in constant pool
this.constantPool = type == Class.class || type == Thread.class;
}
/**
* Returns a human-readable name that describes the value (for example,
* {@code "Maximum Throughput"}).
*
* @return a human-readable name, or {@code null} if doesn't exist
*/
public String getLabel() {
return annotationConstruct.getLabel();
}
/**
* Returns the name of the value (for example, {@code "maxThroughput"}).
*
* @return the name, not {@code null}
*/
public String getName() {
return name;
}
/**
* Returns a sentence describing the value (for example, {@code "Maximum
* throughput in the transaction system. Value is reset after each new
* batch."}).
*
* @return the description, or {@code null} if doesn't exist
*/
public String getDescription() {
return annotationConstruct.getDescription();
}
/**
* Returns a textual identifier that specifies how a value represented by
* this {@link ValueDescriptor} is interpreted or formatted.
* <p>
* For example, if the value descriptor's type is {@code float} and the
* event value is {@code 0.5f}, a content type of
* {@code "jdk.jfr.Percentage"} hints to a client that the value is a
* percentage and that it should be rendered as {@code "50%"}.
* <p>
* The JDK provides the following predefined content types:
* <ul>
* <li>jdk.jfr.Percentage</li>
* <li>jdk.jfr.Timespan</li>
* <li>jdk.jfr.Timestamp</li>
* <li>jdk.jfr.Frequency</li>
* <li>jdk.jfr.Flag</li>
* <li>jdk.jfr.MemoryAddress</li>
* <li>jdk.jfr.DataAmount</li>
* <li>jdk.jfr.NetworkAddress</li>
* </ul>
* <p>
* User-defined content types can be created by using the {@link ContentType} class.
*
* @return the content type, or {@code null} if doesn't exist
*
* @see ContentType
*/
public String getContentType() {
for (AnnotationElement anno : getAnnotationElements()) {
for (AnnotationElement meta : anno.getAnnotationElements()) {
if (meta.getTypeName().equals(ContentType.class.getName())) {
return anno.getTypeName();
}
}
}
return null;
}
/**
* Returns the fully qualified class name of the type that is associated with
* this value descriptor.
*
* @return the type name, not {@code null}
*
* @see ValueDescriptor#getTypeId()
*/
public String getTypeName() {
if (type.isSimpleType()) {
return type.getFields().get(0).getTypeName();
}
return type.getName();
}
/**
* Returns a unique ID for the type in the Java virtual Machine (JVM).
*
* The ID might not be the same between JVM instances.
*
* @return the type ID, not negative
*/
public long getTypeId() {
return type.getId();
}
/**
* Returns if this value descriptor is an array type.
*
* @return {@code true} if it is an array type, {@code false} otherwise
*/
public boolean isArray() {
return isArray;
}
/**
* Returns the first annotation for the specified type if an annotation
* element with the same name is directly present for this value descriptor,
* {@code null} otherwise.
*
* @param <A> the type of the annotation to query for and return if present
* @param annotationType the Class object that corresponds to the annotation
* type, not {@code null}
* @return this element's annotation for the specified annotation type if
* directly present, else {@code null}
*/
public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
Objects.requireNonNull(annotationType);
return annotationConstruct.getAnnotation(annotationType);
}
/**
* Returns an immutable list of annotation elements for this value
* descriptor.
*
* @return a list of annotations, not {@code null}
*/
public List<AnnotationElement> getAnnotationElements() {
return annotationConstruct.getUnmodifiableAnnotationElements();
}
/**
* Returns an immutable list of value descriptors if the type is complex,
* else an empty list.
*
* @return a list of value descriptors, not {@code null}
*/
public List<ValueDescriptor> getFields() {
if (type.isSimpleType()) {
return List.of();
}
return type.getFields();
}
// package private
Type getType() {
return type;
}
// package private
void setAnnotations(List<AnnotationElement> anno) {
annotationConstruct.setAnnotationElements(anno);
}
// package private
boolean isConstantPool() {
return constantPool;
}
// package private
String getJavaFieldName() {
return javaFieldName;
}
// package private
boolean isUnsigned() {
return annotationConstruct.hasUnsigned();
}
}