APIdia Launch Partner
Top Description Fields Constructors Methods
jdk.jfr

public final Class EventFactory

extends Object
Class Inheritance
Imports
java.lang.invoke.MethodHandle, .MethodHandles, java.util.ArrayList, .HashSet, .List, .Objects, .Set, jdk.internal.module.Checks, jdk.jfr.internal.EventClassBuilder, .JVMSupport, .MetadataRepository, .Type, jdk.jfr.internal.util.Utils

Class for defining an event at runtime.

It's highly recommended that the event is defined at compile time, if the field layout is known, so the Java Virtual Machine (JVM) can optimize the code, possibly remove all instrumentation if Flight Recorder is inactive or if the enabled setting for this event is set to false.

To define an event at compile time, see Event.

The following example shows how to implement a dynamic Event class.

List<ValueDescriptor> fields = new ArrayList<>(); List<AnnotationElement> messageAnnotations = Collections.singletonList(new AnnotationElement(Label.class, "Message")); fields.add(new ValueDescriptor(String.class, "message", messageAnnotations)); List<AnnotationElement> numberAnnotations = Collections.singletonList(new AnnotationElement(Label.class, "Number")); fields.add(new ValueDescriptor(int.class, "number", numberAnnotations)); String[] category = { "Example", "Getting Started" }; List<AnnotationElement> eventAnnotations = new ArrayList<>(); eventAnnotations.add(new AnnotationElement(Name.class, "com.example.HelloWorld")); eventAnnotations.add(new AnnotationElement(Label.class, "Hello World")); eventAnnotations.add(new AnnotationElement(Description.class, "Helps programmer getting started")); eventAnnotations.add(new AnnotationElement(Category.class, category)); EventFactory f = EventFactory.create(eventAnnotations, fields); Event event = f.newEvent(); event.set(0, "hello, world!"); event.set(1, 4711); event.commit();
List<ValueDescriptor> fields = new ArrayList<>();
List<AnnotationElement> messageAnnotations = Collections.singletonList(new AnnotationElement(Label.class, "Message"));
fields.add(new ValueDescriptor(String.class, "message", messageAnnotations));
List<AnnotationElement> numberAnnotations = Collections.singletonList(new AnnotationElement(Label.class, "Number"));
fields.add(new ValueDescriptor(int.class, "number", numberAnnotations));

String[] category = { "Example", "Getting Started" };
List<AnnotationElement> eventAnnotations = new ArrayList<>();
eventAnnotations.add(new AnnotationElement(Name.class, "com.example.HelloWorld"));
eventAnnotations.add(new AnnotationElement(Label.class, "Hello World"));
eventAnnotations.add(new AnnotationElement(Description.class, "Helps programmer getting started"));
eventAnnotations.add(new AnnotationElement(Category.class, category));

EventFactory f = EventFactory.create(eventAnnotations, fields);

Event event = f.newEvent();
event.set(0, "hello, world!");
event.set(1, 4711);
event.commit();
Since
9

Field Summary

Modifier and TypeField and Description
private final MethodHandle
private final Class<? extends Event>
private static final long
private final List<AnnotationElement>
private final List<ValueDescriptor>

Constructor Summary

AccessConstructor and Description
private
EventFactory(Class<? extends Event> eventClass, List<AnnotationElement> sanitizedAnnotation, List<ValueDescriptor> sanitizedFields)

Method Summary

Modifier and TypeMethod and Description
public static EventFactory

Returns:

event factory, not null
create
(List<AnnotationElement>
list of annotation elements that describes the annotations on the event, not null
annotationElements
,
List<ValueDescriptor>
list of descriptors that describes the fields of the event, not null
fields
)

Creates an EventFactory object.

public EventType

Returns:

event type that is associated with this event factory, not null
getEventType
()

Returns the event type that is associated with this event factory.

public Event

Returns:

an event instance, not null
newEvent
()

Instantiates an event, so it can be populated with data and written to the Flight Recorder system.

public void
register()

Registers an unregistered event.

public void
unregister()

Unregisters the event that is associated with this event factory.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

constructorHandleback to summary
private final MethodHandle constructorHandle
eventClassback to summary
private final Class<? extends Event> eventClass
REGISTERED_IDback to summary
private static final long REGISTERED_ID
sanitizedAnnotationback to summary
private final List<AnnotationElement> sanitizedAnnotation
sanitizedFieldsback to summary
private final List<ValueDescriptor> sanitizedFields

Constructor Detail

EventFactoryback to summary
private EventFactory(Class<? extends Event> eventClass, List<AnnotationElement> sanitizedAnnotation, List<ValueDescriptor> sanitizedFields) throws IllegalAccessException, NoSuchMethodException, SecurityException

Method Detail

createback to summary
public static EventFactory create(List<AnnotationElement> annotationElements, List<ValueDescriptor> fields)

Creates an EventFactory object.

The order of the value descriptors specifies the index to use when setting event values.

Parameters
annotationElements:List<AnnotationElement>

list of annotation elements that describes the annotations on the event, not null

fields:List<ValueDescriptor>

list of descriptors that describes the fields of the event, not null

Returns:EventFactory

event factory, not null

Exceptions
IllegalArgumentException:
if the input is not valid. For example, input might not be valid if the field type or name is not valid in the Java language or an annotation element references a type that can't be found.
See Also
Event#set(int, Object)
getEventTypeback to summary
public EventType getEventType()

Returns the event type that is associated with this event factory.

Returns:EventType

event type that is associated with this event factory, not null

Exceptions
IllegalStateException:
if the event factory is created with the Registered(false) annotation and the event class is not manually registered before the invocation of this method
newEventback to summary
public Event newEvent()

Instantiates an event, so it can be populated with data and written to the Flight Recorder system.

Use the Event#set(int, Object) method to set a value.

Returns:Event

an event instance, not null

registerback to summary
public void register()

Registers an unregistered event.

By default, the event class associated with this event factory is registered when the event factory is created, unless the event has the Registered annotation.

A registered event class can write data to Flight Recorder and event metadata can be obtained by invoking FlightRecorder#getEventTypes().

If the event class associated with this event factory is already registered, the call to this method is ignored.

See Also
Registered, FlightRecorder#register(Class)
unregisterback to summary
public void unregister()

Unregisters the event that is associated with this event factory.

A unregistered event class can't write data to Flight Recorder and event metadata can't be obtained by invoking FlightRecorder#getEventTypes().

If the event class associated with this event factory is not already registered, the call to this method is ignored.

See Also
Registered, FlightRecorder#unregister(Class)