Spec-Zone.ru › OpenJDK 27

Annotation Interface ContentType

@Target(ANNOTATION_TYPE) @Retention(RUNTIME) public @interface ContentType
Meta annotation, specifies that an annotation represents a content type, such as a time span or a frequency.

The following example shows how a temperature content type can be created and used.

First declare a temperature annotation using the ContentType annotation:

@MetadataDefinition
@ContentType
@Name("com.example.Temperature")
@Label("Temperature")
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Temperature {
    public static final String KELVIN = "KELVIN";
    public static final String CELSIUS = "CELSIUS";
    public static final String FAHRENEHIT = "FAHRENHEIT";

    String value() default CELSIUS;
}
Then declare an event, annotate a field and commit the data:
@Name("com.example.CPU")
@Label("CPU")
@Category({ "Hardware", "CPU" })
@Period("1 s")
@StackTrace(false)
public static class CPUEvent extends Event {
    @Label("ID")
    String id;

    @Temperature(Temperature.KELVIN)
    @Label("Temperature")
    float temperature;
}

public static void main(String... args) throws InterruptedException {
    FlightRecorder.addPeriodicEvent(CPUEvent.class, () -> {
        for (var cpu : listCPUs()) {
            CPUEvent event = new CPUEvent();
            event.id = cpu.id();
            event.temperature = cpu.temperature(); // in Kelvin
            event.commit();
        }
    });
    Thread.sleep(10_000);
}
Finally, inspect the annotation when displaying event data:
void printTemperaturesInCelsius(Path file) throws IOException {
    for (RecordedEvent event : RecordingFile.readAllEvents(file)) {
        for (ValueDescriptor field : event.getEventType().getFields()) {
            for (AnnotationElement ae : field.getAnnotationElements()) {
                ContentType type = ae.getAnnotation(ContentType.class);
                if (type != null) {
                    if (ae.getTypeName().equals("com.example.Temperature")) {
                        double value = event.getDouble(field.getName());
                        String unit = (String) ae.getValue("value");
                        double celsius = switch (unit) {
                            case "CELSIUS" -> value;
                            case "KELVIN" -> value - 273.15;
                            case "FAHRENHEIT" -> (value - 32) / 1.8;
                            default -> throw new IllegalStateException("Unknown temperature unit '" + unit + "'");
                        };
                        System.out.println(celsius + " C");
                    } else {
                        System.err.println("Can't format content type " + ae.getTypeName() + " for field " + field.getName());
                    }
                }
            }
        }
    }
}
Since:
9

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2026, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API