Instrumentation to count every statement in a Scala function

90 views Asked by At

I have a simple Scala function in which I want to increment a class variable every time a statement is executed.

    class C {
        var cnt: Int: 0

        def fun(): Unit = {
             var a: Int = 0
             var b: Int = -10
             var sum: Int = 0
             sum = a + b
             return sum
        }
    }

I want the function to be like this:

    class C {
        var cnt: Int: 0

        def fun(): Unit = {
             var a: Int = 0
             cnt = cnt + 1
             var b: Int = -10
             cnt = cnt + 1
             var sum: Int = 0
             cnt = cnt + 1
             sum = a + b
             cnt = cnt + 1
             return sum
        }
    }

But doing so at the software level makes the code messy. Can I use bytecode manipulation to implement this or I should intervene in other levels of compilation?

1

There are 1 answers

2
Byteman Project On

I'm not sure who added the tag Byteman to this question but Byteman is not going to be able help you with this. As a previous poster said this is not really a problem that can be solved using bytecode manipulation. Bytecode has a fairly opaque link to source line and source statement boundaries and it is often not possible to derive either type of boundary from the instructions encoded in the bytecode.

A source to source translator based on a language parser would be a much better option.