Annotation Type Annotation


  • @Retention(RUNTIME)
    @Target({METHOD,TYPE})
    @Repeatable(Annotations.class)
    public @interface Annotation
    An annotation can be used to add custom metadata to entities and properties of a model.

    Example Model:

    
     package mypkg.vmfmodel;
     
     import eu.mihosoft.vmf.core.*;
     
     interface Node {
     
         String getId();
     
         @Annotation(key="api",value="input")
         Node getA();
     
         @Annotation(key="api",value="input")
         Node getB();
     
         @Annotation(key="api",value="output")
         Node getC();
     
     }

    Retrieving Annotations:

    We can retrieve property annotations with the following API call:
    
     Property p = ...
     List annotations = p.annotations();
     

    Filter Annotations:

    Using the stream API, one can query a specific annotation (e.g. filtered by key). Here's an example that prints all properties of a node instance annotated as input (key="api", value="input").
    
     // predicate to filter inputs
     Predicate isInput = (p) -> {
         return p.annotationByKey("api").
             map(ann->"input".equals(ann.getValue())).orElse(false);
     };
     
     // query inputs:
     n.vmf().reflect().properties().stream().filter(isInput).forEach(p->{
         System.out.println(
             "-> input  param '" + p.getName() + "' -> node: " + ((Node)p.get()).getId()
         );
     });
     

    Created by miho on 05.12.2018.

    See Also:
    Tutorial on Annotation Support
    • Required Element Summary

      Required Elements 
      Modifier and Type Required Element Description
      java.lang.String value
      Value of the annotation.
    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element Description
      java.lang.String key
      Key of the annotation, e.g., "api" or "model".
    • Element Detail

      • value

        java.lang.String value
        Value of the annotation. This can be an arbitrary string. Consumers of the annotation are responsible for parsing the value string.
      • key

        java.lang.String key
        Key of the annotation, e.g., "api" or "model".
        Default:
        ""