模块 jdk.jfr
 jdk.jfr

类 AnnotationElement

java.lang.Object
jdk.jfr.AnnotationElement

public final class AnnotationElement extends Object
描述事件元数据,例如标签、描述和单位。

以下示例显示了如何使用 AnnotationElement 来动态定义事件。

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();
 
自从:
9
  • 构造方法详细信息

    • AnnotationElement

      public AnnotationElement(Class <? extends Annotation > annotationType, Map <String ,Object > values)
      创建一个注解元素以用于动态定义的事件。

      支持的值类型是 byteintshortlongdoublefloatbooleancharString。不支持枚举、数组和类。

      如果 annotationType 有注释(直接存在、间接存在或关联),则这些注释将被递归包含。但是,annotationType 和递归找到的任何注释都必须具有 MetadataDefinition 注释。

      要静态定义事件,请参阅 Event 类。

      参数:
      annotationType - 接口扩展 java.lang.annotation.Annotation,而不是 null
      values - 一个 Map,其键与指定注释接口的方法名称匹配
      抛出:
      IllegalArgumentException - 如果值/键是 null ,则使用了不受支持的值类型,或者使用了与 annotationType 中的签名不匹配的值/键
    • AnnotationElement

      public AnnotationElement(Class <? extends Annotation > annotationType, Object  value)
      创建一个注解元素以用于动态定义的事件。

      支持的值类型是 byteintshortlongdoublefloatbooleancharString。不支持枚举、数组和类。

      如果 annotationType 有注释(直接存在、间接存在或关联),那么这些注释将被递归包含。但是,annotationType 和递归找到的任何注释都必须具有 MetadataDefinition 注释。

      要静态定义事件,请参阅 Event 类。

      参数:
      annotationType - 接口扩展 java.lang.annotation.Annotation, 而不是 null
      value - 匹配指定annotationTypevalue方法的值
      抛出:
      IllegalArgumentException - 如果值/键是 null ,则使用了不受支持的值类型,或者使用了与 annotationType 中的签名不匹配的值/键
    • AnnotationElement

      public AnnotationElement(Class <? extends Annotation > annotationType)
      创建一个注解元素以用于动态定义的事件。

      支持的值类型是 byteshortintlongdoublefloatbooleancharString。不支持枚举、数组和类。

      如果 annotationType 有注释(直接存在、间接存在或关联),那么这些注释将被递归包含。但是,annotationType 和递归找到的任何注释都必须具有 MetadataDefinition 注释。

      要静态定义事件,请参阅 Event 类。

      参数:
      annotationType - 扩展 java.lang.annotation.Annotation 的接口,而不是 null
  • 方法详情

    • getValues

      public List <Object > getValues()
      以与此 AnnotationElement 的值描述符匹配的顺序返回一个不可变的注解值列表。
      返回:
      值列表,而不是 null
    • getValueDescriptors

      public List <ValueDescriptor > getValueDescriptors()
      返回一个不可变的描述符列表,描述此 AnnotationElement 的注解值。
      返回:
      这个 Annotation 的值描述符列表,而不是 null
    • getAnnotationElements

      public List <AnnotationElement > getAnnotationElements()
      返回此 AnnotationElement 的不可变注解元素列表。
      返回:
      元注释列表,而不是null
    • getTypeName

      public String  getTypeName()
      返回与此 AnnotationElement 对应的注释类型的完全限定名称(例如, "jdk.jfr.Label" )。
      返回:
      输入名称,而不是 null
    • getValue

      public Object  getValue(String  name)
      返回此 AnnotationElement 的值。
      参数:
      name - 注释接口中方法的名称,而不是 null
      返回:
      注解值,而不是 null
      抛出:
      IllegalArgumentException - 如果注释中不存在具有指定名称的方法
    • hasValue

      public boolean hasValue(String  name)
      如果此 AnnotationElement 中存在具有指定名称的注解值,则返回 true
      参数:
      name - 要查找的注释接口中的方法名称,而不是 null
      返回:
      true 如果方法存在,false 否则
    • getAnnotation

      public final <A> A getAnnotation(Class <? extends Annotation > annotationType)
      如果存在具有相同名称的 AnnotationElement,则返回指定类型的第一个注释,否则返回 null
      类型参数:
      A - 要查询并返回的注释类型(如果存在)
      参数:
      annotationType - 注释类型对应的Class object,不是null
      返回:
      此元素对指定注释类型的注解(如果存在),否则为 null
    • getTypeId

      public long getTypeId()
      返回此 AnnotationElement 的类型 ID。

      ID 是 Java 虚拟机 (JVM) 中类型的唯一标识符。 JVM 实例之间的 ID 可能不同。

      返回:
      类型 ID,不是负数