- java.lang.Object
-
- jdk.jfr.AnnotationElement
-
public final class AnnotationElement extends Object
Describes event metadata, such as labels, descriptions and units.An
AnnotationElement
carries metadata of already defined events, but can also be used to define events dynamically, like this.List<AnnotationElement> typeAnnotations = new ArrayList<>(); typeannotations.add(new AnnotationElement(Name.class, "com.example.HelloWorld"); typeAnnotations.add(new AnnotationElement(Label.class, "Hello World")); typeAnnotations.add(new AnnotationElement(Description.class, "Helps programmer getting started")); List<AnnotationElement> fieldAnnotations = new ArrayList<>(); fieldAnnotations.add(new AnnotationElement(Label.class, "Message")); List<ValueDescriptor> fields = new ArrayList<>(); fields.add(new ValueDescriptor(String.class, "message", fieldAnnotations)); EventFactory f = EventFactory.create(typeAnnotations, fields); Event event = f.newEvent(); event.commit();
- Since:
- 9
-
-
Constructor Summary
Constructors Constructor Description AnnotationElement(Class<? extends Annotation> annotationType)
Creates an annotation element to be used when defining events dynamically.AnnotationElement(Class<? extends Annotation> annotationType, Object value)
Creates an annotation element to be used when defining events dynamically.AnnotationElement(Class<? extends Annotation> annotationType, Map<String,Object> values)
Creates an annotation element to be used when defining events dynamically.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <A> A
getAnnotation(Class<? extends Annotation> annotationType)
Returns the first annotation for the specified type if an annotation element with the same name is available, elsenull
.List<AnnotationElement>
getAnnotationElements()
Returns an immutable list of annotation elements for this annotation element.long
getTypeId()
Returns the type id for this annotation element.String
getTypeName()
Returns the fully qualified name of the annotation type corresponding to this annotation element.Object
getValue(String name)
Returns a value for this annotation element.List<ValueDescriptor>
getValueDescriptors()
Returns an immutable list of descriptors describing annotation values for thisAnnotationElement
.List<Object>
getValues()
Returns an immutable list of annotation values in an order matching the value descriptors for thisAnnotationElement
.boolean
hasValue(String name)
Returnstrue
if an annotation value with the specified name exists in this annotation element.
-
-
-
Constructor Detail
-
AnnotationElement
public AnnotationElement(Class<? extends Annotation> annotationType, Map<String,Object> values)
Creates an annotation element to be used when defining events dynamically.Supported value types are
byte
,int
,short
,long
,double
,float
,boolean
,char
andString
. Enums, arrays and classes are not supported.If
annotationType
carries annotations (directly present, indirectly present or associated), those will be recursively included. However, both theannotationType
and any annotation found recursively must have theMetadataDefinition
annotation.To define events statically, see
Event
class.- Parameters:
annotationType
- interface extending java.lang.annotation.Annotation, notnull
values
- a map with keys matching the method names of the specified annotation interface- Throws:
IllegalArgumentException
- if value/key isnull
, an unsupported value type is used or a value/key that doesn't match the signatures in the annotation type
-
AnnotationElement
public AnnotationElement(Class<? extends Annotation> annotationType, Object value)
Creates an annotation element to be used when defining events dynamically.Supported value types are
byte
,int
,short
,long
,double
,float
,boolean
,char
andString
. Enums, arrays and classes are not supported.If
annotationType
carries annotations (directly present, indirectly present or associated), those will be recursively included. However, both theannotationType
and any annotation found recursively must have theMetadataDefinition
annotation.To define events statically, see
Event
class.- Parameters:
annotationType
- interface extending java.lang.annotation.Annotation, notnull
value
- the value that should match thevalue
method of the specifiedannotationType
- Throws:
IllegalArgumentException
- if value/key isnull
, an unsupported value type is used or a value/key that doesn't match the signatures in the annotation type
-
AnnotationElement
public AnnotationElement(Class<? extends Annotation> annotationType)
Creates an annotation element to be used when defining events dynamically.Supported value types are
byte
,short
,int
,long
,double
,float
,boolean
,char
andString
. Enums, arrays and classes are not supported.If
annotationType
carries annotations (directly present, indirectly present or associated), those will be recursively included. However, both theannotationType
and any annotation found recursively must have theMetadataDefinition
annotation.To define events statically, see
Event
class.- Parameters:
annotationType
- interface extending java.lang.annotation.Annotation, notnull
-
-
Method Detail
-
getValues
public List<Object> getValues()
Returns an immutable list of annotation values in an order matching the value descriptors for thisAnnotationElement
.- Returns:
- list of values, not
null
-
getValueDescriptors
public List<ValueDescriptor> getValueDescriptors()
Returns an immutable list of descriptors describing annotation values for thisAnnotationElement
.- Returns:
- the list of value descriptor for this
Annotation
, notnull
-
getAnnotationElements
public List<AnnotationElement> getAnnotationElements()
Returns an immutable list of annotation elements for this annotation element.- Returns:
- a list of meta annotation, not
null
-
getTypeName
public String getTypeName()
Returns the fully qualified name of the annotation type corresponding to this annotation element.- Returns:
- type name, not
null
-
getValue
public Object getValue(String name)
Returns a value for this annotation element.- Parameters:
name
- the name of the method in the annotation interface, notnull
.- Returns:
- the annotation value, not
null
. - Throws:
IllegalArgumentException
- if a method with the name could not be located in the annotation
-
hasValue
public boolean hasValue(String name)
Returnstrue
if an annotation value with the specified name exists in this annotation element.- Parameters:
name
- name of the method in the annotation interface to check for, notnull
- Returns:
true
if method exists,false
otherwise
-
getAnnotation
public final <A> A getAnnotation(Class<? extends Annotation> annotationType)
Returns the first annotation for the specified type if an annotation element with the same name is available, elsenull
.- Type Parameters:
A
- the type of the annotation to query for and return if available- Parameters:
annotationType
- the Class object corresponding to the annotation type, notnull
- Returns:
- this element's annotation for the specified annotation type if
available, else
null
-
getTypeId
public long getTypeId()
Returns the type id for this annotation element.The type id is a unique identifier for the type in the JVM. It's not guaranteed to be the same between JVM instances.
- Returns:
- the type id, not negative
-
-