Backend Development 7 min read

Byteman Rule Binding, Downcasting, Expressions, Conditions, and Actions

This article explains Byteman's rule binding syntax, including variable binding, downcasting of bound variables, supported expression types, special variables, condition definitions, and action specifications, providing detailed examples and code snippets for effective Java instrumentation.

FunTester
FunTester
FunTester
Byteman Rule Binding, Downcasting, Expressions, Conditions, and Actions

Byteman's event specifications include a binding clause that computes and references variable values each time a rule fires, allowing those values to be used before testing rule conditions.

# 绑定示例
RULE countdown at commit
CLASS com.arjuna.wst11.messaging.engines.CoordinatorEngine
METHOD commit
AT READ state
BIND engine:CoordinatorEngine = $0;
     recovered:boolean = engine.isRecovered();
     identifier:String = engine.getId()
. . .
ENDRULE

In this example, the variable engine is bound to the receiver of the commit method call via the $0 parameter. If commit is static, referencing $0 would cause a type‑check error. Method parameters can be accessed using $1 , $2 , etc., and types can be optionally declared.

Downcasting can be performed during binding to assign a generic type value to a more specific subclass variable. Example:

# 向下转型示例
RULE countdown at commit
CLASS com.arjuna.wst11.messaging.engines.CoordinatorEngine
METHOD commit
AT READ state
BIND engine:CoordinatorEngine = $0;
     endpoint:javax.xml.ws.EndpointReference = engine.participant;
     w3cEndpoint:javax.xml.ws.wsaddressing.W3CEndpointReference = endpoint
. . .
ENDRULE

Here, engine.participant is bound to endpoint of type EndpointReference , then downcast to W3CEndpointReference for w3cEndpoint . The bytecode verifier checks the validity of such casts.

Expressions on the right side of a binding can be any Java expression supported by Byteman, including variable references, method calls, field accesses, literals, array literals, and built‑in calls. Operators such as + , - , * , / , % , && , || , == , != , < , <= , and the ternary ? : are also supported.

Special variables like $! (return value), $^ (thrown exception), $# (argument count), $* (receiver and arguments array), $@ (receiver and arguments for AT INVOKE ), $CLASS , $METHOD , and $NEWCLASS can be bound in rules.

Rule conditions are boolean expressions that determine whether a rule fires. Examples include testing a bound boolean variable, calling a method directly, or using the literal true for unconditional execution.

RULE countdown at commit
CLASS com.arjuna.wst11.messaging.engines.CoordinatorEngine
METHOD commit
AT READ state
BIND engine:CoordinatorEngine = $this;
     recovered:boolean = engine.isRecovered();
IF recovered
. . .
ENDRULE

Actions can be return statements, throws, or a sequence of expressions separated by semicolons. Supported actions include return , throw , and NOTHING .

DO return false
DO throw new RuntimeException("Error occurred")
DO NOTHING

Built‑in calls such as debug(String) and killJVM() can be used within conditions or actions, and custom helpers can extend Byteman with additional built‑in functionality.

DO debug("killing JVM"), killJVM()
TestingJava instrumentationBytemandowncastingrule-binding
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.