1 Reply Latest reply on Sep 25, 2016 3:51 PM by adinn

    Can we call / write java method from Bytemap .btm file ?

    gaurav6281

      Hi,

       

      As Byteman modifies the bytecode and it's actions as part od DO can be triggered at runtime, can we have full java code written inside as part of DO in .btm file ?  Say, if requirement is -

       

      (1) pause a thread at some time

      (2) check database for any particular value

      (3) return from thread

      (4) finish

       

      Can we have full java code exactly written for point 2 as part of .btm file ? (no extra .java class or method to be written) I didn't find anything such that as part of programmer's guide.

        • 1. Re: Can we call / write java method from Bytemap .btm file ?
          adinn

          Hi Gaurav,

          As Byteman modifies the bytecode and it's actions as part od DO can be triggered at runtime, can we have full java code written inside as part of DO in .btm file ? Say, if requirement is -

           

          (1) pause a thread at some time

          (2) check database for any particular value

          (3) return from thread

          (4) finish

           

          Can we have full java code exactly written for point 2 as part of .btm file ? (no extra .java class or method to be written) I didn't find anything such that as part of programmer's guide.

           

          I am afraid the answer is a yes and no.

           

          No, not all Java code can be written explicitly in a rule body:

           

          1. Most Java expressions can appear in the body of a rule. Declarations (of classes, methods and variables) and control statements are excluded. Lambdas are also excluded. Field accesses and method invocations are valid, whether they are at instance or static level. Class literals may be mentioned by name.
          2. Valid expressions also include builtin calls (which are simply calls to public instance methods of the rule's helper class -- by default Byteman's own class Helper) or references to special variables. The latter are terms which start with a $ and they include references to the method target ($0 or $this) and parameters ($1, $2 etc or $paramName1 etc), references to local variables that are in scope at the injection point ($localName etc) and a variety of available references to other useful values such: as an array of all the method parameter values ($*); the return value in at AT RETURN or AFTER CALL rule ($!); the exception in an AT THROW rule ($^); etc.
          3. A DO clause can specify a sequence of actions by writing a sequence of expressions, separated by ';' (a terminating ';' is optional).
          4. A RETURN or THROW expression (upper or lower case) is allowed as the last expression in a DO clause (that includes the case where there is only one expression in the DO clause).

           

          n.b Java operators and field/method accesses can be used to recursively combine expressions from sets 1 and 2 to construct complex expressions.

           

          Yes, you can execute arbitrary code in a rule body but only if you wrap up the behaviour in a method of a class that you provide:

           

          If you want your rules to execute more complex operations that involve, say, iterative or selective control flow then you have to implement your own helper class to encapsulate the complex behaviour in a public instance method. You make this available to your rules as a builtin operation by specifying your  the helper class as the rule's helper using a HELPER clause. Note that the helper class does not have to exist in the application code. You can add it to a jar and get Byteman to insert that  jar into the system or bootstrap classpath when the agent starts up.

           

          All of this is explained fully in the Programmer's Guide. I recommend you to read it much more carefully before asking questions as I cannot afford to spend a great deal of time rewriting information that is already available.