Class AnnotationApplier<A extends Annotation,B>

java.lang.Object
org.apache.juneau.marshall.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:
BeanConfigAnnotation.Applier, BeanCtorApplyAnnotation.Applier, BeanIgnoreApplyAnnotation.Applier, BeanPropApplyAnnotation.Applier, BeanTypeApplyAnnotation.Applier, BsonApplyAnnotation.Applier, BsonConfigAnnotation.ParserApply, BsonConfigAnnotation.SerializerApply, CborApplyAnnotation.Applier, CborConfigAnnotation.ParserApply, CborConfigAnnotation.SerializerApply, CsvApplyAnnotation.Applier, CsvConfigAnnotation.ParserApply, CsvConfigAnnotation.SerializerApply, ExampleApplyAnnotation.Applier, HjsonApplyAnnotation.Applier, HjsonConfigAnnotation.ParserApply, HjsonConfigAnnotation.SerializerApply, HoconApplyAnnotation.Applier, HoconConfigAnnotation.ParserApply, HoconConfigAnnotation.SerializerApply, HtmlApplyAnnotation.Applier, HtmlConfigAnnotation.ParserApply, HtmlConfigAnnotation.SerializerApply, HtmlDocConfigAnnotation.SerializerApply, HtmlLinkApplyAnnotation.Applier, IniApplyAnnotation.Applier, IniConfigAnnotation.ParserApply, IniConfigAnnotation.SerializerApply, JcsConfigAnnotation.SerializerApply, Json5lConfigAnnotation.ParserApply, Json5lConfigAnnotation.SerializerApply, JsonApplyAnnotation.Applier, JsonConfigAnnotation.ParserApply, JsonConfigAnnotation.SerializerApply, JsonlConfigAnnotation.ParserApply, JsonlConfigAnnotation.SerializerApply, JsonSchemaConfigAnnotation.Apply, MarkdownApplyAnnotation.Applier, MarkdownConfigAnnotation.ParserApply, MarkdownConfigAnnotation.SerializerApply, MarshalledApplyAnnotation.Applier, MarshalledConfigAnnotation.Applier, MarshalledIgnoreApplyAnnotation.Applier, MarshalledPropApplyAnnotation.Applier, MsgPackApplyAnnotation.Applier, MsgPackConfigAnnotation.ParserApply, MsgPackConfigAnnotation.SerializerApply, NamePropertyApplyAnnotation.Applier, OpenApiApplyAnnotation.Applier, OpenApiConfigAnnotation.ParserApply, OpenApiConfigAnnotation.SerializerApply, ParentPropertyApplyAnnotation.Applier, ParquetApplyAnnotation.Applier, ParquetConfigAnnotation.ParserApply, ParquetConfigAnnotation.SerializerApply, ParserConfigAnnotation.InputStreamParserApply, ParserConfigAnnotation.ParserApply, ParserConfigAnnotation.ReaderParserApply, PlainTextApplyAnnotation.Applier, PlainTextConfigAnnotation.ParserApply, PlainTextConfigAnnotation.SerializerApply, RdfApplyAnnotation.Applier, RdfConfigAnnotation.ParserApply, RdfConfigAnnotation.SerializerApply, SchemaApplyAnnotation.Applier, SerializerConfigAnnotation.OutputStreamSerializerApply, SerializerConfigAnnotation.SerializerApply, SerializerConfigAnnotation.WriterSerializerApply, SoapXmlApplyAnnotation.Applier, SoapXmlConfigAnnotation.SerializerApply, SwapApplyAnnotation.Applier, TomlApplyAnnotation.Applier, TomlConfigAnnotation.ParserApply, TomlConfigAnnotation.SerializerApply, UonApplyAnnotation.Applier, UonConfigAnnotation.ParserApply, UonConfigAnnotation.SerializerApply, UriApplyAnnotation.Applier, UrlEncodingApplyAnnotation.Applier, UrlEncodingConfigAnnotation.ParserApply, UrlEncodingConfigAnnotation.SerializerApply, XmlApplyAnnotation.Applier, XmlConfigAnnotation.ParserApply, XmlConfigAnnotation.SerializerApply, YamlApplyAnnotation.Applier, YamlConfigAnnotation.ParserApply, YamlConfigAnnotation.SerializerApply

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

Used by Context.Builder.applyAnnotations(Class...) and Context.Builder.applyAnnotations(Object...) 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(MarshalledConfigAnnotationApplier.class) public @interface MarshalledConfig { String unsortedProperties() default ""; } // The applier that applies the annotation to the bean context builder. public class MarshalledConfigAnnotationApplier extends AnnotationApplier<MarshalledConfig,MarshallingContext.Builder<?>> { // Required constructor. public Applier(VarResolverSession vr) { super(MarshalledConfig.class, MarshallingContext.Builder.class, vr); } @Override public void apply(AnnotationInfo<MarshalledConfig> annotationInfo, MarshallingContext.Builder<?> builder) { MarshalledConfig beanConfig = annotationInfo.getAnnotation(); String unsortedProperties = beanConfig.unsortedProperties(); if (! unsortedProperties.isEmpty()) builder.unsortedProperties(Boolean.parseBoolean(unsortedProperties)); } } // An annotated class opting out of the default sorted behavior. @MarshalledConfig(unsortedProperties="true") public class AnnotatedClass {} // Putting it together. public static void main(String[] args) { // Create a JSON serializer where AnnotatedClass uses natural JVM order. Serializer serializer = JsonSerializer.create().applyAnnotations(AnnotatedClass.class).build(); }

  • Constructor Details

    • AnnotationApplier

      protected AnnotationApplier(Class<A> annotationClass, Class<B> builderClass, VarResolverSession varResolverSession)
      Constructor.
      Parameters:
      annotationClass - The annotation class. Must not be null.
      builderClass - The builder class. Must not be null.
      varResolverSession - The string resolver to use for resolving strings. Must not be null.
  • 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.
    • 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.
    • 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.
    • 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:
      A stream of resolved strings.
    • 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.
    • 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.
    • 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.
    • 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.
    • stream

      protected Stream<String> stream(String[] in)
      Resolves each string in the specified array.
      Parameters:
      in - The array of strings containing variables to resolve.
      Returns:
      A stream of resolved strings.
    • 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.
    • 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.
    • 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.
    • vr

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