ConditionalOnClass class
An annotation that conditionally activates a Component, Pod, or configuration class only if specific classes are present in the Dart runtime or compilation environment.
The ConditionalOnClass annotation allows JetLeaf to selectively load components based on the presence of other classes, enabling modular configuration and safe integration with optional dependencies.
Purpose
In JetLeaf applications, some modules or configurations should only initialize when certain external or framework classes are available. This is especially useful for optional integrations, third-party dependencies, or feature toggles controlled by class availability.
ConditionalOnClass provides a declarative, type-safe mechanism for expressing such conditions without resorting to procedural runtime checks or try-catch imports.
Behavior
-
values may include:
- A Dart Type (e.g.,
MyService) - A
ClassTypewrapper (e.g.,ClassType<MyService>()) - A qualified class name string (e.g.,
'package:app/core/logger.dart.LoggingService')
- A Dart Type (e.g.,
-
The annotated element is processed only if all specified classes exist.
-
If any referenced class cannot be resolved, the condition fails silently, and the annotated element is skipped.
Example
// Activated only if AdvancedCache class is available
@ConditionalOnClass([ClassType<AdvancedCache>()])
class AdvancedCacheConfig {}
// Activated only if LoggingService class exists by qualified name
@ConditionalOnClass([
ClassType.qualified('package:jetleaf/example/jetleaf_example.dart.LoggingService')
])
class LoggingConfig {}
Related Components
- ConditionalContext – Evaluates and manages class- and pod-based conditional activation.
- Conditional – The core conditional annotation base class that this extends.
- ConditionalOnMissingClass – The inverse condition, which activates only when classes are absent.
- Inheritance
-
- Object
- WhenConditional
- ConditionalOnClass
- Annotations
-
- @Conditional([OnClassCondition()])
- @Target.new({TargetKind.classType, TargetKind.method})
Constructors
-
ConditionalOnClass([List<
Object> values = const []]) -
An annotation that conditionally activates a Component, Pod, or
configuration class only if specific classes are present in the Dart
runtime or compilation environment.
const
Properties
- annotationType → Type
-
Returns the annotation _type of this annotation.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
values
→ List<
Object> -
The list of class identifiers that must be present for activation.
final
Methods
-
equalizedProperties(
) → List< Object?> -
Mixin-style contract for value-based equality,
hashCode, andtoString. -
equals(
Object other) → bool -
Checks whether the given object is logically equivalent to this annotation.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String - Returns a string representation of this annotation.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited