Java Code Examples for jdk.jfr.ValueDescriptor#getAnnotation()

The following examples show how to use jdk.jfr.ValueDescriptor#getAnnotation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RecordedObject.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private Duration getDuration(long timespan, String name) throws InternalError {
    ValueDescriptor v = getValueDescriptor(descriptors, name, null);
    Timespan ts = v.getAnnotation(Timespan.class);
    if (ts != null) {
        switch (ts.value()) {
        case Timespan.MICROSECONDS:
            return Duration.ofNanos(1000 * timespan);
        case Timespan.SECONDS:
            return Duration.ofSeconds(timespan);
        case Timespan.MILLISECONDS:
            return Duration.ofMillis(timespan);
        case Timespan.NANOSECONDS:
            return Duration.ofNanos(timespan);
        case Timespan.TICKS:
            return Duration.ofNanos(timeConverter.convertTimespan(timespan));
        }
        throw new IllegalArgumentException("Attempt to get " + v.getTypeName() + " field \"" + name + "\" with illegal timespan unit " + ts.value());
    }
    throw new IllegalArgumentException("Attempt to get " + v.getTypeName() + " field \"" + name + "\" with missing @Timespan");
}
 
Example 2
Source File: MainTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void assertMetadata(List<RecordedEvent> events) {
    for (RecordedEvent e : events) {
        EventType type = e.getEventType();
        ModularizedAnnotation maType = type.getAnnotation(ModularizedAnnotation.class);
        if (maType == null) {
            fail("Missing @ModularizedAnnotation on type " + type);
        }
        assertEquals(maType.value(), "hello type");
        assertMetaAnnotation(type.getAnnotationElements());

        ValueDescriptor messageField = type.getField("message");
        ModularizedAnnotation maField = messageField.getAnnotation(ModularizedAnnotation.class);
        if (maField == null) {
            fail("Missing @ModularizedAnnotation on field in " + type);
        }
        assertEquals(maField.value(), "hello field");
        assertMetaAnnotation(messageField.getAnnotationElements());
    }
}
 
Example 3
Source File: TestConstructor.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    ValueDescriptor vdSimple = new ValueDescriptor(String.class, "message");
    Asserts.assertNull(vdSimple.getAnnotation(Label.class), "Expected getAnnotation()==null");
    Asserts.assertEquals(0, vdSimple.getAnnotationElements().size(), "Expected getAnnotations().size() == 0");

    // Add labelA and verify we can read it back
    List<AnnotationElement> annos = new ArrayList<>();
    AnnotationElement labelA = new AnnotationElement(Label.class, "labelA");
    annos.add(labelA);
    System.out.println("labelA.getClass()" + labelA.getClass());
    ValueDescriptor vdComplex = new ValueDescriptor(String.class, "message", annos);

    final Label outLabel = vdComplex.getAnnotation(Label.class);
    Asserts.assertFalse(outLabel == null, "getLabel(Label.class) was null");
    System.out.println("outLabel.value() = " + outLabel.value());

    // Get labelA from getAnnotations() list
    Asserts.assertEquals(1, vdComplex.getAnnotationElements().size(), "Expected getAnnotations().size() == 1");
    final AnnotationElement outAnnotation = vdComplex.getAnnotationElements().get(0);
    Asserts.assertNotNull(outAnnotation, "outAnnotation was null");
    System.out.printf("Annotation: %s = %s%n", outAnnotation.getTypeName(), outAnnotation.getValue("value"));
    Asserts.assertEquals(outAnnotation, labelA, "Expected firstAnnotation == labelA");

}
 
Example 4
Source File: RecordedObject.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private Duration getDuration(long timespan, String name) throws InternalError {
    ValueDescriptor v = getValueDescriptor(descriptors, name, null);
    if (timespan == Long.MIN_VALUE) {
        return Duration.ofSeconds(Long.MIN_VALUE, 0);
    }
    Timespan ts = v.getAnnotation(Timespan.class);
    if (ts != null) {
        switch (ts.value()) {
        case Timespan.MICROSECONDS:
            return Duration.ofNanos(1000 * timespan);
        case Timespan.SECONDS:
            return Duration.ofSeconds(timespan);
        case Timespan.MILLISECONDS:
            return Duration.ofMillis(timespan);
        case Timespan.NANOSECONDS:
            return Duration.ofNanos(timespan);
        case Timespan.TICKS:
            return Duration.ofNanos(timeConverter.convertTimespan(timespan));
        }
        throw new IllegalArgumentException("Attempt to get " + v.getTypeName() + " field \"" + name + "\" with illegal timespan unit " + ts.value());
    }
    throw new IllegalArgumentException("Attempt to get " + v.getTypeName() + " field \"" + name + "\" with missing @Timespan");
}
 
Example 5
Source File: RecordedObject.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private Instant getInstant(long timestamp, String name) {
    ValueDescriptor v = getValueDescriptor(descriptors, name, null);
    Timestamp ts = v.getAnnotation(Timestamp.class);
    if (ts != null) {
        if (timestamp == Long.MIN_VALUE) {
            return Instant.MIN;
        }
        switch (ts.value()) {
        case Timestamp.MILLISECONDS_SINCE_EPOCH:
            return Instant.ofEpochMilli(timestamp);
        case Timestamp.TICKS:
            return Instant.ofEpochSecond(0, timeConverter.convertTimestamp(timestamp));
        }
        throw new IllegalArgumentException("Attempt to get " + v.getTypeName() + " field \"" + name + "\" with illegal timestamp unit " + ts.value());
    }
    throw new IllegalArgumentException("Attempt to get " + v.getTypeName() + " field \"" + name + "\" with missing @Timestamp");
}
 
Example 6
Source File: TestConstructor.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    ValueDescriptor vdSimple = new ValueDescriptor(String.class, "message");
    Asserts.assertNull(vdSimple.getAnnotation(Label.class), "Expected getAnnotation()==null");
    Asserts.assertEquals(0, vdSimple.getAnnotationElements().size(), "Expected getAnnotations().size() == 0");

    // Add labelA and verify we can read it back
    List<AnnotationElement> annos = new ArrayList<>();
    AnnotationElement labelA = new AnnotationElement(Label.class, "labelA");
    annos.add(labelA);
    System.out.println("labelA.getClass()" + labelA.getClass());
    ValueDescriptor vdComplex = new ValueDescriptor(String.class, "message", annos);

    final Label outLabel = vdComplex.getAnnotation(Label.class);
    Asserts.assertFalse(outLabel == null, "getLabel(Label.class) was null");
    System.out.println("outLabel.value() = " + outLabel.value());

    // Get labelA from getAnnotations() list
    Asserts.assertEquals(1, vdComplex.getAnnotationElements().size(), "Expected getAnnotations().size() == 1");
    final AnnotationElement outAnnotation = vdComplex.getAnnotationElements().get(0);
    Asserts.assertNotNull(outAnnotation, "outAnnotation was null");
    System.out.printf("Annotation: %s = %s%n", outAnnotation.getTypeName(), outAnnotation.getValue("value"));
    Asserts.assertEquals(outAnnotation, labelA, "Expected firstAnnotation == labelA");

}
 
Example 7
Source File: MainTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static void assertMetadata(List<RecordedEvent> events) {
    for (RecordedEvent e : events) {
        EventType type = e.getEventType();
        ModularizedAnnotation maType = type.getAnnotation(ModularizedAnnotation.class);
        if (maType == null) {
            fail("Missing @ModularizedAnnotation on type " + type);
        }
        assertEquals(maType.value(), "hello type");
        assertMetaAnnotation(type.getAnnotationElements());

        ValueDescriptor messageField = type.getField("message");
        ModularizedAnnotation maField = messageField.getAnnotation(ModularizedAnnotation.class);
        if (maField == null) {
            fail("Missing @ModularizedAnnotation on field in " + type);
        }
        assertEquals(maField.value(), "hello field");
        assertMetaAnnotation(messageField.getAnnotationElements());
    }
}
 
Example 8
Source File: MainTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void assertMetadata(List<RecordedEvent> events) {
    for (RecordedEvent e : events) {
        EventType type = e.getEventType();
        ModularizedAnnotation maType = type.getAnnotation(ModularizedAnnotation.class);
        if (maType == null) {
            fail("Missing @ModularizedAnnotation on type " + type);
        }
        assertEquals(maType.value(), "hello type");
        assertMetaAnnotation(type.getAnnotationElements());

        ValueDescriptor messageField = type.getField("message");
        ModularizedAnnotation maField = messageField.getAnnotation(ModularizedAnnotation.class);
        if (maField == null) {
            fail("Missing @ModularizedAnnotation on field in " + type);
        }
        assertEquals(maField.value(), "hello field");
        assertMetaAnnotation(messageField.getAnnotationElements());
    }
}
 
Example 9
Source File: TestConstructor.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    ValueDescriptor vdSimple = new ValueDescriptor(String.class, "message");
    Asserts.assertNull(vdSimple.getAnnotation(Label.class), "Expected getAnnotation()==null");
    Asserts.assertEquals(0, vdSimple.getAnnotationElements().size(), "Expected getAnnotations().size() == 0");

    // Add labelA and verify we can read it back
    List<AnnotationElement> annos = new ArrayList<>();
    AnnotationElement labelA = new AnnotationElement(Label.class, "labelA");
    annos.add(labelA);
    System.out.println("labelA.getClass()" + labelA.getClass());
    ValueDescriptor vdComplex = new ValueDescriptor(String.class, "message", annos);

    final Label outLabel = vdComplex.getAnnotation(Label.class);
    Asserts.assertFalse(outLabel == null, "getLabel(Label.class) was null");
    System.out.println("outLabel.value() = " + outLabel.value());

    // Get labelA from getAnnotations() list
    Asserts.assertEquals(1, vdComplex.getAnnotationElements().size(), "Expected getAnnotations().size() == 1");
    final AnnotationElement outAnnotation = vdComplex.getAnnotationElements().get(0);
    Asserts.assertNotNull(outAnnotation, "outAnnotation was null");
    System.out.printf("Annotation: %s = %s%n", outAnnotation.getTypeName(), outAnnotation.getValue("value"));
    Asserts.assertEquals(outAnnotation, labelA, "Expected firstAnnotation == labelA");

}
 
Example 10
Source File: RecordedObject.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private Duration getDuration(long timespan, String name) throws InternalError {
    ValueDescriptor v = getValueDescriptor(descriptors, name, null);
    if (timespan == Long.MIN_VALUE) {
        return Duration.ofSeconds(Long.MIN_VALUE, 0);
    }
    Timespan ts = v.getAnnotation(Timespan.class);
    if (ts != null) {
        switch (ts.value()) {
        case Timespan.MICROSECONDS:
            return Duration.ofNanos(1000 * timespan);
        case Timespan.SECONDS:
            return Duration.ofSeconds(timespan);
        case Timespan.MILLISECONDS:
            return Duration.ofMillis(timespan);
        case Timespan.NANOSECONDS:
            return Duration.ofNanos(timespan);
        case Timespan.TICKS:
            return Duration.ofNanos(timeConverter.convertTimespan(timespan));
        }
        throw new IllegalArgumentException("Attempt to get " + v.getTypeName() + " field \"" + name + "\" with illegal timespan unit " + ts.value());
    }
    throw new IllegalArgumentException("Attempt to get " + v.getTypeName() + " field \"" + name + "\" with missing @Timespan");
}
 
Example 11
Source File: RecordedObject.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private Instant getInstant(long timestamp, String name) {
    ValueDescriptor v = getValueDescriptor(descriptors, name, null);
    Timestamp ts = v.getAnnotation(Timestamp.class);
    if (ts != null) {
        if (timestamp == Long.MIN_VALUE) {
            return Instant.MIN;
        }
        switch (ts.value()) {
        case Timestamp.MILLISECONDS_SINCE_EPOCH:
            return Instant.ofEpochMilli(timestamp);
        case Timestamp.TICKS:
            return Instant.ofEpochSecond(0, timeConverter.convertTimestamp(timestamp));
        }
        throw new IllegalArgumentException("Attempt to get " + v.getTypeName() + " field \"" + name + "\" with illegal timestamp unit " + ts.value());
    }
    throw new IllegalArgumentException("Attempt to get " + v.getTypeName() + " field \"" + name + "\" with missing @Timestamp");
}
 
Example 12
Source File: RecordedObject.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private Instant getInstant(long timestamp, String name) {
    ValueDescriptor v = getValueDescriptor(descriptors, name, null);
    Timestamp ts = v.getAnnotation(Timestamp.class);
    if (ts != null) {
        switch (ts.value()) {
        case Timestamp.MILLISECONDS_SINCE_EPOCH:
            return Instant.ofEpochMilli(timestamp);
        case Timestamp.TICKS:
            return Instant.ofEpochSecond(0, timeConverter.convertTimestamp(timestamp));
        }
        throw new IllegalArgumentException("Attempt to get " + v.getTypeName() + " field \"" + name + "\" with illegal timestamp unit " + ts.value());
    }
    throw new IllegalArgumentException("Attempt to get " + v.getTypeName() + " field \"" + name + "\" with missing @Timestamp");
}
 
Example 13
Source File: EventPrintWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private ValueType determineValueType(ValueDescriptor v) {
    if (v.getAnnotation(Timespan.class) != null) {
        return ValueType.TIMESPAN;
    }
    if (v.getAnnotation(Timestamp.class) != null) {
        return ValueType.TIMESTAMP;
    }
    return ValueType.OTHER;
}
 
Example 14
Source File: EventPrintWriter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private ValueType determineValueType(ValueDescriptor v) {
    if (v.getAnnotation(Timespan.class) != null) {
        return ValueType.TIMESPAN;
    }
    if (v.getAnnotation(Timestamp.class) != null) {
        return ValueType.TIMESTAMP;
    }
    return ValueType.OTHER;
}
 
Example 15
Source File: TestPrintXML.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
static boolean compare(Object eventObject, Object xmlObject) {
    if (eventObject == null) {
        return xmlObject == null;
    }
    if (eventObject instanceof RecordedObject) {
        RecordedObject re = (RecordedObject) eventObject;
        Map<String, Object> xmlMap = (Map<String, Object>) xmlObject;
        List<ValueDescriptor> fields = re.getFields();
        if (fields.size() != xmlMap.size()) {
            return false;
        }
        for (ValueDescriptor v : fields) {
            String name = v.getName();
            Object xmlValue = xmlMap.get(name);
            Object expectedValue = re.getValue(name);
            if (v.getAnnotation(Timestamp.class) != null) {
                // Make instant of OffsetDateTime
                xmlValue = OffsetDateTime.parse("" + xmlValue).toInstant().toString();
                expectedValue = re.getInstant(name);
            }
            if (v.getAnnotation(Timespan.class) != null) {
                expectedValue = re.getDuration(name);
            }
            if (!compare(expectedValue, xmlValue)) {
                return false;
            }
        }
        return true;
    }
    if (eventObject.getClass().isArray()) {
        Object[] array = (Object[]) eventObject;
        Object[] xmlArray = (Object[]) xmlObject;
        if (array.length != xmlArray.length) {
            return false;
        }
        for (int i = 0; i < array.length; i++) {
            if (!compare(array[i], xmlArray[i])) {
                return false;
            }
        }
        return true;
    }
    String s1 = String.valueOf(eventObject);
    String s2 = (String) xmlObject;
    return s1.equals(s2);
}
 
Example 16
Source File: TestPrintXML.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
static boolean compare(Object eventObject, Object xmlObject) {
    if (eventObject == null) {
        return xmlObject == null;
    }
    if (eventObject instanceof RecordedObject) {
        RecordedObject re = (RecordedObject) eventObject;
        Map<String, Object> xmlMap = (Map<String, Object>) xmlObject;
        List<ValueDescriptor> fields = re.getFields();
        if (fields.size() != xmlMap.size()) {
            return false;
        }
        for (ValueDescriptor v : fields) {
            String name = v.getName();
            Object xmlValue = xmlMap.get(name);
            Object expectedValue = re.getValue(name);
            if (v.getAnnotation(Timestamp.class) != null) {
                // Make instant of OffsetDateTime
                xmlValue = OffsetDateTime.parse("" + xmlValue).toInstant().toString();
                expectedValue = re.getInstant(name);
            }
            if (v.getAnnotation(Timespan.class) != null) {
                expectedValue = re.getDuration(name);
            }
            if (!compare(expectedValue, xmlValue)) {
                return false;
            }
        }
        return true;
    }
    if (eventObject.getClass().isArray()) {
        Object[] array = (Object[]) eventObject;
        Object[] xmlArray = (Object[]) xmlObject;
        if (array.length != xmlArray.length) {
            return false;
        }
        for (int i = 0; i < array.length; i++) {
            if (!compare(array[i], xmlArray[i])) {
                return false;
            }
        }
        return true;
    }
    String s1 = String.valueOf(eventObject);
    String s2 = (String) xmlObject;
    return s1.equals(s2);
}
 
Example 17
Source File: DisplayableSupport.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
private static boolean isExperimental(ValueDescriptor descriptor) {
    return descriptor.getAnnotation(Experimental.class) != null;
}
 
Example 18
Source File: DisplayableSupport.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
@Override
Format createFormat(ValueDescriptor descriptor, Frequency annotation) {
    FormatProcessor dataAmountProcessor = FORMAT_PROCESSORS[FORMAT_PROCESSORS.length - 1];
    Annotation dataAmountAnnotation = descriptor.getAnnotation(dataAmountProcessor.getType());
    return new FrequencyFormat(dataAmountAnnotation == null ? null : dataAmountProcessor.createFormat(descriptor, dataAmountAnnotation));
}