1 Reply Latest reply on Jan 17, 2017 7:08 AM by Ondra Chaloupka

    Rule for changing message of System.out.println

    Ondra Chaloupka Expert

      Hi,

       

      I wanted to create a simple rule which changes message which is printed by System.out.println. I found having trouble to put it together.

       

      I took maven archetype program to get

       

      package org.jboss.qa;
      
      /**
       * Hello world!
       *
       */
      public class App 
      {
          public static void main( String[] args )
          {
              System.out.println( "Hello world!" );
          }
      }
      
      
      

       

      Then I started with naive byteman script. I think it is wrong (is it?) as I expect it's not possible to change method parameter on stack this way.

       

      RULE Simple byteman
      CLASS org.jboss.qa.App
      METHOD main
      AT INVOKE PrintStream.println
      IF TRUE
      DO 
        $@[1] = "Hello Byteman!";
      ENDRULE
      
      
      

       

      ThenI take route that I was thinking must work but it dos not for me as I receive exceptions.

      RULE Simple byteman
      CLASS java.io.PrintStream
      METHOD println(java.lang.String)
      AT ENTRY
      IF TRUE
      DO 
        $1 = "Hello Byteman!";
      ENDRULE
      
      
      

       

      java -javaagent:$BYTEMAN_HOME/lib/byteman.jar=script:../simple.btm -Dorg.jboss.byteman.verbose -cp target/byteman-simple-1.0-SNAPSHOT.jar org.jboss.qa.App
      retransforming java.io.PrintStream
      org.jboss.byteman.agent.Transformer : possible trigger for rule Simple byteman change in class java.io.PrintStream
      RuleTriggerMethodAdapter.injectTriggerPoint : inserting trigger into java.io.PrintStream.println(java.lang.String) void for rule Simple byteman change
      org.jboss.byteman.agent.Transformer : inserted trigger for Simple byteman change in class java.io.PrintStream
      Exception in thread "main" java.lang.NoClassDefFoundError: org/jboss/byteman/rule/exception/EarlyReturnException
              at java.io.PrintStream.println(PrintStream.java)
              at org.jboss.qa.App.main(App.java:12)

       

      Is there something that I should do differently? Or what different approach to solve the problem I can use?

       

      Thank you for help

      Ondra