Class AnnotationReader<T extends Annotation>

  • Type Parameters:
    T - Type of the MP metrics annotation

    public final class AnnotationReader<T extends Annotation>
    extends Object
    Utility that allows reading the different MP metrics Annotations from different annotated abstractions providing a common interface to allow generic handling and a common logic independent of the source of the Annotation. Supported are: It is important to realise that Annotated and InjectionPoint have to be used as a source when available as they allow to add or remove Annotation effectively acting as a runtime override of the compiled information provided by AnnotatedElement. This utility also encodes most of the logic as defined by the MP Metrics specification. This includes the logic of which annotation applied and how the metrics effective name if computed from annotation values and the annotated element. For this reason the methods are documented in great detail.
    Since:
    5.202
    Author:
    Jan Bernitt
    • Field Detail

      • CONCURRENT_GAUGE

        public static final AnnotationReader<org.eclipse.microprofile.metrics.annotation.ConcurrentGauge> CONCURRENT_GAUGE
      • COUNTED

        public static final AnnotationReader<org.eclipse.microprofile.metrics.annotation.Counted> COUNTED
      • GAUGE

        public static final AnnotationReader<org.eclipse.microprofile.metrics.annotation.Gauge> GAUGE
      • METERED

        public static final AnnotationReader<org.eclipse.microprofile.metrics.annotation.Metered> METERED
      • METRIC

        public static final AnnotationReader<org.eclipse.microprofile.metrics.annotation.Metric> METRIC
      • TIMED

        public static final AnnotationReader<org.eclipse.microprofile.metrics.annotation.Timed> TIMED
      • SIMPLY_TIMED

        public static final AnnotationReader<org.eclipse.microprofile.metrics.annotation.SimplyTimed> SIMPLY_TIMED
    • Method Detail

      • annotationType

        public Class<T> annotationType()
      • type

        public org.eclipse.microprofile.metrics.MetricType type()
        Returns:
        the MetricType used by this AnnotationReader when generating a Metadata object any of the variants of the metadata(Annotation) methods. Usually each Annotation is associated with a particular MetricType but for Metric Annotation the type can be changed using #asType(Class) methods.
      • annotation

        public <E extends Member & AnnotatedElementT annotation​(Class<?> bean,
                                                                  E element)
        Returns the effective annotation for the provided bean and element.
        Parameters:
        bean - type of the bean that declared the provided element
        element - a AnnotatedElement possibly annotated with this AnnotationReader's annotationType()
        Returns:
        the effective Annotation, or null. The element's annotations take precedence over the bean's annotations.
      • annotation

        public T annotation​(jakarta.enterprise.inject.spi.InjectionPoint point)
        Reads the effective Annotation for the provided InjectionPoint.
        Parameters:
        point - source InjectionPoint for an annotated element having this AnnotationReader's annotationType(), not null
        Returns:
        the effective annotation for the provided InjectionPoint, never null
        Throws:
        IllegalArgumentException - In case the provided InjectionPoint isn't effectively annotated with this AnnotationReader's Annotation.
      • name

        public String name​(T annotation)
        Returns the metric name as defined by the provided Annotation
        Parameters:
        annotation - source annotation to read, not null
        Returns:
        name value of the provided source annotation
      • name

        public String name​(jakarta.enterprise.inject.spi.InjectionPoint point)
        Returns the metric name as defined by the MP specification for the annotation situation at hand for the provided InjectionPoint. This does take into account that annotations might have been added or removed at runtime.
        Parameters:
        point - source InjectionPoint for an annotated element having this AnnotationReader's annotationType(), not null
        Returns:
        full metric name as required by the MP specification
        Throws:
        IllegalArgumentException - In case the provided InjectionPoint isn't effectively annotated with this AnnotationReader's Annotation.
      • name

        public String name​(jakarta.enterprise.inject.spi.AnnotatedMember<?> member)
        Returns the metric name as defined by the MP specification for the annotation situation at hand for the provided AnnotatedMember. This does take into account that annotations might have been added or removed at runtime.
        Parameters:
        member - source AnnotatedMember for an annotated element having this AnnotationReader's annotationType(), not null
        Returns:
        full metric name as required by the MP specification
        Throws:
        IllegalArgumentException - In case the provided AnnotatedMember isn't effectively annotated with this AnnotationReader's Annotation.
      • tags

        public org.eclipse.microprofile.metrics.Tag[] tags​(T annotation)
        Returns the metric tags as defined by the provided Annotation
        Parameters:
        annotation - source annotation to read, not null
        Returns:
        tags value of the provided source annotation
      • metricID

        public org.eclipse.microprofile.metrics.MetricID metricID​(T annotation)
        Returns the MetricID as defined by the provided Annotation's name and tags attributes.
        Parameters:
        annotation - source annotation to read, not null
        Returns:
        MetricID value of the provided source annotation
      • metricID

        public org.eclipse.microprofile.metrics.MetricID metricID​(jakarta.enterprise.inject.spi.InjectionPoint point)
        Returns the metric MetricID as defined by the MP specification for the annotation situation at hand for the provided InjectionPoint. This does take into account that annotations might have been added or removed at runtime.
        Parameters:
        point - source InjectionPoint for an annotated element having this AnnotationReader's annotationType(), not null
        Returns:
        MetricID with full metric name as required by the MP specification
        Throws:
        IllegalArgumentException - In case the provided InjectionPoint isn't effectively annotated with this AnnotationReader's Annotation.
      • metricID

        public org.eclipse.microprofile.metrics.MetricID metricID​(jakarta.enterprise.inject.spi.AnnotatedMember<?> member)
        Returns the metric MetricID as defined by the MP specification for the annotation situation at hand for the provided InjectionPoint. This does take into account that annotations might have been added or removed at runtime.
        Parameters:
        point - source AnnotatedMember for an annotated element having this AnnotationReader's annotationType(), not null
        Returns:
        MetricID with full metric name as required by the MP specification
        Throws:
        IllegalArgumentException - In case the provided AnnotatedMember isn't effectively annotated with this AnnotationReader's Annotation.
      • displayName

        public String displayName​(T annotation)
        Returns the metric display name as defined by the provided Annotation
        Parameters:
        annotation - source annotation to read, not null
        Returns:
        display name value of the provided source annotation
      • description

        public String description​(T annotation)
        Returns the metric description as defined by the provided Annotation
        Parameters:
        annotation - source annotation to read, not null
        Returns:
        description of the provided source annotation
      • unit

        public String unit​(T annotation)
        Returns the metric unit as defined by the provided Annotation
        Parameters:
        annotation - source annotation to read, not null
        Returns:
        unit of the provided source annotation
      • absolute

        public boolean absolute​(T annotation)
        Returns the metric absolute flag as defined by the provided Annotation
        Parameters:
        annotation - source annotation to read, not null
        Returns:
        absolute flag of the provided source annotation
      • metadata

        public org.eclipse.microprofile.metrics.Metadata metadata​(T annotation)
        Returns the full Metadata as defined by the provided Annotation
        Parameters:
        annotation - source annotation to read, not null
        Returns:
        Metadata of the provided source annotation
      • metadata

        public org.eclipse.microprofile.metrics.Metadata metadata​(jakarta.enterprise.inject.spi.InjectionPoint point)
        Returns the metric Metadata as defined by the MP specification for the annotation situation at hand for the provided InjectionPoint. This does take into account that annotations might have been added or removed at runtime.
        Parameters:
        point - source InjectionPoint for an annotated element having this AnnotationReader's annotationType(), not null
        Returns:
        Metadata with full metric name as required by the MP specification
        Throws:
        IllegalArgumentException - In case the provided InjectionPoint isn't effectively annotated with this AnnotationReader's Annotation.
      • metadata

        public org.eclipse.microprofile.metrics.Metadata metadata​(jakarta.enterprise.inject.spi.AnnotatedMember<?> member)
        Returns the metric Metadata as defined by the MP specification for the annotation situation at hand for the provided InjectionPoint. This does take into account that annotations might have been added or removed at runtime.
        Parameters:
        point - source AnnotatedMember for an annotated element having this AnnotationReader's annotationType(), not null
        Returns:
        Metadata with full metric name as required by the MP specification
        Throws:
        IllegalArgumentException - In case the provided AnnotatedMember isn't effectively annotated with this AnnotationReader's Annotation.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • isReference

        public boolean isReference​(T annotation)
        Checks if an Annotation does not provide any information beyond the required name and tags.
        Parameters:
        annotation - source annotation to read, not null
        Returns:
        true, of no property is set to a value that would require using Metadata when registering, else false.
      • getOrRegister

        public <M extends org.eclipse.microprofile.metrics.Metric> M getOrRegister​(jakarta.enterprise.inject.spi.InjectionPoint point,
                                                                                   Class<M> metric,
                                                                                   org.eclipse.microprofile.metrics.MetricRegistry registry)
        Resolves the Metric referred to by the provided InjectionPoint. If it does not exist, the metric is created. Lookup and creation are one atomic operation. Depending on the provided information in the effective Annotation for the provided InjectionPoint the metric is resolved or registered using Metadata, name and Tags or just its name. A Gauge can only be resolved, not created.
        Parameters:
        point - source InjectionPoint for an annotated element having this AnnotationReader's annotationType(), not null
        metric - type of the Metric to find or create, not null
        registry - MetricRegistry to use, not null
        Returns:
        the resolved or registered metric, or null if a Gauge did not exist
      • tagsFromString

        public static org.eclipse.microprofile.metrics.Tag[] tagsFromString​(String[] tags)