Package com.tnsai.annotations
Annotation Interface Contract
Defines behavioral contracts for actions using Design by Contract principles.
Contracts specify preconditions, postconditions, and invariants that must hold for correct action execution. These are validated at runtime and can be used for formal verification.
Usage
@ActionSpec(type = ActionType.LOCAL, description = "Transfer funds")
@Contract(
preconditions = {
"amount > 0",
"sourceAccount.balance >= amount"
},
postconditions = {
"sourceAccount.balance == old(sourceAccount.balance) - amount",
"targetAccount.balance == old(targetAccount.balance) + amount"
},
invariants = {
"totalMoney == old(totalMoney)"
}
)
public TransferResult transfer(Account source, Account target, double amount) {
...
}
Expression Language
old(expr)- Value of expression before executionresult- Return value (in postconditions)- Standard Java expressions with parameter names
The annotated method must also carry @ActionSpec. Discovery
fails if @Contract is present without it — the clause would
otherwise never be evaluated.
- Author:
- TnsAI Team
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumDefines how contracts are inherited. -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionContract inheritance mode.String[]Invariants that must hold before and after execution.Custom message for contract violations.String[]Postconditions that must be true after action execution.String[]Preconditions that must be true before action execution.booleanWhether to throw exception on contract violation.booleanWhether to validate contracts at runtime.
-
Element Details
-
preconditions
String[] preconditionsPreconditions that must be true before action execution.Expressions use method parameter names.
- Returns:
- precondition expressions
- Default:
{}
-
postconditions
String[] postconditionsPostconditions that must be true after action execution.Can use
resultto refer to return value andold(expr)to refer to pre-execution values.- Returns:
- postcondition expressions
- Default:
{}
-
invariants
String[] invariantsInvariants that must hold before and after execution.Typically used for class-level consistency constraints.
- Returns:
- invariant expressions
- Default:
{}
-
validate
boolean validateWhether to validate contracts at runtime.When false, contracts are documentation only.
- Returns:
- true to validate at runtime
- Default:
true
-
strict
boolean strictWhether to throw exception on contract violation.When false, violations are logged but execution continues.
- Returns:
- true to throw on violation
- Default:
true
-
message
String messageCustom message for contract violations.- Returns:
- violation message template
- Default:
""
-
inheritance
Contract.InheritanceMode inheritanceContract inheritance mode.- Default:
STRENGTHEN
-