Annotations

Mirror has its own annotation system that is safer but can also interop with existing Java annotations.

You can get an Annotated object's annotations with Annotated.getAnnotations().

The supported Annotated objects are:

  • Constructor

  • Invokable

  • Method

  • Parameter

  • ParameterizedType

  • TypeDefinition

@Manufacturer("TOYOTA")
class Car {}

TypeDefinition<Car> carType = mirror.reflect(Car.class);
AnnotationValues annotations = carType.getAnnotations();

assert annotations.getString(AnnotationElement.value(Manufacturer.class))
        .orElseThrow(AssertionError::new).equals("TOYOTA");

Manufacturer manufacturer = carType.getRawAnnotation(Manufacturer.class);

assert manufacturer != null;
assert manufacturer.value().equals("TOYOTA");

Creating Annotations

Mirror supports creating your own AnnotationValues instances via AnnotationValues#builder().

AnnotationValues annotations = AnnotationValues.builder()
        .annotate(Awesome.class)
        .value(AnnotationElement.value(Manufacturer.class), "TOYOTA")
        .build();

// equivalient to @Awesome @Manufacturer("TOYOTA")

Last updated