Suppose I have a the following in Scala
object Foo {
var functions: List[String => String] = Nil // can be any type to any type.
def addFunc(f:String => String) = functions = f :: functions
}
At runtime, I am given Foo with some functions added. I now want to construct a new .class file implementing something like following in Scala:
object MyObject {
def process1(s:String) = // call Foo.functions(1)
}
I then want to save MyObject in bytecode that can be executed later on even when Foo is not there.
The above is just an example to show what I want to do . I am given the names MyObject, process1, and I have to generate an executable file MyObject.class. The source of MyObject is not needed (it could well have been Java source).
So, at a high level, we need to take memory “snapshot” of Foo.function(1), convert that snapshot into bytecode to store, and generate bytecode of MyObject using this.
All the bytecode engineering libraries I found are too low-level, so I was wondering if there is a higher level library that lets me deal with abstract objects such as functions etc.
Have you looked at the Tree model of ASM? I’ve only used the Event model before, but the Tree sounds like just what you’re looking for. You’ll find an overview in section 1.2.2 of the ASM user guide (a PDF–I don’t think there’s an HTML version, or I’d link that).