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