src/jdk.jfr/share/classes/jdk/jfr/AnnotationElement.java
author egahlin
Tue, 15 May 2018 20:24:34 +0200
changeset 50113 caf115bb98ad
permissions -rw-r--r--
8199712: Flight Recorder Reviewed-by: coleenp, ihse, erikj, dsamersoff, mseledtsov, egahlin, mgronlun Contributed-by: erik.gahlin@oracle.com, markus.gronlund@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     1
/*
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     2
 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
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;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    27
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    28
import java.lang.annotation.Annotation;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    29
import java.lang.reflect.Method;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    30
import java.util.ArrayList;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    31
import java.util.Collections;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    32
import java.util.HashMap;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    33
import java.util.HashSet;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    34
import java.util.List;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    35
import java.util.Map;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    36
import java.util.Objects;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    37
import java.util.Set;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    38
import java.util.StringJoiner;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    39
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    40
import jdk.jfr.internal.Type;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    41
import jdk.jfr.internal.TypeLibrary;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    42
import jdk.jfr.internal.Utils;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    43
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    44
/**
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    45
 * Describes event metadata, such as labels, descriptions and units.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    46
 * <p>
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    47
 * The following example shows how {@code AnnotationElement} can be used to dynamically define events.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    48
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    49
 * <pre>
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    50
 * <code>
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    51
 *   List{@literal <}AnnotationElement{@literal >} typeAnnotations = new ArrayList{@literal <}{@literal >}();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    52
 *   typeannotations.add(new AnnotationElement(Name.class, "com.example.HelloWorld");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    53
 *   typeAnnotations.add(new AnnotationElement(Label.class, "Hello World"));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    54
 *   typeAnnotations.add(new AnnotationElement(Description.class, "Helps programmer getting started"));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    55
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    56
 *   List{@literal <}AnnotationElement{@literal >} fieldAnnotations = new ArrayList{@literal <}{@literal >}();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    57
 *   fieldAnnotations.add(new AnnotationElement(Label.class, "Message"));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    58
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    59
 *   List{@literal <}ValueDescriptor{@literal >} fields = new ArrayList{@literal <}{@literal >}();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    60
 *   fields.add(new ValueDescriptor(String.class, "message", fieldAnnotations));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    61
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    62
 *   EventFactory f = EventFactory.create(typeAnnotations, fields);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    63
 *   Event event = f.newEvent();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    64
 *   event.commit();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    65
 * </code>
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    66
 * </pre>
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    67
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    68
 * @since 9
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    69
 */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    70
public final class AnnotationElement {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    71
    private final Type type;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    72
    private final List<Object> annotationValues;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    73
    private final List<String> annotationNames;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    74
    private final boolean inBootClassLoader;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    75
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    76
    // package private
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    77
    AnnotationElement(Type type, List<Object> objects, boolean boot) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    78
        Objects.requireNonNull(type);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    79
        Objects.requireNonNull(objects);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    80
        this.type = type;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    81
        if (objects.size() != type.getFields().size()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    82
            StringJoiner descriptors = new StringJoiner(",", "[", "]");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    83
            for (ValueDescriptor v : type.getFields()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    84
                descriptors.add(v.getName());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    85
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    86
            StringJoiner values = new StringJoiner(",", "[", "]");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    87
            for (Object object : objects) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    88
                descriptors.add(String.valueOf(object));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    89
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    90
            throw new IllegalArgumentException("Annotation " + descriptors + " for " + type.getName() + " doesn't match number of values " + values);
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
        List<String> n = new ArrayList<>();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    94
        List<Object> v = new ArrayList<>();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    95
        int index = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    96
        for (ValueDescriptor valueDescriptor : type.getFields()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    97
            Object object = objects.get(index);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    98
            if (object == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    99
                throw new IllegalArgumentException("Annotation value can't be null");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   100
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   101
            Class<?> valueType = object.getClass();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   102
            if (valueDescriptor.isArray()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   103
                valueType = valueType.getComponentType();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   104
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   105
            checkType(Utils.unboxType(valueType));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   106
            n.add(valueDescriptor.getName());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   107
            v.add(object);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   108
            index++;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   109
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   110
        this.annotationValues = Utils.smallUnmodifiable(v);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   111
        this.annotationNames = Utils.smallUnmodifiable(n);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   112
        this.inBootClassLoader = boot;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   113
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   114
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   115
    /**
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   116
     * Creates an annotation element to use for dynamically defined events.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   117
     * <p>
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   118
     * Supported value types are {@code byte}, {@code int}, {@code short},
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   119
     * {@code long}, {@code double}, {@code float}, {@code boolean}, {@code char},
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   120
     * and {@code String}. Enums, arrays and classes, are not supported.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   121
     * <p>
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   122
     * If {@code annotationType} has annotations (directly present, indirectly
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   123
     * present, or associated), then those annotation are recursively included.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   124
     * However, both the {@code annotationType} and any annotation found recursively
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   125
     * must have the {@link MetadataDefinition} annotation.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   126
     * <p>
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   127
     * To statically define events, see {@link Event} class.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   128
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   129
     * @param annotationType interface extending
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   130
     *        {@code java.lang.annotation.Annotation}, not {@code null}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   131
     * @param values a {@code Map} with keys that match method names of the specified
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   132
     *        annotation interface
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   133
     * @throws IllegalArgumentException if value/key is {@code null}, an unsupported
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   134
     *         value type is used, or a value/key is used that doesn't match the
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   135
     *         signatures in the {@code annotationType}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   136
     */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   137
    public AnnotationElement(Class<? extends Annotation> annotationType, Map<String, Object> values) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   138
        Objects.requireNonNull(annotationType);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   139
        Objects.requireNonNull(values);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   140
        Utils.checkRegisterPermission();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   141
        // copy values to avoid modification after validation
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   142
        HashMap<String, Object> map = new HashMap<>(values);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   143
        for (Map.Entry<String, Object> entry : map.entrySet()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   144
            if (entry.getKey() == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   145
                throw new NullPointerException("Name of annotation method can't be null");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   146
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   147
            if (entry.getValue() == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   148
                throw new NullPointerException("Return value for annotation method can't be null");
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
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   152
        if (AnnotationElement.class.isAssignableFrom(annotationType) && annotationType.isInterface()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   153
            throw new IllegalArgumentException("Must be interface extending " + Annotation.class.getName());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   154
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   155
        if (!isKnownJFRAnnotation(annotationType) && annotationType.getAnnotation(MetadataDefinition.class) == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   156
            throw new IllegalArgumentException("Annotation class must be annotated with jdk.jfr.MetadataDefinition to be valid");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   157
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   158
        if (isKnownJFRAnnotation(annotationType)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   159
            this.type = new Type(annotationType.getCanonicalName(), Type.SUPER_TYPE_ANNOTATION, Type.getTypeId(annotationType));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   160
        } else {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   161
            this.type = TypeLibrary.createAnnotationType(annotationType);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   162
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   163
        Method[] methods = annotationType.getDeclaredMethods();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   164
        if (methods.length != map.size()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   165
            throw new IllegalArgumentException("Number of declared methods must match size of value map");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   166
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   167
        List<String> n = new ArrayList<>();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   168
        List<Object> v = new ArrayList<>();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   169
        Set<String> nameSet = new HashSet<>();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   170
        for (Method method : methods) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   171
            String fieldName = method.getName();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   172
            Object object = map.get(fieldName);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   173
            if (object == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   174
                throw new IllegalArgumentException("No method in annotation interface " + annotationType.getName() + " matching name " + fieldName);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   175
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   176
            Class<?> fieldType = object.getClass();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   177
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   178
            if (fieldType == Class.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   179
                throw new IllegalArgumentException("Annotation value for " + fieldName + " can't be class");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   180
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   181
            if (object instanceof Enum) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   182
                throw new IllegalArgumentException("Annotation value for " + fieldName + " can't be enum");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   183
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   184
            if (!fieldType.equals(object.getClass())) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   185
                throw new IllegalArgumentException("Return type of annotation " + fieldType.getName() + " must match type of object" + object.getClass());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   186
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   187
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   188
            if (fieldType.isArray()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   189
                Class<?> componentType = fieldType.getComponentType();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   190
                checkType(componentType);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   191
                if (componentType.equals(String.class)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   192
                    String[] stringArray = (String[]) object;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   193
                    for (int i = 0; i < stringArray.length; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   194
                        if (stringArray[i] == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   195
                            throw new IllegalArgumentException("Annotation value for " + fieldName + " contains null");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   196
                        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   197
                    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   198
                }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   199
            } else {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   200
                fieldType = Utils.unboxType(object.getClass());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   201
                checkType(fieldType);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   202
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   203
            if (nameSet.contains(fieldName)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   204
                throw new IllegalArgumentException("Value with name '" + fieldName + "' already exists");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   205
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   206
            if (isKnownJFRAnnotation(annotationType)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   207
                ValueDescriptor vd = new ValueDescriptor(fieldType, fieldName, Collections.emptyList(), true);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   208
                type.add(vd);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   209
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   210
            n.add(fieldName);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   211
            v.add(object);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   212
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   213
        this.annotationValues = Utils.smallUnmodifiable(v);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   214
        this.annotationNames = Utils.smallUnmodifiable(n);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   215
        this.inBootClassLoader = annotationType.getClassLoader() == null;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   216
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   217
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   218
    /**
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   219
     * Creates an annotation element to use for dynamically defined events.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   220
     * <p>
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   221
     * Supported value types are {@code byte}, {@code int}, {@code short},
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   222
     * {@code long}, {@code double}, {@code float}, {@code boolean}, {@code char},
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   223
     * and {@code String}. Enums, arrays, and classes are not supported.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   224
     * <p>
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   225
     * If {@code annotationType} has annotations (directly present, indirectly
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   226
     * present, or associated), then those annotations are recursively included.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   227
     * However, both {@code annotationType} and any annotation found recursively
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   228
     * must have the {@link MetadataDefinition} annotation.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   229
     * <p>
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   230
     * To statically define events, see {@link Event} class.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   231
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   232
     * @param annotationType interface extending
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   233
     *        {@code java.lang.annotation.Annotation,} not {@code null}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   234
     * @param value the value that matches the {@code value} method of the specified
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   235
     *        {@code annotationType}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   236
     * @throws IllegalArgumentException if value/key is {@code null}, an unsupported
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   237
     *         value type is used, or a value/key is used that doesn't match the
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   238
     *         signatures in the {@code annotationType}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   239
     */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   240
    public AnnotationElement(Class<? extends Annotation> annotationType, Object value) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   241
        this(annotationType, Collections.singletonMap("value", Objects.requireNonNull(value)));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   242
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   243
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   244
    /**
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   245
     * Creates an annotation element to use for dynamically defined events.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   246
     * <p>
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   247
     * Supported value types are {@code byte}, {@code short}, {@code int},
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   248
     * {@code long}, {@code double}, {@code float}, {@code boolean}, {@code char},
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   249
     * and {@code String}. Enums, arrays, and classes are not supported.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   250
     * <p>
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   251
     * If {@code annotationType} has annotations (directly present, indirectly
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   252
     * present or associated), then those annotation are recursively included.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   253
     * However, both {@code annotationType} and any annotation found recursively
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   254
     * must have the {@link MetadataDefinition} annotation.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   255
     * <p>
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   256
     * To statically define events, see {@link Event} class.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   257
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   258
     * @param annotationType interface extending java.lang.annotation.Annotation,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   259
     *        not {@code null}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   260
     */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   261
    public AnnotationElement(Class<? extends Annotation> annotationType) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   262
        this(annotationType, Collections.emptyMap());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   263
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   264
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   265
    /**
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   266
     * Returns an immutable list of annotation values in an order that matches the
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   267
     * value descriptors for this {@code AnnotationElement}.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   268
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   269
     * @return list of values, not {@code null}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   270
     */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   271
    public List<Object> getValues() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   272
        return annotationValues;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   273
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   274
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   275
    /**
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   276
     * Returns an immutable list of descriptors that describes the annotation values
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   277
     * for this {@code AnnotationElement}.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   278
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   279
     * @return the list of value descriptors for this {@code Annotation}, not
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   280
     *         {@code null}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   281
     */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   282
    public List<ValueDescriptor> getValueDescriptors() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   283
        return Collections.unmodifiableList(type.getFields());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   284
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   285
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   286
    /**
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   287
     * Returns an immutable list of annotation elements for this
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   288
     * {@code AnnotationElement}.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   289
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   290
     * @return a list of meta annotation, not {@code null}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   291
     */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   292
    public List<AnnotationElement> getAnnotationElements() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   293
        return type.getAnnotationElements();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   294
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   295
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   296
    /**
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   297
     * Returns the fully qualified name of the annotation type that corresponds to
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   298
     * this {@code AnnotationElement} (for example, {@code "jdk.jfr.Label"}).
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   299
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   300
     * @return type name, not {@code null}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   301
     */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   302
    public String getTypeName() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   303
        return type.getName();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   304
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   305
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   306
    /**
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   307
     * Returns a value for this {@code AnnotationElement}.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   308
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   309
     * @param name the name of the method in the annotation interface, not
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   310
     *        {@code null}.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   311
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   312
     * @return the annotation value, not {@code null}.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   313
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   314
     * @throws IllegalArgumentException if a method with the specified name does
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   315
     *         not exist in the annotation
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   316
     */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   317
    public Object getValue(String name) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   318
        Objects.requireNonNull(name);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   319
        int index = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   320
        for (String n : annotationNames) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   321
            if (name.equals(n)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   322
                return annotationValues.get(index);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   323
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   324
            index++;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   325
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   326
        StringJoiner valueNames = new StringJoiner(",", "[", "]");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   327
        for (ValueDescriptor v : type.getFields()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   328
            valueNames.add(v.getName());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   329
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   330
        throw new IllegalArgumentException("No value with name '" + name + "'. Valid names are " + valueNames);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   331
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   332
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   333
    /**
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   334
     * Returns {@code true} if an annotation value with the specified name exists in
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   335
     * this {@code AnnotationElement}.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   336
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   337
     * @param name name of the method in the annotation interface to find, not
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   338
     *        {@code null}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   339
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   340
     * @return {@code true} if method exists, {@code false} otherwise
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   341
     */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   342
    public boolean hasValue(String name) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   343
        Objects.requireNonNull(name);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   344
        for (String n : annotationNames) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   345
            if (name.equals(n)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   346
                return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   347
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   348
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   349
        return false;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   350
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   351
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   352
    /**
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   353
     * Returns the first annotation for the specified type if an
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   354
     * {@code AnnotationElement} with the same name exists, else {@code null}.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   355
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   356
     * @param <A> the type of the annotation to query for and return if it exists
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   357
     * @param annotationType the {@code Class object} corresponding to the annotation type,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   358
     *        not {@code null}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   359
     * @return this element's annotation for the specified annotation type if
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   360
     *         it it exists, else {@code null}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   361
     */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   362
    public final <A> A getAnnotation(Class<? extends Annotation> annotationType) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   363
        Objects.requireNonNull(annotationType);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   364
        return type.getAnnotation(annotationType);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   365
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   366
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   367
    /**
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   368
     * Returns the type ID for this {@code AnnotationElement}.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   369
     * <p>
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   370
     * The ID is a unique identifier for the type in the Java Virtual Machine (JVM). The ID might not
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   371
     * be the same between JVM instances.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   372
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   373
     * @return the type ID, not negative
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   374
     */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   375
    public long getTypeId() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   376
        return type.getId();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   377
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   378
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   379
    // package private
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   380
    Type getType() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   381
        return type;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   382
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   383
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   384
    private static void checkType(Class<?> type) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   385
        if (type.isPrimitive()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   386
            return;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   387
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   388
        if (type == String.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   389
            return;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   390
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   391
        throw new IllegalArgumentException("Only primitives types or java.lang.String are allowed");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   392
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   393
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   394
    // Whitelist of annotation classes that are allowed, even though
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   395
    // they don't have @MetadataDefinition.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   396
    private static boolean isKnownJFRAnnotation(Class<? extends Annotation> annotationType) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   397
        if (annotationType == Registered.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   398
            return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   399
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   400
        if (annotationType == Threshold.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   401
            return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   402
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   403
        if (annotationType == StackTrace.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   404
            return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   405
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   406
        if (annotationType == Period.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   407
            return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   408
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   409
        if (annotationType == Enabled.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   410
            return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   411
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   412
        return false;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   413
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   414
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   415
    // package private
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   416
    boolean isInBoot() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   417
        return inBootClassLoader;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   418
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   419
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   420
}