Package io.jstach.prism


package io.jstach.prism
Annotations and processors for creating Prisms which are partial reflections of known annotations.

The problem: When writing annotation processors the two conventional mechanisms to access the annotations in the source code are both awkward. Element.getAnnotation() can throw Exceptions if the annotation being modelled is not semantically correct, and the member methods on the returned Annotation can also throw Exceptions if the annotation being modelled is not semantically correct. Moreover when calling a member with a Class return type, you need to catch an exception to extract the TypeMirror.

On the other hand, AnnotationMirror and AnnotationValue do a good job of modelling both correct and incorrect annotations, but provide no simple mechanism to determine whether it is correct or incorrect, and provide no convenient functionality to access the member values in a simple type specific way. While AnnotationMirror and AnnotationValue provide an ideal mechanism for dealing with unknown annotations, they are inconvenient for reading member values from known annotations.

A Prism provides a solution to this problem by combining the advantages of the pure reflective model of AnnotationMirror and the runtime (real) model provided by Element.getAnnotation(), hence the term Prism to capture this idea of partial reflection.

A Mirror is where you look for a reflection whereas a Prism is where you look for a partial reflection. A Prism provides a partially reflective and partially real view of an Annotation.

A Prism class is generated by using the @GeneratePrism annotation, or several can be grouped together using @GeneratePrisms. The generated prisms have a complete set of javadoc for reference.

An instance of a Prism is obtained by calling the generated prism's factory method getInstance(AnnotationMirror) to obtain a prism corresponding to an annotation mirror, or the factory method getInstanceOn(Element) to obtain an instance corresponding to an annotation on the specified Element.

A prism has the same member methods as the annotation except that the return types are translated from runtime types to compile time types as follows...

  • Primitive members return their equivalent wrapper class in the prism.
  • Class members return a TypeMirror from the mirror API.
  • Enum members return a String being the name of the enum constant (because the constant value in the mirror API might not match those available in the runtime it cannot consistently return the appropriate enum).
  • String members return Strings.
  • Annotation members return a Prism of the annotation. If a prism for that annotation is generated from the same @GeneratePrisms annotation as the prism that uses it, then an instance of that prism will be returned. Otherwise a Prism for that annotation is supplied as an inner class of the dependant Prism. the name of which is the simple name of the referenced annotation type.
  • Array members return a List<X> where X is the appropriate prism mapping of the array component as above.
If a prism instance represents a semantically incorrect annotation then its isValid field will be false, and the member(s) with the erroneous value will return null. If isValid is true then no members will return null. AnnotatonProcessors using a prism should ignore any prism instance that is invalid. It can be assumed that the processing tool itself will indicate an error to the user in this case.

The mirror field provides access to the underlying AnnotationMirror to assist with using the Messager.

The values field contains an object whose methods have the same signature as the annotation's methods but return the corresponding AnnotationValue or null if that member of the Annotation does not have a value. This is provided for use when using Messager which can take an AnnotationValue as a hint as to the message's position in the source code.

To generate a prism of a known annotation to be used by an AnnotationProcessor, use the @GeneratePrism annotation on either the Processor itself, or the package, compile once and the prism should be generated. The Processor for @GeneratePrism will be discovered automatically using the ServiceLoader mechanism if the annotation processing tool supports that means of discovery process.

If the processor needs to access prisms for more than one annotation type, use @GeneratePrisms to group together all the @GeneratePrism annotations needed.

  • Annotation Interfaces
    Class
    Description
    Generates a Prism for the specified annotation, in the same package as the target.
    Use this to group several @GeneratePrism annotations together, for example in a package annotation.