Class Context

java.lang.Object
org.apache.juneau.Context
All Implemented Interfaces:
AnnotationProvider
Direct Known Subclasses:
BeanContext, BeanContextable, Config, ConfigStore, RestContext, RestOpContext

public abstract class Context extends Object implements AnnotationProvider
Base class for all Context beans.

Context beans follow the convention of havinTg the following parts:

  • A Context.Builder class for configuring the context bean.
    • This bean is non-thread-safe and meant for one-time use.
  • A Context(Builder) constructor that takes in a builder object.
    • This bean is thread-safe and cacheable/reusable.
  • A ContextSession class for doing work.
    • This bean is non-thread-safe and meant for one-time use.
    Notes:
    • This class is thread safe and reusable.
    See Also:
  • Field Details

  • Constructor Details

    • Context

      protected Context(Context copyFrom)
      Copy constructor.
      Parameters:
      copyFrom - The context to copy from.
    • Context

      protected Context(Context.Builder builder)
      Constructor for this class.
      Parameters:
      builder - The builder for this class.
  • Method Details

    • createBuilder

      public static Context.Builder createBuilder(Class<? extends Context> type)
      Instantiates a builder of the specified context class.

      Looks for a public static method called create that returns an object that can be passed into a public or protected constructor of the class.

      Parameters:
      type - The builder to create.
      Returns:
      A new builder.
    • init

      protected void init(Context.Builder builder)
      Perform optional initialization on builder before it is used.

      Default behavior is a no-op.

      Parameters:
      builder - The builder to initialize.
    • copy

      Creates a builder from this context object.

      Builders are used to define new contexts (e.g. serializers, parsers) based on existing configurations.

      Returns:
      A new Builder object.
    • createSession

      Create a session builder based on the properties defined on this context.

      Use this method for creating sessions where you want to override basic settings. Otherwise, use getSession() directly.

      Returns:
      A new session builder.
    • getSession

      Returns a session to use for this context.

      Note that subclasses may opt to return a reusable non-modifiable session.

      Returns:
      A new session object.
    • isDebug

      public boolean isDebug()
      Debug mode.
      Returns:
      true if debug mode is enabled.
      See Also:
    • forEachAnnotation

      public <A extends Annotation> void forEachAnnotation(Class<A> type, Class<?> onClass, Predicate<A> filter, Consumer<A> action)
      Description copied from interface: AnnotationProvider
      Performs an action on the matching annotations on the specified class.
      Specified by:
      forEachAnnotation in interface AnnotationProvider
      Type Parameters:
      A - The annotation type to find.
      Parameters:
      type - The annotation type to find.
      onClass - The class to search on.
      filter - A predicate to apply to the entries to determine if action should be performed. Can be null.
      action - An action to perform on the entry.
    • firstAnnotation

      public <A extends Annotation> A firstAnnotation(Class<A> type, Class<?> onClass, Predicate<A> filter)
      Description copied from interface: AnnotationProvider
      Finds the first matching annotation on the specified class.
      Specified by:
      firstAnnotation in interface AnnotationProvider
      Type Parameters:
      A - The annotation type to find.
      Parameters:
      type - The annotation type to find.
      onClass - The class to search on.
      filter - A predicate to apply to the entries to determine if value should be used. Can be null.
      Returns:
      The matched annotation, or null if not found.
    • lastAnnotation

      public <A extends Annotation> A lastAnnotation(Class<A> type, Class<?> onClass, Predicate<A> filter)
      Description copied from interface: AnnotationProvider
      Finds the last matching annotation on the specified class.
      Specified by:
      lastAnnotation in interface AnnotationProvider
      Type Parameters:
      A - The annotation type to find.
      Parameters:
      type - The annotation type to find.
      onClass - The class to search on.
      filter - A predicate to apply to the entries to determine if value should be used. Can be null.
      Returns:
      The matched annotation, or null if not found.
    • forEachDeclaredAnnotation

      public <A extends Annotation> void forEachDeclaredAnnotation(Class<A> type, Class<?> onClass, Predicate<A> filter, Consumer<A> action)
      Description copied from interface: AnnotationProvider
      Performs an action on the matching declared annotations on the specified class.
      Specified by:
      forEachDeclaredAnnotation in interface AnnotationProvider
      Type Parameters:
      A - The annotation type to find.
      Parameters:
      type - The annotation type to find.
      onClass - The class to search on.
      filter - A predicate to apply to the entries to determine if action should be performed. Can be null.
      action - An action to perform on the entry.
    • firstDeclaredAnnotation

      public <A extends Annotation> A firstDeclaredAnnotation(Class<A> type, Class<?> onClass, Predicate<A> filter)
      Description copied from interface: AnnotationProvider
      Finds the first matching declared annotations on the specified class.
      Specified by:
      firstDeclaredAnnotation in interface AnnotationProvider
      Type Parameters:
      A - The annotation type to find.
      Parameters:
      type - The annotation type to find.
      onClass - The class to search on.
      filter - A predicate to apply to the entries to determine if value should be used. Can be null.
      Returns:
      The matched annotation, or null if no annotations matched.
    • lastDeclaredAnnotation

      public <A extends Annotation> A lastDeclaredAnnotation(Class<A> type, Class<?> onClass, Predicate<A> filter)
      Description copied from interface: AnnotationProvider
      Finds the last matching declared annotations on the specified class.
      Specified by:
      lastDeclaredAnnotation in interface AnnotationProvider
      Type Parameters:
      A - The annotation type to find.
      Parameters:
      type - The annotation type to find.
      onClass - The class to search on.
      filter - A predicate to apply to the entries to determine if value should be used. Can be null.
      Returns:
      The matched annotation, or null if no annotations matched.
    • forEachAnnotation

      public <A extends Annotation> void forEachAnnotation(Class<A> type, Method onMethod, Predicate<A> filter, Consumer<A> action)
      Description copied from interface: AnnotationProvider
      Performs an action on the matching annotations on the specified method.
      Specified by:
      forEachAnnotation in interface AnnotationProvider
      Type Parameters:
      A - The annotation type to find.
      Parameters:
      type - The annotation type to find.
      onMethod - The method to search on.
      filter - A predicate to apply to the entries to determine if action should be performed. Can be null.
      action - An action to perform on the entry.
    • firstAnnotation

      public <A extends Annotation> A firstAnnotation(Class<A> type, Method onMethod, Predicate<A> filter)
      Description copied from interface: AnnotationProvider
      Finds the first matching annotation on the specified method.
      Specified by:
      firstAnnotation in interface AnnotationProvider
      Type Parameters:
      A - The annotation type to find.
      Parameters:
      type - The annotation type to find.
      onMethod - The method to search on.
      filter - A predicate to apply to the entries to determine if value should be used. Can be null.
      Returns:
      The matched annotation, or null if no annotations matched.
    • lastAnnotation

      public <A extends Annotation> A lastAnnotation(Class<A> type, Method onMethod, Predicate<A> filter)
      Description copied from interface: AnnotationProvider
      Finds the last matching annotation on the specified method.
      Specified by:
      lastAnnotation in interface AnnotationProvider
      Type Parameters:
      A - The annotation type to find.
      Parameters:
      type - The annotation type to find.
      onMethod - The method to search on.
      filter - A predicate to apply to the entries to determine if value should be used. Can be null.
      Returns:
      The matched annotation, or null if no annotations matched.
    • forEachAnnotation

      public <A extends Annotation> void forEachAnnotation(Class<A> type, Field onField, Predicate<A> filter, Consumer<A> action)
      Description copied from interface: AnnotationProvider
      Performs an action on the matching annotations on the specified field.
      Specified by:
      forEachAnnotation in interface AnnotationProvider
      Type Parameters:
      A - The annotation type to find.
      Parameters:
      type - The annotation type to find.
      onField - The field to search on.
      filter - A predicate to apply to the entries to determine if action should be performed. Can be null.
      action - An action to perform on the entry.
    • firstAnnotation

      public <A extends Annotation> A firstAnnotation(Class<A> type, Field onField, Predicate<A> filter)
      Description copied from interface: AnnotationProvider
      Finds the first matching annotation on the specified field.
      Specified by:
      firstAnnotation in interface AnnotationProvider
      Type Parameters:
      A - The annotation type to find.
      Parameters:
      type - The annotation type to find.
      onField - The field to search on.
      filter - A predicate to apply to the entries to determine if value should be used. Can be null.
      Returns:
      The matched annotation, or null if no annotations matched.
    • lastAnnotation

      public <A extends Annotation> A lastAnnotation(Class<A> type, Field onField, Predicate<A> filter)
      Description copied from interface: AnnotationProvider
      Finds the last matching annotation on the specified field.
      Specified by:
      lastAnnotation in interface AnnotationProvider
      Type Parameters:
      A - The annotation type to find.
      Parameters:
      type - The annotation type to find.
      onField - The field to search on.
      filter - A predicate to apply to the entries to determine if value should be used. Can be null.
      Returns:
      The matched annotation, or null if no annotations matched.
    • forEachAnnotation

      public <A extends Annotation> void forEachAnnotation(Class<A> type, Constructor<?> onConstructor, Predicate<A> filter, Consumer<A> action)
      Description copied from interface: AnnotationProvider
      Performs an action on the matching annotations on the specified constructor.
      Specified by:
      forEachAnnotation in interface AnnotationProvider
      Type Parameters:
      A - The annotation type to find.
      Parameters:
      type - The annotation type to find.
      onConstructor - The constructor to search on.
      filter - A predicate to apply to the entries to determine if action should be performed. Can be null.
      action - An action to perform on the entry.
    • firstAnnotation

      public <A extends Annotation> A firstAnnotation(Class<A> type, Constructor<?> onConstructor, Predicate<A> filter)
      Description copied from interface: AnnotationProvider
      Finds the first matching annotation on the specified constructor.
      Specified by:
      firstAnnotation in interface AnnotationProvider
      Type Parameters:
      A - The annotation type to find.
      Parameters:
      type - The annotation type to find.
      onConstructor - The constructor to search on.
      filter - A predicate to apply to the entries to determine if value should be used. Can be null.
      Returns:
      The matched annotation, or null if no annotations matched.
    • lastAnnotation

      public <A extends Annotation> A lastAnnotation(Class<A> type, Constructor<?> onConstructor, Predicate<A> filter)
      Description copied from interface: AnnotationProvider
      Finds the last matching annotation on the specified constructor.
      Specified by:
      lastAnnotation in interface AnnotationProvider
      Type Parameters:
      A - The annotation type to find.
      Parameters:
      type - The annotation type to find.
      onConstructor - The constructor to search on.
      filter - A predicate to apply to the entries to determine if value should be used. Can be null.
      Returns:
      The matched annotation, or null if no annotations matched.
    • hasAnnotation

      public <A extends Annotation> boolean hasAnnotation(Class<A> type, Class<?> onClass)
      Returns true if getAnnotation(a,c) returns a non-null value.
      Type Parameters:
      A - The annotation being checked for.
      Parameters:
      type - The annotation being checked for.
      onClass - The class being checked on.
      Returns:
      true if the annotation exists on the specified class.
    • hasAnnotation

      public <A extends Annotation> boolean hasAnnotation(Class<A> type, Method onMethod)
      Returns true if getAnnotation(a,m) returns a non-null value.
      Type Parameters:
      A - The annotation being checked for.
      Parameters:
      type - The annotation being checked for.
      onMethod - The method being checked on.
      Returns:
      true if the annotation exists on the specified method.
    • hasAnnotation

      public <A extends Annotation> boolean hasAnnotation(Class<A> type, Field onField)
      Returns true if getAnnotation(a,f) returns a non-null value.
      Type Parameters:
      A - The annotation being checked for.
      Parameters:
      type - The annotation being checked for.
      onField - The field being checked on.
      Returns:
      true if the annotation exists on the specified field.
    • hasAnnotation

      public <A extends Annotation> boolean hasAnnotation(Class<A> type, Constructor<?> onConstructor)
      Returns true if getAnnotation(a,c) returns a non-null value.
      Type Parameters:
      A - The annotation being checked for.
      Parameters:
      type - The annotation being checked for.
      onConstructor - The constructor being checked on.
      Returns:
      true if the annotation exists on the specified field.
    • properties

      protected JsonMap properties()
      Returns the properties on this bean as a map for debugging.
      Returns:
      The properties on this bean as a map for debugging.
    • toString

      public String toString()
      Overrides:
      toString in class Object