Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Byteman – Byte code manipulation tool for Logging, Testing and Fault Injection

Byteman is a byte Code Manipulation Tool for fault injection, testing and tracing. The Byteman agent allows to inject the rules into the existing java application with out changing the source code. You can inject the rules during the JVM startup time or into running application with out build and redeploy. We can inject the rules for custom java classes, private methods and the JRE libraries as well. Below are the Byteman use cases.

  • Add logging statements for the legacy application
  • Inject fault scenarios to the application via Junit or TestNG unit test cases

Now, we will see simple java application for which we will add debug statements via Byteman.

Let us write a simple Java application.


package org.smarttechie;

/**
* Class will demonstrate the Byteman
* @author Siva
*
*/
public class BytemanDemonstration {
public static void main(String[] args) {
    System.out.println("With this class we are demonstrating the byteman");
 }
}

Download the latest Byteman distribution.

Write Byteman script to inject the debug statements for the above class.


RULE trace main entry
CLASS BytemanDemonstration
METHOD main
AT ENTRY
IF true
DO traceln("entering into main method")
ENDRULE

RULE trace main exit
CLASS BytemanDemonstration
METHOD main
AT EXIT
IF true
DO traceln("exiting the main method")
ENDRULE

Launch the JVM by passing the Byteman agent and the script file path.

 java -javaagent:${Byteman_Home}\lib\byteman.jar=script:${SCRIPT_FILE_PATH}\logrules.btm 

After launching the JVM, you will able to see the log messages coming from the script file.


entering into main method
With this class we are demonstrating the byteman
exiting the main method

For further exploration, follow the below links.

https://developer.jboss.org/wiki/ABytemanTutorial#top

https://developer.jboss.org/wiki/BMUnitUsingBytemanWithJUnitOrTestNGFromMavenAndAnt#top




This post first appeared on Smart Techie | My Thoughts, My Work, My Ideas, please read the originial post: here

Share the post

Byteman – Byte code manipulation tool for Logging, Testing and Fault Injection

×

Subscribe to Smart Techie | My Thoughts, My Work, My Ideas

Get updates delivered right to your inbox!

Thank you for your subscription

×