OnExpressionCondition class
A Condition implementation that dynamically evaluates an expression to determine whether a component, configuration, or pod should be activated.
OnExpressionCondition works in conjunction with the ConditionalOnExpression annotation to enable or disable annotated elements based on the result of an evaluated expression. This allows developers to declaratively define logical or contextual conditions that control configuration activation.
Purpose
This condition is especially useful for scenarios where simple presence-based conditions (e.g., OnClassCondition, OnPropertyCondition) are not sufficient. It enables fine-grained activation logic using dynamic expressions that can reference pods, scopes, or runtime environment properties.
Expressions are evaluated using JetLeaf’s PodExpressionResolver, which supports structured resolution against the active PodFactory and optionally within a Scope.
Behavior
- If a ConditionalOnExpression annotation is present on the Annotation:
- The annotation’s
expressionfield defines the condition to evaluate. - The expression is resolved in the context of the current PodFactory.
- If a Scope annotation is also present, the expression is evaluated within that scope’s registered pod namespace.
- The condition passes (
true) if the expression evaluates successfully and produces a non-null result. - The condition fails (
false) if the evaluation returnsnull, throws an error, or the expression cannot be resolved.
- The annotation’s
- If no ConditionalOnExpression annotation is present, the condition passes automatically.
- If expression resolution is unavailable (no PodExpressionResolver),
evaluation defaults to
falsefor safety.
Example
// Activates only if a specific property evaluates to true
@ConditionalOnExpression("env['jetleaf.enableFeatureX'] == true")
class FeatureXPod {}
// Scoped expression example
@Scope('web')
@ConditionalOnExpression("pods.contains('httpServer')")
class WebServerPod {}
Evaluation Flow
- The condition checks for the presence of a ConditionalOnExpression annotation on the Annotation.
- If found, it retrieves the associated expression string.
- It looks for a Scope annotation to determine the appropriate evaluation context.
- It obtains a PodExpressionResolver from the PodFactory.
- The resolver executes the expression inside a PodExpressionContext, passing in the PodFactory and the optional scope.
- If the expression returns a non-null value, the condition passes; otherwise, it fails.
- If no expression annotation is found, the condition passes by default.
Logging
When trace logging is enabled:
- Logs the start of evaluation for each annotated source.
- Logs the expression being evaluated and the target scope (if any).
- Emits
✅when evaluation returns a non-null value. - Emits
❌when the result is null or evaluation fails. - Captures all intermediate evaluation details to aid conditional debugging.
Related Components
- ConditionalOnExpression: Declares the expression that determines activation.
- PodExpressionResolver: Executes the expression logic within the context.
- PodExpressionContext: Provides access to pods and scoped runtime state.
- Scope: Defines the contextual scope in which the expression is evaluated.
- PodFactory: Provides registered pods and access to the resolver.
- ConditionalContext: Supplies the overall evaluation environment.
- Annotation: Represents the annotated class or method being processed.
- Implemented types
Constructors
- OnExpressionCondition()
-
A Condition implementation that dynamically evaluates an expression to
determine whether a component, configuration, or pod should be activated.
const
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
matches(
ConditionalContext context, Annotation annotation, Source source) → Future< bool> -
Evaluates the condition against the given ConditionalContext and
annotationfor the specified Source.override -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited