I'm trying to come up with a way to hash scala functions. That is, I want to be able to generate a hash for a function that is consistent between runtimes if the function hasn't changed, but is different if the logic has changed in any way.
Anonymous scala functions will each generate their own .class file. It'll be similar to the classname which will be something like "package.Main$$anonfun$2" And you can get a handle to that and hash the bytes just fine. However, this only goes one level deep.
if this
val nested = (x:Int) => x + 1
val f = (x:Int) => nested(x) + 1
is changed to this
val nested = (x:Int) => x + 2
val f = (x:Int) => nested(x) + 1
Then the class file for nested will change, but the class file for f will not. I'm trying to figure out how to find out what the class file for nested is if all you have is the reference to f.
I've been trying out using the ASM library and so far I haven't gotten what I want. In this example nested will have a class name something like "package.Main$$anonfun$1" and f will have a class name like "package.Main$$anonfun$2". Starting with f I know its classname, but traversing it with ASM class/field/method visitors all I'm getting is "nested$1" instead of "package.Main$$anonfun$1" like I would want. Anyone got any ideas?