Stefan Endrullis
Stefan Endrullis

Reputation: 4208

Best practice for detecting changes to functions in Scala programs?

I'm working on a Scala-based script language (internal DSL) that allows users to define multiple data transformations functions in a Scala script file. Since the application of these functions could take several hours I would like to cache the results in a database. Users are allowed to change the definition of the transformation functions and also to add new functions. However, then the user restarts the application with a slightly modified script I would like to execute only those functions that have been changed or added. The question is how to detect those changes? For simplicity let us assume that the user can only adapt the script file so that any reference to something not defined in this script can be assumed to be unchanged.

In this case what's the best practice for detecting changes to such user-defined functions?

Until now I though about:

However, all three approaches have their pitfalls.

Does someone has experience with one of these "solutions" or can suggest me a better one?

Upvotes: 11

Views: 284

Answers (1)

axtavt
axtavt

Reputation: 242726

The second option doesn't look difficult. For example, with Javassist library obtaining bytecode of a method is as simple as

CtClass c = ClassPool.getDefault().get(className);
for (CtMethod m: c.getDeclaredMethod()) {
    CodeAttribute ca = m.getMethodInfo().getCodeAttribute();
    if (ca != null) { // i.e. if the method is not native
        byte[] byteCode = ca.getCode();
        ...
    }
}

So, as long as you assume that results of your methods depend on the code of that methods only, it's pretty straighforward.

UPDATE: On the other hand, since your methods are written in Scala, they probably contain some closures, so that parts of their code reside in anonymous classes, and you may need to trace usage of these classes somehow.

Upvotes: 3

Related Questions