Skip to content

Commit

Permalink
First steps with dynamic loading.
Browse files Browse the repository at this point in the history
We can now (sort of) load cafebabe-generated classes in memory. The API
is not final.
  • Loading branch information
psuter committed Dec 2, 2012
1 parent a369b9c commit 4ccfc7e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 63 deletions.
59 changes: 0 additions & 59 deletions src/main/scala/Test.scala

This file was deleted.

3 changes: 0 additions & 3 deletions src/main/scala/cafebabe/ClassFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,9 @@ class ClassFile(val className: String, parentName: Option[String] = None) extend

/** Loads the class using the current class loader. */
def dynamicallyLoad : Unit = {
throw new Error("Not supported yet.")
/*
val byteStream = (new ByteStream) << this
val bytes : Array[Byte] = byteStream.getBytes
CustomClassLoader.registerClass(className, bytes)
*/
}

def toStream(byteStream: ByteStream): ByteStream = {
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/cafebabe/Defaults.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object Defaults {
val defaultMinor: U2 = 0
val defaultMajor: U2 = 49 // J2SE 6.0=50, J2SE 5.0=49, JDK 1.4=48, JDK 1.3=47, JDK 1.2=46, JDK 1.1=45

val defaultClassAccessFlags: U2 = /* CLASS_ACC_PUBLIC | */ CLASS_ACC_SUPER
val defaultClassAccessFlags: U2 = CLASS_ACC_PUBLIC | CLASS_ACC_SUPER
val defaultMethodAccessFlags: U2 = METHOD_ACC_PUBLIC
val defaultFieldAccessFlags: U2 = FIELD_ACC_PROTECTED

Expand Down
25 changes: 25 additions & 0 deletions src/test/scala/cafebabe/test/DynamicLoading.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cafebabe.test

import cafebabe._
import cafebabe.ByteCodes._
import cafebabe.AbstractByteCodes._

import org.scalatest.FunSuite
class DynamicLoading extends FunSuite {
test("DL 1") {
val cf = new ClassFile("MyTest", None)
cf.addDefaultConstructor
val ch = cf.addMethod("I", "plusOne", "I").codeHandler
ch << ILoad(1) << Ldc(1) << IADD << IRETURN
ch.freeze

cf.dynamicallyLoad

val c = CustomClassLoader.loadClass("MyTest")
val o = c.newInstance().asInstanceOf[AnyRef]
val m = c.getMethod("plusOne", Integer.TYPE)
(m.invoke(o, 41 : java.lang.Integer) === 42)
}


}

0 comments on commit 4ccfc7e

Please sign in to comment.