-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Next steps are to provide a better interface to generated classes than Java reflection. Scala Dynamic trait should prove useful.
- Loading branch information
Showing
6 changed files
with
62 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cafebabe | ||
|
||
import scala.collection.mutable.{Map=>MutableMap} | ||
|
||
/** A `ClassLoader` with the capability for loading cafebabe | ||
* `ClassFile`s directly from memory. */ | ||
class CafebabeClassLoader extends ClassLoader { | ||
private val classes : MutableMap[String,Array[Byte]] = MutableMap.empty | ||
|
||
def register(classFile : ClassFile) { | ||
val byteStream = (new ByteStream) << classFile | ||
classes(classFile.className) = byteStream.getBytes | ||
} | ||
|
||
override def findClass(name : String) : Class[_] = { | ||
classes.get(name) match { | ||
case Some(ba) => defineClass(name, ba, 0, ba.length) | ||
case None => super.findClass(name) | ||
} | ||
} | ||
|
||
def newInstance(name : String) : AnyRef = { | ||
val klass = this.loadClass(name) | ||
klass.newInstance().asInstanceOf[AnyRef] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cafebabe | ||
|
||
/** Do not rely on this object, as it will change/be renamed, etc. | ||
* The goal is for `CafebabeClassLoader`s to generate `DynObj`s or | ||
* equivalent, so that users of dynamic class generation have to | ||
* write explicit reflection code as little as possible. */ | ||
class DynObj private[cafebabe](base : AnyRef) extends Dynamic { | ||
// getDeclaredMethods to build method map, etc. | ||
|
||
def applyDynamic(name: String)(args: Any*) : Any = { | ||
println("applyDynamic: " + name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters