OnClassCondition class
A Condition implementation that evaluates the presence or absence of classes in the current runtime to determine if a component, pod, or configuration should be activated.
OnClassCondition works in conjunction with the ConditionalOnClass and ConditionalOnMissingClass annotations. It checks for the existence of required classes or ensures that specific classes are missing, enabling conditional activation based on the application's classpath or type availability.
Behavior
- If the annotated element has a ConditionalOnClass annotation:
- Each type listed in the annotation is checked for existence.
- The condition fails if any required class is not present.
- The condition passes only if all required classes are found.
- If the annotated element has a ConditionalOnMissingClass annotation:
- Each type listed is checked for presence.
- The condition fails if any specified class exists.
- The condition passes only if all specified classes are absent.
- If neither annotation is present, the condition automatically passes.
- Detailed trace logging is available at every step when enabled, showing evaluation of each class requirement or absence check.
Example
// Activate only if AdvancedCache class exists
@ConditionalOnClass([ClassType<AdvancedCache>()])
class CacheConfig {}
// Activate only if LoggingService class is missing
@ConditionalOnMissingClass([ClassType<LoggingService>()])
class DefaultLoggerConfig {}
During runtime evaluation:
final context = ConditionalContext(environment, podFactory, runtimeProvider);
final condition = OnClassCondition();
final shouldActivate = await condition.matches(context, source);
Logging
- Logs the evaluation of each required or missing class.
- Trace messages indicate pass/fail decisions and any missing or present classes.
Related Components
- ConditionalContext: Provides the runtime environment for evaluating class presence or absence.
- ConditionalOnClass: Annotation specifying required classes for activation.
- ConditionalOnMissingClass: Annotation specifying classes that must be absent for activation.
Annotation: The metadata source of the annotated element.
- Implemented types
Constructors
- OnClassCondition()
-
A Condition implementation that evaluates the presence or absence of
classes in the current runtime to determine if a component, pod, or
configuration 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 specifiedSource.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