author | egahlin |
Mon, 08 Jul 2019 23:08:05 +0200 | |
branch | JEP-349-branch |
changeset 57460 | bcbc53560c77 |
parent 57452 | 6fabe73e5d9a |
child 58145 | bc54ed8d908a |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
57360 | 2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
50113 | 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 jdk.jfr.consumer; |
|
27 |
||
57360 | 28 |
import java.io.IOException; |
50113 | 29 |
import java.io.PrintWriter; |
30 |
import java.io.StringWriter; |
|
31 |
import java.time.Duration; |
|
32 |
import java.time.Instant; |
|
52850 | 33 |
import java.time.OffsetDateTime; |
57360 | 34 |
import java.util.Collections; |
50113 | 35 |
import java.util.List; |
36 |
import java.util.Objects; |
|
37 |
||
38 |
import jdk.jfr.Timespan; |
|
39 |
import jdk.jfr.Timestamp; |
|
40 |
import jdk.jfr.ValueDescriptor; |
|
41 |
import jdk.jfr.internal.PrivateAccess; |
|
57360 | 42 |
import jdk.jfr.internal.Type; |
43 |
import jdk.jfr.internal.consumer.Parser; |
|
44 |
import jdk.jfr.internal.consumer.RecordingInternals; |
|
52850 | 45 |
import jdk.jfr.internal.tool.PrettyWriter; |
50113 | 46 |
|
47 |
/** |
|
48 |
* A complex data type that consists of one or more fields. |
|
49 |
* <p> |
|
50 |
* This class provides methods to select and query nested objects by passing a |
|
51 |
* dot {@code "."} delimited {@code String} object (for instance, |
|
52 |
* {@code "aaa.bbb"}). A method evaluates a nested object from left to right, |
|
53 |
* and if a part is {@code null}, it throws {@code NullPointerException}. |
|
54 |
* |
|
55 |
* @since 9 |
|
56 |
*/ |
|
57 |
public class RecordedObject { |
|
58 |
||
57360 | 59 |
static{ |
60 |
RecordingInternals.INSTANCE = new RecordingInternals() { |
|
61 |
public List<Type> readTypes(RecordingFile file) throws IOException { |
|
62 |
return file.readTypes(); |
|
63 |
} |
|
64 |
||
65 |
public boolean isLastEventInChunk(RecordingFile file) { |
|
66 |
return file.isLastEventInChunk; |
|
67 |
} |
|
68 |
||
69 |
@Override |
|
70 |
public Object getOffsetDataTime(RecordedObject event, String name) { |
|
71 |
return event.getOffsetDateTime(name); |
|
72 |
} |
|
73 |
||
74 |
@Override |
|
75 |
public void sort(List<RecordedEvent> events) { |
|
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57376
diff
changeset
|
76 |
Collections.sort(events, (e1, e2) -> Long.compare(e1.endTimeTicks, e2.endTimeTicks)); |
57360 | 77 |
} |
78 |
||
79 |
@Override |
|
80 |
public Parser newStringParser() { |
|
81 |
return new StringParser(null, false); |
|
82 |
} |
|
83 |
}; |
|
84 |
} |
|
85 |
||
50113 | 86 |
private final static class UnsignedValue { |
87 |
private final Object o; |
|
88 |
||
89 |
UnsignedValue(Object o) { |
|
90 |
this.o = o; |
|
91 |
} |
|
92 |
||
93 |
Object value() { |
|
94 |
return o; |
|
95 |
} |
|
96 |
} |
|
97 |
||
57376 | 98 |
final Object[] objects; |
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
99 |
final ObjectContext objectContext; |
50113 | 100 |
|
101 |
// package private, not to be subclassed outside this package |
|
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
102 |
RecordedObject(ObjectContext objectContext, Object[] objects) { |
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
103 |
this.objectContext = objectContext; |
50113 | 104 |
this.objects = objects; |
105 |
} |
|
106 |
||
107 |
// package private |
|
108 |
final <T> T getTyped(String name, Class<T> clazz, T defaultValue) { |
|
109 |
// Unnecessary to check field presence twice, but this |
|
110 |
// will do for now. |
|
111 |
if (!hasField(name)) { |
|
112 |
return defaultValue; |
|
113 |
} |
|
114 |
T object = getValue(name); |
|
115 |
if (object == null || object.getClass().isAssignableFrom(clazz)) { |
|
116 |
return object; |
|
117 |
} else { |
|
118 |
return defaultValue; |
|
119 |
} |
|
120 |
} |
|
121 |
||
122 |
/** |
|
123 |
* Returns {@code true} if a field with the given name exists, {@code false} |
|
124 |
* otherwise. |
|
125 |
* |
|
126 |
* @param name name of the field to get, not {@code null} |
|
127 |
* |
|
128 |
* @return {@code true} if the field exists, {@code false} otherwise. |
|
129 |
* |
|
130 |
* @see #getFields() |
|
131 |
*/ |
|
132 |
public boolean hasField(String name) { |
|
133 |
Objects.requireNonNull(name); |
|
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
134 |
for (ValueDescriptor v : objectContext.fields) { |
50113 | 135 |
if (v.getName().equals(name)) { |
136 |
return true; |
|
137 |
} |
|
138 |
} |
|
139 |
int dotIndex = name.indexOf("."); |
|
140 |
if (dotIndex > 0) { |
|
141 |
String structName = name.substring(0, dotIndex); |
|
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
142 |
for (ValueDescriptor v : objectContext.fields) { |
50113 | 143 |
if (!v.getFields().isEmpty() && v.getName().equals(structName)) { |
144 |
RecordedObject child = getValue(structName); |
|
145 |
if (child != null) { |
|
146 |
return child.hasField(name.substring(dotIndex + 1)); |
|
147 |
} |
|
148 |
} |
|
149 |
} |
|
150 |
} |
|
151 |
return false; |
|
152 |
} |
|
153 |
||
154 |
/** |
|
155 |
* Returns the value of the field with the given name. |
|
156 |
* <p> |
|
157 |
* The return type may be a primitive type or a subclass of |
|
158 |
* {@link RecordedObject}. |
|
159 |
* <p> |
|
160 |
* It's possible to index into a nested object by using {@code "."} (for |
|
161 |
* instance {@code "thread.group.parent.name}"). |
|
162 |
* <p> |
|
163 |
* A field might change or be removed in a future JDK release. A best practice |
|
164 |
* for callers of this method is to validate the field before attempting access. |
|
165 |
* <p> |
|
166 |
* Example |
|
167 |
* |
|
168 |
* <pre> |
|
169 |
* <code> |
|
170 |
* if (event.hasField("intValue")) { |
|
171 |
* int intValue = event.getValue("intValue"); |
|
172 |
* System.out.println("Int value: " + intValue); |
|
173 |
* } |
|
174 |
* |
|
175 |
* if (event.hasField("objectClass")) { |
|
176 |
* RecordedClass clazz = event.getValue("objectClass"); |
|
177 |
* System.out.println("Class name: " + clazz.getName()); |
|
178 |
* } |
|
179 |
* |
|
180 |
* if (event.hasField("sampledThread")) { |
|
181 |
* RecordedThread sampledThread = event.getValue("sampledThread"); |
|
182 |
* System.out.println("Sampled thread: " + sampledThread.getName()); |
|
183 |
* } |
|
184 |
* </code> |
|
185 |
* </pre> |
|
186 |
* |
|
187 |
* @param <T> the return type |
|
188 |
* @param name of the field to get, not {@code null} |
|
189 |
* @throws IllegalArgumentException if no field called {@code name} exists |
|
190 |
* |
|
191 |
* @return the value, can be {@code null} |
|
192 |
* |
|
193 |
* @see #hasField(String) |
|
194 |
* |
|
195 |
*/ |
|
196 |
final public <T> T getValue(String name) { |
|
197 |
@SuppressWarnings("unchecked") |
|
198 |
T t = (T) getValue(name, false); |
|
199 |
return t; |
|
200 |
} |
|
201 |
||
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57376
diff
changeset
|
202 |
protected Object objectAt(int index) { |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57376
diff
changeset
|
203 |
return objects[index]; |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57376
diff
changeset
|
204 |
} |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57376
diff
changeset
|
205 |
|
50113 | 206 |
private Object getValue(String name, boolean allowUnsigned) { |
207 |
Objects.requireNonNull(name); |
|
208 |
int index = 0; |
|
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
209 |
for (ValueDescriptor v : objectContext.fields) { |
50113 | 210 |
if (name.equals(v.getName())) { |
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57376
diff
changeset
|
211 |
Object object = objectAt(index); |
50113 | 212 |
if (object == null) { |
213 |
// error or missing |
|
214 |
return null; |
|
215 |
} |
|
216 |
if (v.getFields().isEmpty()) { |
|
217 |
if (allowUnsigned && PrivateAccess.getInstance().isUnsigned(v)) { |
|
218 |
// Types that are meaningless to widen |
|
219 |
if (object instanceof Character || object instanceof Long) { |
|
220 |
return object; |
|
221 |
} |
|
222 |
return new UnsignedValue(object); |
|
223 |
} |
|
224 |
return object; // primitives and primitive arrays |
|
225 |
} else { |
|
226 |
if (object instanceof RecordedObject) { |
|
227 |
// known types from factory |
|
228 |
return object; |
|
229 |
} |
|
230 |
// must be array type |
|
231 |
Object[] array = (Object[]) object; |
|
232 |
if (v.isArray()) { |
|
233 |
// struct array |
|
234 |
return structifyArray(v, array, 0); |
|
235 |
} |
|
236 |
// struct |
|
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
237 |
return new RecordedObject(objectContext.getInstance(v), (Object[]) object); |
50113 | 238 |
} |
239 |
} |
|
240 |
index++; |
|
241 |
} |
|
242 |
||
243 |
int dotIndex = name.indexOf("."); |
|
244 |
if (dotIndex > 0) { |
|
245 |
String structName = name.substring(0, dotIndex); |
|
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
246 |
for (ValueDescriptor v : objectContext.fields) { |
50113 | 247 |
if (!v.getFields().isEmpty() && v.getName().equals(structName)) { |
248 |
RecordedObject child = getValue(structName); |
|
249 |
String subName = name.substring(dotIndex + 1); |
|
250 |
if (child != null) { |
|
251 |
return child.getValue(subName, allowUnsigned); |
|
252 |
} else { |
|
253 |
// Call getValueDescriptor to trigger IllegalArgumentException if the name is |
|
254 |
// incorrect. Type can't be validate due to type erasure |
|
255 |
getValueDescriptor(v.getFields(), subName, null); |
|
256 |
throw new NullPointerException("Field value for \"" + structName + "\" was null. Can't access nested field \"" + subName + "\""); |
|
257 |
} |
|
258 |
} |
|
259 |
} |
|
260 |
} |
|
261 |
throw new IllegalArgumentException("Could not find field with name " + name); |
|
262 |
} |
|
263 |
||
264 |
// Returns the leaf value descriptor matches both name or value, or throws an |
|
265 |
// IllegalArgumentException |
|
266 |
private ValueDescriptor getValueDescriptor(List<ValueDescriptor> descriptors, String name, String leafType) { |
|
267 |
int dotIndex = name.indexOf("."); |
|
268 |
if (dotIndex > 0) { |
|
269 |
String first = name.substring(0, dotIndex); |
|
270 |
String second = name.substring(dotIndex + 1); |
|
271 |
for (ValueDescriptor v : descriptors) { |
|
272 |
if (v.getName().equals(first)) { |
|
273 |
List<ValueDescriptor> fields = v.getFields(); |
|
274 |
if (!fields.isEmpty()) { |
|
275 |
return getValueDescriptor(v.getFields(), second, leafType); |
|
276 |
} |
|
277 |
} |
|
278 |
} |
|
279 |
throw new IllegalArgumentException("Attempt to get unknown field \"" + first + "\""); |
|
280 |
} |
|
281 |
for (ValueDescriptor v : descriptors) { |
|
282 |
if (v.getName().equals(name)) { |
|
283 |
if (leafType != null && !v.getTypeName().equals(leafType)) { |
|
284 |
throw new IllegalArgumentException("Attempt to get " + v.getTypeName() + " field \"" + name + "\" with illegal data type conversion " + leafType); |
|
285 |
} |
|
286 |
return v; |
|
287 |
} |
|
288 |
} |
|
289 |
throw new IllegalArgumentException("\"Attempt to get unknown field \"" + name + "\""); |
|
290 |
} |
|
291 |
||
292 |
// Gets a value, but checks that type and name is correct first |
|
293 |
// This is to prevent a call to getString on a thread field, that is |
|
294 |
// null to succeed. |
|
295 |
private <T> T getTypedValue(String name, String typeName) { |
|
296 |
Objects.requireNonNull(name); |
|
297 |
// Validate name and type first |
|
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
298 |
getValueDescriptor(objectContext.fields, name, typeName); |
50113 | 299 |
return getValue(name); |
300 |
} |
|
301 |
||
302 |
private Object[] structifyArray(ValueDescriptor v, Object[] array, int dimension) { |
|
303 |
if (array == null) { |
|
304 |
return null; |
|
305 |
} |
|
306 |
Object[] structArray = new Object[array.length]; |
|
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
307 |
ObjectContext objContext = objectContext.getInstance(v); |
50113 | 308 |
for (int i = 0; i < structArray.length; i++) { |
309 |
Object arrayElement = array[i]; |
|
310 |
if (dimension == 0) { |
|
311 |
// No general way to handle structarrays |
|
312 |
// without invoking ObjectFactory for every instance (which may require id) |
|
313 |
if (isStackFrameType(v.getTypeName())) { |
|
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
314 |
structArray[i] = new RecordedFrame(objContext, (Object[]) arrayElement); |
50113 | 315 |
} else { |
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
316 |
structArray[i] = new RecordedObject(objContext, (Object[]) arrayElement); |
50113 | 317 |
} |
318 |
} else { |
|
319 |
structArray[i] = structifyArray(v, (Object[]) arrayElement, dimension - 1); |
|
320 |
} |
|
321 |
} |
|
322 |
return structArray; |
|
323 |
} |
|
324 |
||
325 |
private boolean isStackFrameType(String typeName) { |
|
326 |
if (ObjectFactory.STACK_FRAME_VERSION_1.equals(typeName)) { |
|
327 |
return true; |
|
328 |
} |
|
329 |
if (ObjectFactory.STACK_FRAME_VERSION_2.equals(typeName)) { |
|
330 |
return true; |
|
331 |
} |
|
332 |
return false; |
|
333 |
} |
|
334 |
||
335 |
/** |
|
336 |
* Returns an immutable list of the fields for this object. |
|
337 |
* |
|
338 |
* @return the fields, not {@code null} |
|
339 |
*/ |
|
340 |
public List<ValueDescriptor> getFields() { |
|
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
341 |
return objectContext.fields; |
50113 | 342 |
} |
343 |
||
344 |
/** |
|
345 |
* Returns the value of a field of type {@code boolean}. |
|
346 |
* <p> |
|
347 |
* It's possible to index into a nested object using {@code "."} (for example, |
|
348 |
* {@code "aaa.bbb"}). |
|
349 |
* <p> |
|
350 |
* A field might change or be removed in a future JDK release. A best practice |
|
351 |
* for callers of this method is to validate the field before attempting access. |
|
352 |
* |
|
353 |
* @param name name of the field to get, not {@code null} |
|
354 |
* |
|
355 |
* @return the value of the field, {@code true} or {@code false} |
|
356 |
* |
|
357 |
* @throws IllegalArgumentException if the field doesn't exist, or the field is |
|
358 |
* not of type {@code boolean} |
|
359 |
* |
|
360 |
* @see #hasField(String) |
|
361 |
* @see #getValue(String) |
|
362 |
*/ |
|
363 |
public final boolean getBoolean(String name) { |
|
364 |
Object o = getValue(name); |
|
365 |
if (o instanceof Boolean) { |
|
366 |
return ((Boolean) o).booleanValue(); |
|
367 |
} |
|
368 |
throw newIllegalArgumentException(name, "boolean"); |
|
369 |
} |
|
370 |
||
371 |
/** |
|
372 |
* Returns the value of a field of type {@code byte}. |
|
373 |
* <p> |
|
374 |
* It's possible to index into a nested object using {@code "."} (for example, |
|
375 |
* {@code "foo.bar"}). |
|
376 |
* <p> |
|
377 |
* A field might change or be removed in a future JDK release. A best practice |
|
378 |
* for callers of this method is to validate the field before attempting access. |
|
379 |
* |
|
380 |
* @param name of the field to get, not {@code null} |
|
381 |
* |
|
382 |
* @return the value of the field |
|
383 |
* |
|
384 |
* @throws IllegalArgumentException if the field doesn't exist, or the field is |
|
385 |
* not of type {@code byte} |
|
386 |
* |
|
387 |
* @see #hasField(String) |
|
388 |
* @see #getValue(String) |
|
389 |
*/ |
|
390 |
public final byte getByte(String name) { |
|
391 |
Object o = getValue(name); |
|
392 |
if (o instanceof Byte) { |
|
393 |
return ((Byte) o).byteValue(); |
|
394 |
} |
|
395 |
throw newIllegalArgumentException(name, "byte"); |
|
396 |
} |
|
397 |
||
398 |
/** |
|
399 |
* Returns the value of a field of type {@code char}. |
|
400 |
* <p> |
|
401 |
* It's possible to index into a nested object using {@code "."} (for example, |
|
402 |
* {@code "aaa.bbb"}). |
|
403 |
* <p> |
|
404 |
* A field might change or be removed in a future JDK release. A best practice |
|
405 |
* for callers of this method is to validate the field before attempting access. |
|
406 |
* |
|
407 |
* @param name of the field to get, not {@code null} |
|
408 |
* |
|
409 |
* @return the value of the field as a {@code char} |
|
410 |
* |
|
411 |
* @throws IllegalArgumentException if the field doesn't exist, or the field is |
|
412 |
* not of type {@code char} |
|
413 |
* |
|
414 |
* @see #hasField(String) |
|
415 |
* @see #getValue(String) |
|
416 |
*/ |
|
417 |
public final char getChar(String name) { |
|
418 |
Object o = getValue(name); |
|
419 |
if (o instanceof Character) { |
|
420 |
return ((Character) o).charValue(); |
|
421 |
} |
|
422 |
||
423 |
throw newIllegalArgumentException(name, "char"); |
|
424 |
} |
|
425 |
||
426 |
/** |
|
427 |
* Returns the value of a field of type {@code short} or of another primitive |
|
428 |
* type convertible to type {@code short} by a widening conversion. |
|
429 |
* <p> |
|
430 |
* This method can be used on the following types: {@code short} and {@code byte}. |
|
431 |
* <p> |
|
432 |
* If the field has the {@code @Unsigned} annotation and is of a narrower type |
|
433 |
* than {@code short}, then the value is returned as an unsigned. |
|
434 |
* <p> |
|
435 |
* It's possible to index into a nested object using {@code "."} (for example, |
|
436 |
* {@code "aaa.bbb"}). |
|
437 |
* <p> |
|
438 |
* A field might change or be removed in a future JDK release. A best practice |
|
439 |
* for callers of this method is to validate the field before attempting access. |
|
440 |
* |
|
441 |
* @param name of the field to get, not {@code null} |
|
442 |
* |
|
443 |
* @return the value of the field converted to type {@code short} |
|
444 |
* |
|
445 |
* @throws IllegalArgumentException if the field doesn't exist, or the field |
|
446 |
* value can't be converted to the type {@code short} by a widening |
|
447 |
* conversion |
|
448 |
* |
|
449 |
* @see #hasField(String) |
|
450 |
* @set #getValue(String) |
|
451 |
*/ |
|
452 |
public final short getShort(String name) { |
|
453 |
Object o = getValue(name, true); |
|
454 |
if (o instanceof Short) { |
|
455 |
return ((Short) o).shortValue(); |
|
456 |
} |
|
457 |
if (o instanceof Byte) { |
|
458 |
return ((Byte) o).byteValue(); |
|
459 |
} |
|
460 |
if (o instanceof UnsignedValue) { |
|
461 |
Object u = ((UnsignedValue) o).value(); |
|
462 |
if (u instanceof Short) { |
|
463 |
return ((Short) u).shortValue(); |
|
464 |
} |
|
465 |
if (u instanceof Byte) { |
|
466 |
return (short) Byte.toUnsignedInt(((Byte) u)); |
|
467 |
} |
|
468 |
} |
|
469 |
throw newIllegalArgumentException(name, "short"); |
|
470 |
} |
|
471 |
||
472 |
/** |
|
473 |
* Returns the value of a field of type {@code int} or of another primitive type |
|
474 |
* that is convertible to type {@code int} by a widening conversion. |
|
475 |
* <p> |
|
476 |
* This method can be used on fields of the following types: {@code int}, |
|
477 |
* {@code short}, {@code char}, and {@code byte}. |
|
478 |
* <p> |
|
479 |
* If the field has the {@code @Unsigned} annotation and is of a narrower type |
|
480 |
* than {@code int}, then the value will be returned as an unsigned. |
|
481 |
* <p> |
|
482 |
* It's possible to index into a nested object using {@code "."} (for example, |
|
483 |
* {@code "aaa.bbb"}). |
|
484 |
* <p> |
|
485 |
* A field might change or be removed in a future JDK release. A best practice |
|
486 |
* for callers of this method is to validate the field before attempting access. |
|
487 |
* |
|
488 |
* @param name of the field to get, not {@code null} |
|
489 |
* |
|
490 |
* @return the value of the field converted to type {@code int} |
|
491 |
* |
|
492 |
* @throws IllegalArgumentException if the field doesn't exist, or the field |
|
493 |
* value can't be converted to the type {@code int} by a widening |
|
494 |
* conversion |
|
495 |
* |
|
496 |
* @see #hasField(String) |
|
497 |
* @set #getValue(String) |
|
498 |
*/ |
|
499 |
public final int getInt(String name) { |
|
500 |
Object o = getValue(name, true); |
|
501 |
if (o instanceof Integer) { |
|
502 |
return ((Integer) o).intValue(); |
|
503 |
} |
|
504 |
if (o instanceof Short) { |
|
505 |
return ((Short) o).intValue(); |
|
506 |
} |
|
507 |
if (o instanceof Character) { |
|
508 |
return ((Character) o).charValue(); |
|
509 |
} |
|
510 |
if (o instanceof Byte) { |
|
511 |
return ((Byte) o).intValue(); |
|
512 |
} |
|
513 |
if (o instanceof UnsignedValue) { |
|
514 |
Object u = ((UnsignedValue) o).value(); |
|
515 |
if (u instanceof Integer) { |
|
516 |
return ((Integer) u).intValue(); |
|
517 |
} |
|
518 |
if (u instanceof Short) { |
|
519 |
return Short.toUnsignedInt(((Short) u)); |
|
520 |
} |
|
521 |
if (u instanceof Byte) { |
|
522 |
return Byte.toUnsignedInt(((Byte) u)); |
|
523 |
} |
|
524 |
} |
|
525 |
throw newIllegalArgumentException(name, "int"); |
|
526 |
} |
|
527 |
||
528 |
/** |
|
529 |
* Returns the value of a field of type {@code float} or of another primitive |
|
530 |
* type convertible to type {@code float} by a widening conversion. |
|
531 |
* <p> |
|
532 |
* This method can be used on fields of the following types: {@code float}, |
|
533 |
* {@code long}, {@code int}, {@code short}, {@code char}, and {@code byte}. |
|
534 |
* <p> |
|
535 |
* It's possible to index into a nested object using {@code "."} (for example, |
|
536 |
* {@code "aaa.bbb"}). |
|
537 |
* <p> |
|
538 |
* A field might change or be removed in a future JDK release. A best practice |
|
539 |
* for callers of this method is to validate the field before attempting access. |
|
540 |
* |
|
541 |
* @param name of the field to get, not {@code null} |
|
542 |
* |
|
543 |
* @return the value of the field converted to type {@code float} |
|
544 |
* |
|
545 |
* @throws IllegalArgumentException if the field doesn't exist, or the field |
|
546 |
* value can't be converted to the type {@code float} by a widening |
|
547 |
* conversion |
|
548 |
* |
|
549 |
* @see #hasField(String) |
|
550 |
* @set #getValue(String) |
|
551 |
*/ |
|
552 |
public final float getFloat(String name) { |
|
553 |
Object o = getValue(name); |
|
554 |
if (o instanceof Float) { |
|
555 |
return ((Float) o).floatValue(); |
|
556 |
} |
|
557 |
if (o instanceof Long) { |
|
558 |
return ((Long) o).floatValue(); |
|
559 |
} |
|
560 |
if (o instanceof Integer) { |
|
561 |
return ((Integer) o).floatValue(); |
|
562 |
} |
|
563 |
if (o instanceof Short) { |
|
564 |
return ((Short) o).floatValue(); |
|
565 |
} |
|
566 |
if (o instanceof Byte) { |
|
567 |
return ((Byte) o).byteValue(); |
|
568 |
} |
|
569 |
if (o instanceof Character) { |
|
570 |
return ((Character) o).charValue(); |
|
571 |
} |
|
572 |
throw newIllegalArgumentException(name, "float"); |
|
573 |
} |
|
574 |
||
575 |
/** |
|
576 |
* Returns the value of a field of type {@code long} or of another primitive |
|
577 |
* type that is convertible to type {@code long} by a widening conversion. |
|
578 |
* <p> |
|
579 |
* This method can be used on fields of the following types: {@code long}, |
|
580 |
* {@code int}, {@code short}, {@code char}, and {@code byte}. |
|
581 |
* <p> |
|
582 |
* If the field has the {@code @Unsigned} annotation and is of a narrower type |
|
583 |
* than {@code long}, then the value will be returned as an unsigned. |
|
584 |
* <p> |
|
585 |
* It's possible to index into a nested object using {@code "."} (for example, |
|
586 |
* {@code "aaa.bbb"}). |
|
587 |
* <p> |
|
588 |
* A field might change or be removed in a future JDK release. A best practice |
|
589 |
* for callers of this method is to validate the field before attempting access. |
|
590 |
* |
|
591 |
* @param name of the field to get, not {@code null} |
|
592 |
* |
|
593 |
* @return the value of the field converted to type {@code long} |
|
594 |
* |
|
595 |
* @throws IllegalArgumentException if the field doesn't exist, or the field |
|
596 |
* value can't be converted to the type {@code long} via a widening |
|
597 |
* conversion |
|
598 |
* |
|
599 |
* @see #hasField(String) |
|
600 |
* @set #getValue(String) |
|
601 |
*/ |
|
602 |
public final long getLong(String name) { |
|
603 |
Object o = getValue(name, true); |
|
604 |
if (o instanceof Long) { |
|
605 |
return ((Long) o).longValue(); |
|
606 |
} |
|
607 |
if (o instanceof Integer) { |
|
608 |
return ((Integer) o).longValue(); |
|
609 |
} |
|
610 |
if (o instanceof Short) { |
|
611 |
return ((Short) o).longValue(); |
|
612 |
} |
|
613 |
if (o instanceof Character) { |
|
614 |
return ((Character) o).charValue(); |
|
615 |
} |
|
616 |
if (o instanceof Byte) { |
|
617 |
return ((Byte) o).longValue(); |
|
618 |
} |
|
619 |
if (o instanceof UnsignedValue) { |
|
620 |
Object u = ((UnsignedValue) o).value(); |
|
621 |
if (u instanceof Integer) { |
|
622 |
return Integer.toUnsignedLong(((Integer) u)); |
|
623 |
} |
|
624 |
if (u instanceof Short) { |
|
625 |
return Short.toUnsignedLong(((Short) u)); |
|
626 |
} |
|
627 |
if (u instanceof Byte) { |
|
628 |
return Byte.toUnsignedLong(((Byte) u)); |
|
629 |
} |
|
630 |
} |
|
631 |
throw newIllegalArgumentException(name, "long"); |
|
632 |
} |
|
633 |
||
634 |
/** |
|
635 |
* Returns the value of a field of type {@code double} or of another primitive |
|
636 |
* type that is convertible to type {@code double} by a widening conversion. |
|
637 |
* <p> |
|
638 |
* This method can be used on fields of the following types: {@code double}, {@code float}, |
|
639 |
* {@code long}, {@code int}, {@code short}, {@code char}, and {@code byte}. |
|
640 |
* <p> |
|
641 |
* It's possible to index into a nested object using {@code "."} (for example, |
|
642 |
* {@code "aaa.bbb"}). |
|
643 |
* <p> |
|
644 |
* A field might change or be removed in a future JDK release. A best practice |
|
645 |
* for callers of this method is to validate the field before attempting access. |
|
646 |
* |
|
647 |
* @param name of the field to get, not {@code null} |
|
648 |
* |
|
649 |
* @return the value of the field converted to type {@code double} |
|
650 |
* |
|
651 |
* @throws IllegalArgumentException if the field doesn't exist, or the field |
|
652 |
* value can't be converted to the type {@code double} by a widening |
|
653 |
* conversion |
|
654 |
* |
|
655 |
* @see #hasField(String) |
|
656 |
* @set #getValue(String) |
|
657 |
*/ |
|
658 |
public final double getDouble(String name) { |
|
659 |
Object o = getValue(name); |
|
660 |
if (o instanceof Double) { |
|
661 |
return ((Double) o).doubleValue(); |
|
662 |
} |
|
663 |
if (o instanceof Float) { |
|
664 |
return ((Float) o).doubleValue(); |
|
665 |
} |
|
666 |
if (o instanceof Long) { |
|
667 |
return ((Long) o).doubleValue(); |
|
668 |
} |
|
669 |
if (o instanceof Integer) { |
|
670 |
return ((Integer) o).doubleValue(); |
|
671 |
} |
|
672 |
if (o instanceof Short) { |
|
673 |
return ((Short) o).doubleValue(); |
|
674 |
} |
|
675 |
if (o instanceof Byte) { |
|
676 |
return ((Byte) o).byteValue(); |
|
677 |
} |
|
678 |
if (o instanceof Character) { |
|
679 |
return ((Character) o).charValue(); |
|
680 |
} |
|
681 |
throw newIllegalArgumentException(name, "double"); |
|
682 |
} |
|
683 |
||
684 |
/** |
|
685 |
* Returns the value of a field of type {@code String}. |
|
686 |
* <p> |
|
687 |
* It's possible to index into a nested object using {@code "."} (for example, |
|
688 |
* {@code "foo.bar"}). |
|
689 |
* <p> |
|
690 |
* A field might change or be removed in a future JDK release. A best practice |
|
691 |
* for callers of this method is to validate the field before attempting access. |
|
692 |
* |
|
693 |
* @param name of the field to get, not {@code null} |
|
694 |
* |
|
695 |
* @return the value of the field as a {@code String}, can be {@code null} |
|
696 |
* |
|
697 |
* @throws IllegalArgumentException if the field doesn't exist, or the field |
|
698 |
* isn't of type {@code String} |
|
699 |
* |
|
700 |
* @see #hasField(String) |
|
701 |
* @set #getValue(String) |
|
702 |
*/ |
|
703 |
public final String getString(String name) { |
|
704 |
return getTypedValue(name, "java.lang.String"); |
|
705 |
} |
|
706 |
||
707 |
/** |
|
708 |
* Returns the value of a timespan field. |
|
709 |
* <p> |
|
710 |
* This method can be used on fields annotated with {@code @Timespan}, and of |
|
711 |
* the following types: {@code long}, {@code int}, {@code short}, {@code char}, |
|
712 |
* and {@code byte}. |
|
713 |
* <p> |
|
714 |
* It's possible to index into a nested object using {@code "."} (for example, |
|
715 |
* {@code "aaa.bbb"}). |
|
716 |
* <p> |
|
717 |
* A field might change or be removed in a future JDK release. A best practice |
|
718 |
* for callers of this method is to validate the field before attempting access. |
|
719 |
* |
|
720 |
* @param name of the field to get, not {@code null} |
|
721 |
* |
|
722 |
* @return a time span represented as a {@code Duration}, not {@code null} |
|
723 |
* |
|
724 |
* @throws IllegalArgumentException if the field doesn't exist, or the field |
|
725 |
* value can't be converted to a {@code Duration} object |
|
726 |
* |
|
727 |
* @see #hasField(String) |
|
728 |
* @set #getValue(String) |
|
729 |
*/ |
|
730 |
public final Duration getDuration(String name) { |
|
731 |
Object o = getValue(name); |
|
732 |
if (o instanceof Long) { |
|
733 |
return getDuration(((Long) o).longValue(), name); |
|
734 |
} |
|
735 |
if (o instanceof Integer) { |
|
736 |
return getDuration(((Integer) o).longValue(), name); |
|
737 |
} |
|
738 |
if (o instanceof Short) { |
|
739 |
return getDuration(((Short) o).longValue(), name); |
|
740 |
} |
|
741 |
if (o instanceof Character) { |
|
742 |
return getDuration(((Character) o).charValue(), name); |
|
743 |
} |
|
744 |
if (o instanceof Byte) { |
|
745 |
return getDuration(((Byte) o).longValue(), name); |
|
746 |
} |
|
747 |
if (o instanceof UnsignedValue) { |
|
748 |
Object u = ((UnsignedValue) o).value(); |
|
749 |
if (u instanceof Integer) { |
|
750 |
return getDuration(Integer.toUnsignedLong((Integer) u), name); |
|
751 |
} |
|
752 |
if (u instanceof Short) { |
|
753 |
return getDuration(Short.toUnsignedLong((Short) u), name); |
|
754 |
} |
|
755 |
if (u instanceof Byte) { |
|
756 |
return getDuration(Short.toUnsignedLong((Byte) u), name); |
|
757 |
} |
|
758 |
} |
|
759 |
throw newIllegalArgumentException(name, "java,time.Duration"); |
|
760 |
} |
|
761 |
||
762 |
private Duration getDuration(long timespan, String name) throws InternalError { |
|
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
763 |
ValueDescriptor v = getValueDescriptor(objectContext.fields, name, null); |
52981
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
52850
diff
changeset
|
764 |
if (timespan == Long.MIN_VALUE) { |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
52850
diff
changeset
|
765 |
return Duration.ofSeconds(Long.MIN_VALUE, 0); |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
52850
diff
changeset
|
766 |
} |
50113 | 767 |
Timespan ts = v.getAnnotation(Timespan.class); |
768 |
if (ts != null) { |
|
769 |
switch (ts.value()) { |
|
770 |
case Timespan.MICROSECONDS: |
|
771 |
return Duration.ofNanos(1000 * timespan); |
|
772 |
case Timespan.SECONDS: |
|
773 |
return Duration.ofSeconds(timespan); |
|
774 |
case Timespan.MILLISECONDS: |
|
775 |
return Duration.ofMillis(timespan); |
|
776 |
case Timespan.NANOSECONDS: |
|
777 |
return Duration.ofNanos(timespan); |
|
778 |
case Timespan.TICKS: |
|
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
779 |
return Duration.ofNanos(objectContext.timeConverter.convertTimespan(timespan)); |
50113 | 780 |
} |
781 |
throw new IllegalArgumentException("Attempt to get " + v.getTypeName() + " field \"" + name + "\" with illegal timespan unit " + ts.value()); |
|
782 |
} |
|
783 |
throw new IllegalArgumentException("Attempt to get " + v.getTypeName() + " field \"" + name + "\" with missing @Timespan"); |
|
784 |
} |
|
785 |
||
786 |
/** |
|
787 |
* Returns the value of a timestamp field. |
|
788 |
* <p> |
|
789 |
* This method can be used on fields annotated with {@code @Timestamp}, and of |
|
790 |
* the following types: {@code long}, {@code int}, {@code short}, {@code char} |
|
791 |
* and {@code byte}. |
|
792 |
* <p> |
|
793 |
* It's possible to index into a nested object using {@code "."} (for example, |
|
794 |
* {@code "aaa.bbb"}). |
|
795 |
* <p> |
|
796 |
* A field might change or be removed in a future JDK release. A best practice |
|
797 |
* for callers of this method is to validate the field before attempting access. |
|
798 |
* |
|
799 |
* @param name of the field to get, not {@code null} |
|
800 |
* |
|
801 |
* @return a timstamp represented as an {@code Instant}, not {@code null} |
|
802 |
* |
|
803 |
* @throws IllegalArgumentException if the field doesn't exist, or the field |
|
804 |
* value can't be converted to an {@code Instant} object |
|
805 |
* |
|
806 |
* @see #hasField(String) |
|
807 |
* @set #getValue(String) |
|
808 |
*/ |
|
809 |
public final Instant getInstant(String name) { |
|
810 |
Object o = getValue(name, true); |
|
811 |
if (o instanceof Long) { |
|
812 |
return getInstant(((Long) o).longValue(), name); |
|
813 |
} |
|
814 |
if (o instanceof Integer) { |
|
815 |
return getInstant(((Integer) o).longValue(), name); |
|
816 |
} |
|
817 |
if (o instanceof Short) { |
|
818 |
return getInstant(((Short) o).longValue(), name); |
|
819 |
} |
|
820 |
if (o instanceof Character) { |
|
821 |
return getInstant(((Character) o).charValue(), name); |
|
822 |
} |
|
823 |
if (o instanceof Byte) { |
|
824 |
return getInstant(((Byte) o).longValue(), name); |
|
825 |
} |
|
826 |
if (o instanceof UnsignedValue) { |
|
827 |
Object u = ((UnsignedValue) o).value(); |
|
828 |
if (u instanceof Integer) { |
|
829 |
return getInstant(Integer.toUnsignedLong((Integer) u), name); |
|
830 |
} |
|
831 |
if (u instanceof Short) { |
|
832 |
return getInstant(Short.toUnsignedLong((Short) u), name); |
|
833 |
} |
|
834 |
if (u instanceof Byte) { |
|
835 |
return getInstant(Short.toUnsignedLong((Byte) u), name); |
|
836 |
} |
|
837 |
} |
|
52981
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
52850
diff
changeset
|
838 |
throw newIllegalArgumentException(name, "java.time.Instant"); |
50113 | 839 |
} |
840 |
||
841 |
private Instant getInstant(long timestamp, String name) { |
|
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
842 |
ValueDescriptor v = getValueDescriptor(objectContext.fields, name, null); |
50113 | 843 |
Timestamp ts = v.getAnnotation(Timestamp.class); |
844 |
if (ts != null) { |
|
52981
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
52850
diff
changeset
|
845 |
if (timestamp == Long.MIN_VALUE) { |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
52850
diff
changeset
|
846 |
return Instant.MIN; |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
52850
diff
changeset
|
847 |
} |
50113 | 848 |
switch (ts.value()) { |
849 |
case Timestamp.MILLISECONDS_SINCE_EPOCH: |
|
850 |
return Instant.ofEpochMilli(timestamp); |
|
851 |
case Timestamp.TICKS: |
|
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
852 |
return Instant.ofEpochSecond(0, objectContext.timeConverter.convertTimestamp(timestamp)); |
50113 | 853 |
} |
854 |
throw new IllegalArgumentException("Attempt to get " + v.getTypeName() + " field \"" + name + "\" with illegal timestamp unit " + ts.value()); |
|
855 |
} |
|
856 |
throw new IllegalArgumentException("Attempt to get " + v.getTypeName() + " field \"" + name + "\" with missing @Timestamp"); |
|
857 |
} |
|
858 |
||
859 |
/** |
|
860 |
* Returns the value of a field of type {@code Class}. |
|
861 |
* <p> |
|
862 |
* It's possible to index into a nested object using {@code "."} (for example, |
|
863 |
* {@code "aaa.bbb"}). |
|
864 |
* <p> |
|
865 |
* A field might change or be removed in a future JDK release. A best practice |
|
866 |
* for callers of this method is to validate the field before attempting access. |
|
867 |
* |
|
868 |
* @param name of the field to get, not {@code null} |
|
869 |
* |
|
870 |
* @return the value of the field as a {@code RecordedClass}, can be |
|
871 |
* {@code null} |
|
872 |
* |
|
873 |
* @throws IllegalArgumentException if the field doesn't exist, or the field |
|
874 |
* isn't of type {@code Class} |
|
875 |
* |
|
876 |
* @see #hasField(String) |
|
877 |
* @set #getValue(String) |
|
878 |
*/ |
|
879 |
public final RecordedClass getClass(String name) { |
|
880 |
return getTypedValue(name, "java.lang.Class"); |
|
881 |
} |
|
882 |
||
883 |
/** |
|
884 |
* Returns the value of a field of type {@code Thread}. |
|
885 |
* <p> |
|
886 |
* It's possible to index into a nested object using {@code "."} (for example, |
|
887 |
* {@code "foo.bar"}). |
|
888 |
* <p> |
|
889 |
* A field might change or be removed in a future JDK release. A best practice |
|
890 |
* for callers of this method is to validate the field before attempting access. |
|
891 |
* |
|
892 |
* @param name of the field to get, not {@code null} |
|
893 |
* |
|
894 |
* @return the value of the field as a {@code RecordedThread} object, can be |
|
895 |
* {@code null} |
|
896 |
* |
|
897 |
* @throws IllegalArgumentException if the field doesn't exist, or the field |
|
898 |
* isn't of type {@code Thread} |
|
899 |
* |
|
900 |
* @see #hasField(String) |
|
901 |
* @set #getValue(String) |
|
902 |
*/ |
|
903 |
public final RecordedThread getThread(String name) { |
|
904 |
return getTypedValue(name, "java.lang.Thread"); |
|
905 |
} |
|
906 |
||
907 |
/** |
|
908 |
* Returns a textual representation of this object. |
|
909 |
* |
|
910 |
* @return textual description of this object |
|
911 |
*/ |
|
912 |
@Override |
|
913 |
final public String toString() { |
|
914 |
StringWriter s = new StringWriter(); |
|
915 |
PrettyWriter p = new PrettyWriter(new PrintWriter(s)); |
|
52850 | 916 |
p.setStackDepth(5); |
917 |
if (this instanceof RecordedEvent) { |
|
918 |
p.print((RecordedEvent) this); |
|
919 |
} else { |
|
920 |
p.print(this, ""); |
|
921 |
} |
|
922 |
p.flush(true); |
|
923 |
return s.toString(); |
|
924 |
} |
|
50113 | 925 |
|
52850 | 926 |
// package private for now. Used by EventWriter |
927 |
OffsetDateTime getOffsetDateTime(String name) { |
|
52981
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
52850
diff
changeset
|
928 |
Instant instant = getInstant(name); |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
52850
diff
changeset
|
929 |
if (instant.equals(Instant.MIN)) { |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
52850
diff
changeset
|
930 |
return OffsetDateTime.MIN; |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
52850
diff
changeset
|
931 |
} |
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
932 |
return OffsetDateTime.ofInstant(getInstant(name), objectContext.timeConverter.getZoneOffset()); |
50113 | 933 |
} |
934 |
||
935 |
private static IllegalArgumentException newIllegalArgumentException(String name, String typeName) { |
|
936 |
return new IllegalArgumentException("Attempt to get field \"" + name + "\" with illegal data type conversion " + typeName); |
|
937 |
} |
|
938 |
} |