Class AnnotationApplier<A extends Annotation,B>

java.lang.Object
org.apache.juneau.AnnotationApplier<A,B>
Type Parameters:
A - The annotation that this applier reads from.
B - The builder class to apply the annotation to.
Direct Known Subclasses:
AnnotationApplier.NoOp, BeanAnnotation.Applier, BeancAnnotation.Applier, BeanConfigAnnotation.Applier, BeanIgnoreAnnotation.Applier, BeanpAnnotation.Applier, ContentAnnotation.Applier, CsvAnnotation.Apply, CsvConfigAnnotation.ParserApply, CsvConfigAnnotation.SerializerApply, ExampleAnnotation.Applier, FormDataAnnotation.Applier, HeaderAnnotation.Applier, HtmlAnnotation.Apply, HtmlConfigAnnotation.ParserApply, HtmlConfigAnnotation.SerializerApply, HtmlDocConfigAnnotation.SerializerApply, HtmlLinkAnnotation.Apply, JsonAnnotation.Apply, JsonConfigAnnotation.ParserApply, JsonConfigAnnotation.SerializerApply, JsonSchemaConfigAnnotation.Apply, MarshalledAnnotation.Applier, MsgPackAnnotation.Apply, MsgPackConfigAnnotation.ParserApply, MsgPackConfigAnnotation.SerializerApply, NamePropertyAnnotation.Applier, OpenApiAnnotation.Apply, OpenApiConfigAnnotation.ParserApply, OpenApiConfigAnnotation.SerializerApply, ParentPropertyAnnotation.Applier, ParserConfigAnnotation.InputStreamParserApply, ParserConfigAnnotation.ParserApply, ParserConfigAnnotation.ReaderParserApply, PathAnnotation.Applier, PlainTextAnnotation.Apply, PlainTextConfigAnnotation.ParserApply, PlainTextConfigAnnotation.SerializerApply, QueryAnnotation.Applier, RequestAnnotation.Applier, ResponseAnnotation.Applier, RestAnnotation.RestContextApply, RestAnnotation.RestOpContextApply, RestDeleteAnnotation.RestOpContextApply, RestGetAnnotation.RestOpContextApply, RestOpAnnotation.RestOpContextApply, RestOptionsAnnotation.RestOpContextApply, RestPatchAnnotation.RestOpContextApply, RestPostAnnotation.RestOpContextApply, RestPutAnnotation.RestOpContextApply, SchemaAnnotation.Apply, SerializerConfigAnnotation.OutputStreamSerializerApply, SerializerConfigAnnotation.SerializerApply, SerializerConfigAnnotation.WriterSerializerApply, SoapXmlAnnotation.Apply, SoapXmlConfigAnnotation.SerializerApply, StatusCodeAnnotation.Applier, SwapAnnotation.Applier, UonAnnotation.Apply, UonConfigAnnotation.ParserApply, UonConfigAnnotation.SerializerApply, UriAnnotation.Applier, UrlEncodingAnnotation.Apply, UrlEncodingConfigAnnotation.ParserApply, UrlEncodingConfigAnnotation.SerializerApply, XmlAnnotation.Apply, XmlConfigAnnotation.ParserApply, XmlConfigAnnotation.SerializerApply

public abstract class AnnotationApplier<A extends Annotation,B> extends Object
Class used to add properties to a context builder (e.g. BeanContext.Builder) from an annotation (e.g. BeanConfig).

Used by Context.Builder.applyAnnotations(Class...) and Context.Builder.applyAnnotations(java.lang.reflect.Method...) to apply annotations to context beans.

The following code shows the general design pattern.

// The annotation applied to classes and methods. @Target({METHOD,TYPE}) @Retention(RUNTIME) @ContextApply(BeanConfigAnnotationApplier.class) public @interface BeanConfig { String sortProperties() default ""; } // The applier that applies the annotation to the bean context builder. public class BeanConfigAnnotationApplier extends AnnotationApplier<BeanConfig,BeanContext.Builder> { // Required constructor. public Applier(VarResolverSession vr) { super(BeanConfig.class, BeanContext.Builder.class, vr); } @Override public void apply(AnnotationInfo<BeanConfig> annotationInfo, BeanContext.Builder builder) { BeanConfig beanConfig = annotationInfo.getAnnotation(); String sortProperties = beanConfig.sortProperties(); if (! sortProperties.isEmpty()) builder.sortProperties(Boolean.parseBoolean(sortProperties)); } } // An annotated class. @BeanConfig(sortProperties="true") public class AnnotatedClass {} // Putting it together. public static void main(String[] args) { // Create a JSON serializer with sorted properties. Serializer serializer = JsonSerializer.create().applyAnnotations(AnnotatedClass.class).build(); }

See Also:
  • Constructor Details

    • AnnotationApplier

      protected AnnotationApplier(Class<A> annotationClass, Class<B> builderClass, VarResolverSession vr)
      Constructor.
      Parameters:
      annotationClass - The annotation class.
      builderClass - The annotation class.
      vr - The string resolver to use for resolving strings.
  • Method Details

    • apply

      public abstract void apply(AnnotationInfo<A> annotationInfo, B builder)
      Apply the specified annotation to the specified property store builder.
      Parameters:
      annotationInfo - The annotation.
      builder - The property store builder.
    • canApply

      public boolean canApply(Object builder)
      Returns true if this apply can be appied to the specified builder.
      Parameters:
      builder - The builder to check.
      Returns:
      true if this apply can be appied to the specified builder.
    • getBuilderClass

      public Class<?> getBuilderClass()
      Returns the builder class that this applier applies to.
      Returns:
      The builder class that this applier applies to.
    • vr

      protected VarResolverSession vr()
      Returns the var resolver session for this apply.
      Returns:
      The var resolver session for this apply.
    • string

      protected Optional<String> string(String in)
      Resolves the specified string.
      Parameters:
      in - The string containing variables to resolve.
      Returns:
      An optional containing the specified string if it exists, or Optional.empty() if it does not.
    • type

      protected <T> Optional<Class<T>> type(Class<T> in)
      Returns the specified value if it's simple name is not "void".
      Type Parameters:
      T - The value to return.
      Parameters:
      in - The value to return.
      Returns:
      An optional containing the specified value.
    • strings

      protected Optional<String[]> strings(String[] in)
      Returns the specified string array as an Optional.

      If the array is empty, then returns Optional.empty().

      Parameters:
      in - The string array.
      Returns:
      The array wrapped in an Optional.
    • stream

      protected Stream<String> stream(String[] in)
      Resolves the specified string as a comma-delimited list of strings.
      Parameters:
      in - The CDL string containing variables to resolve.
      Returns:
      An array with resolved strings.
    • cdl

      protected Stream<String> cdl(String in)
      Resolves the specified string as a comma-delimited list of strings.
      Parameters:
      in - The CDL string containing variables to resolve.
      Returns:
      An array with resolved strings.
    • bool

      public Optional<Boolean> bool(String in)
      Resolves the specified string and converts it to a boolean.
      Parameters:
      in - The string containing variables to resolve.
      Returns:
      The resolved boolean.
    • integer

      protected Optional<Integer> integer(String in, String loc)
      Resolves the specified string and converts it to an int.
      Parameters:
      in - The string containing variables to resolve.
      loc - The annotation field name.
      Returns:
      The resolved int.
    • charset

      protected Optional<Charset> charset(String in)
      Resolves the specified string and converts it to a Charset.
      Parameters:
      in - The string containing variables to resolve.
      Returns:
      The resolved Charset.
    • character

      protected Optional<Character> character(String in, String loc)
      Resolves the specified string and converts it to a Character.
      Parameters:
      in - The string containing variables to resolve.
      loc - The annotation field name.
      Returns:
      The resolved Character.
    • classes

      protected Optional<Class<?>[]> classes(Class<?>[] in)
      Returns the specified class array as an Optional.

      If the array is empty, then returns Optional.empty().

      Parameters:
      in - The class array.
      Returns:
      The array wrapped in an Optional.