BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Byteman 2.0.0: Bytecode Manipulation, Testing, Fault Injection, Logging

Byteman 2.0.0: Bytecode Manipulation, Testing, Fault Injection, Logging

Leia em Português

This item in japanese

Bookmarks

JBoss has released Byteman 2.0.0, an open source Java bytecode manipulation tool licensed under GNU LGPL 2.1. Byteman is a Java agent which helps testing, tracing, and monitoring code. It allows developers to change the operation of Java applications, either as it is loaded or during runtime. It works without the need to rewrite or recompile the application, and can even modify Java Platform classes like String, Thread, etc.

Here are what's new with Byteman 2.0.0.

  • File and Line Debug Information. Compiled Byteman rules now contain file and line number information. This is one step in providing full debugger support.
  • TestNG Import of BMunit using @Listener. TestNG classes can now import BMUnit behavior using @Listener annotation. Previous versions required TestNG classes to subclass BMNGRunner, which causes conflicts when test classes already extend another test class.
  • Throw Error Rule. Byteman now allows for a throw Error rule even if it is undeclared by the target class.
  • Bug Fixes. The latest release has fixed eight bugs found in the previous 1.6.0 release.

 

Byteman uses a simple Event Condition Action (ECA) scripting language to specify where, when and how the target Java code should be transformed. Code execution continues once the injected code has been executed, although the injected code may throw an exception or force an early return. Here's an example of a simple Byteman script.

 

# script.btm
RULE trace main entry
CLASS App
METHOD main
AT ENTRY
IF true
DO traceln("entering main")
ENDRULE

 

Here we have a script that tells Byteman to print "entering main" at the start of the App.main() method. To enable Byteman in your application, and specify the script, add the Java agent JVM argument.

 

-javaagent:<BYTEMAN-DIR>\lib\byteman.jar=script:script.btm

 

Note you can also add Byteman on already running application using bminstall.sh. For more complex rules and code injection, you can use the built-in Byteman Rule Helpers, or you can create your own POJO as a plug-in.

Similar to Byteman, AOP is capable of instrumenting classes and inject code. With Byteman however, there is no need to create classes or compile code. You don't need 100% hindsight when you write your code, and can decide what code to inject at a later time. Byteman is also simpler to use and easier to change, especially for testing and ad hoc logging purposes.

To get started, you can download the Byteman binaries and specify it as a Java agent to your application. Note that Byteman requires JDK 6 or higher. Read the Byteman Programmer's Guide for in depth details. More information about this release can be found in the Byteman 2.0.0 release notes. You can also visit the official Byteman documentation and forum. Source code is available at GitHub.

Rate this Article

Adoption
Style

BT