Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added primitive argument loader.

  • Loading branch information...
commit 3c041c2122a8276591ad787c238ee68dd32c6a52 1 parent 6b7c3a4
Marko Elezovic authored
View
24 project/ProxyKwaiBuild.scala
@@ -12,21 +12,21 @@ object BuildSettings {
crossScalaVersions := Seq("2.9.1", "2.9.0-1", "2.9.0"),
scalaVersion <<= (crossScalaVersions) { versions => versions.head },
-
+
scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "UTF-8", "-optimise"), // , "-Yrepl-sync"
javacOptions := Seq("-deprecation", "-encoding", "UTF-8", "-source", "1.5", "-target", "1.5"),
-
+
unmanagedSourceDirectories in Compile <<= (scalaSource in Compile)( _ :: Nil),
unmanagedSourceDirectories in Test <<= (scalaSource in Test )( _ :: Nil)
- )
+ )
- val bsProxyKwai =
+ val bsProxyKwai =
defaultSettings ++ Seq(
name := "ProxyKwai",
version := "0.0.1-SNAPSHOT",
- unmanagedSourceDirectories in Compile <<= (javaSource in Compile)( _ :: Nil),
+ unmanagedSourceDirectories in Compile <<= (javaSource in Compile)( _ :: Nil),
autoScalaLibrary := false,
crossPaths := false
)
@@ -38,11 +38,13 @@ object Dependencies {
val jasmin = "net.sf.jasmin" % "jasmin" % "2.4"
val scalaTest = "org.scalatest" %% "scalatest" % "1.6.1" % "test"
-
- val depsProxyKwai =
+ val jUnit = "junit" % "junit" % "4.10" % "test"
+
+ val depsProxyKwai =
libraryDependencies := Seq(
jasmin,
- scalaTest
+ scalaTest,
+ jUnit
)
}
@@ -66,7 +68,7 @@ object Repositories {
val ElementNexus = "Element Nexus" at "http://maven.element.hr/nexus/content/groups/public/"
val ElementReleases = "Element Releases" at "http://maven.element.hr/nexus/content/repositories/releases/"
val ElementSnapshots = "Element Snapshots" at "http://maven.element.hr/nexus/content/repositories/snapshots/"
-}
+}
// ---------------------------------------------------------------------------
@@ -79,7 +81,7 @@ object Resolvers {
Resolver.withDefaultResolvers(rs, mavenCentral = false, scalaTools = false)
}
)
-}
+}
// ---------------------------------------------------------------------------
@@ -93,6 +95,6 @@ object Publishing {
credentials += Credentials(Path.userHome / ".publish" / "element.credentials"),
publishArtifact in (Compile, packageDoc) := false
)
-}
+}
// ===========================================================================
View
34 proxykwai/src/main/java/hr/element/proxykwai/ProxyKwai.java
@@ -8,9 +8,9 @@
import jasmin.ClassFile;
public class ProxyKwai {
- final String targetClass;
- final String targetMethod;
- final Class<?>[] arguments;
+ private final String targetClass;
+ private final String targetMethod;
+ private final Class<?>[] arguments;
public ProxyKwai(
final String targetClass,
@@ -26,7 +26,7 @@ public ProxyKwai(
final ProxyCall[] calls;
- ProxyKwai(
+ private ProxyKwai(
final String targetClass,
final String targetMethod,
final Class<?>[] arguments,
@@ -110,7 +110,8 @@ private String toJasmin() {
" getstatic %1$s/MODULE$ L%1$s;\n",
slashify(call.sourceClass)
).format(
- " aload_0\n"
+ "%s",
+ getArgumentLoader(arguments)
).format(
" invokevirtual %s/%s(%s)%s\n",
slashify(call.sourceClass),
@@ -126,10 +127,29 @@ private String toJasmin() {
}
System.out.println(formatter);
-
return formatter.toString();
}
+ private static String getArgumentLoader(final Class<?>[] arguments) {
+ final StringBuilder sB = new StringBuilder();
+
+ for (int index = 0; index < arguments.length; index ++) {
+ final String quickDirtyHack =
+ getReturnType(arguments[index]);
+
+ // quick and dirty hack will not work for indexes > 3
+ // and god knows how many other cases...
+
+ sB.append(" ")
+ .append(quickDirtyHack)
+ .append("load_")
+ .append(index)
+ .append('\n');
+ }
+
+ return sB.toString();
+ }
+
public void export(final String classPath) {
final String body = toJasmin();
@@ -157,7 +177,7 @@ public void export(final String classPath) {
}
}
- static byte[] assemble(final String body) throws Exception {
+ private static byte[] assemble(final String body) throws Exception {
final ClassFile classFile = new ClassFile();
final BufferedReader bR =
new BufferedReader(new StringReader(body));
View
37 proxykwai/src/test/scala/hr/element/proxykwai/test/ProxyKwaiCoolcam.scala
@@ -0,0 +1,37 @@
+package hr.element.proxykwai
+package test
+
+import org.scalatest._
+
+import org.junit.runner.RunWith
+import org.scalatest.junit.JUnitRunner
+
+@RunWith(classOf[JUnitRunner])
+class ProxyKwaiCoolcam extends FeatureSpec {
+
+ object UglyPowers {
+ def powerAsInt (x: Int, y: Int) = math.pow(x, y).toInt
+ def powerAsString(x: Int, y: Int) = x +" ^ "+ y
+ }
+
+ val prettyPowerCompiler =
+ new ProxyKwai(
+ "hr.element.proxykwai.test.PrettyPowers",
+ "$times$times",
+ Array[Class[_]](classOf[Int], classOf[Int]))
+
+ .addObjectProxy(UglyPowers.getClass, "powerAsInt", classOf[Int])
+ .addObjectProxy(UglyPowers.getClass, "powerAsString", classOf[String])
+
+ feature("Proxykwai is pretty") {
+
+ scenario("Multiple arguments") {
+ prettyPowerCompiler.export("target/test-classes")
+
+/*
+ info(**(2,8): Int) // 390625
+ info(**(5,8): String) // 5 ^ 8
+*/
+ }
+ }
+}
View
176 proxykwai/src/test/scala/hr/element/proxykwai/test/ProxyKwaiSpec.scala
@@ -1,85 +1,91 @@
-package hr.element.proxykwai
-package test
-
-import org.scalatest._
-
-object ProxyKwaiFeatureSpec extends FeatureSpec {
-
- object TestParserPrimitives {
- def parseBoolean(s: String) = s.toBoolean
- def parseByte(s: String) = s.toByte
- def parseShort(s: String) = s.toShort
- def parseChar(s: String) = s.head
- def parseInt(s: String) = s.toInt
- def parseFloat(s: String) = s.toFloat
- def parseDouble(s: String) = s.toDouble
- def parseLong(s: String) = s.toLong
- def parseUnit(s: String) = ()
- }
-
- object TestParserJavaBoxes {
- def parseBoolean(s: String) = java.lang.Boolean.valueOf(s)
- def parseByte(s: String) = java.lang.Byte.valueOf(s)
- def parseShort(s: String) = java.lang.Short.valueOf(s)
- def parseCharacter(s: String) = java.lang.Character.valueOf(s.head)
- def parseInteger(s: String) = java.lang.Integer.valueOf(s)
- def parseFloat(s: String) = java.lang.Float.valueOf(s)
- def parseDouble(s: String) = java.lang.Double.valueOf(s)
- def parseLong(s: String) = java.lang.Long.valueOf(s)
- def parseVoid(s: String) = null: java.lang.Void
- }
-
- object TestParserClasses {
- def parseString(s: String) = s
- def parseCharArray(s: String) = s.toCharArray
- def parseIntArray(s: String) = s.toCharArray.map(_.getNumericValue)
- def parseBigInt(s: String) = BigInt(s)
- def parseRegex(s: String) = s.r
- def parseOption(s: String) = Option(s)
- def parseStringList(s: String) = s.split(",").toList
- }
-
- val testParserProxyCompiler =
- new ProxyKwai(
- "hr.element.proxykwai.test.TestParserProxy",
- "parse",
- Array[Class[_]](classOf[String]))
-
- .addObjectProxy(TestParserPrimitives.getClass, "parseBoolean", classOf[Boolean])
- .addObjectProxy(TestParserPrimitives.getClass, "parseByte", classOf[Byte])
- .addObjectProxy(TestParserPrimitives.getClass, "parseShort", classOf[Short])
- .addObjectProxy(TestParserPrimitives.getClass, "parseChar", classOf[Char])
- .addObjectProxy(TestParserPrimitives.getClass, "parseInt", classOf[Int])
- .addObjectProxy(TestParserPrimitives.getClass, "parseLong", classOf[Long])
- .addObjectProxy(TestParserPrimitives.getClass, "parseFloat", classOf[Float])
- .addObjectProxy(TestParserPrimitives.getClass, "parseDouble", classOf[Double])
- .addObjectProxy(TestParserPrimitives.getClass, "parseUnit", classOf[Unit])
-
- .addObjectProxy(TestParserJavaBoxes.getClass, "parseBoolean", classOf[java.lang.Boolean])
- .addObjectProxy(TestParserJavaBoxes.getClass, "parseByte", classOf[java.lang.Byte])
- .addObjectProxy(TestParserJavaBoxes.getClass, "parseShort", classOf[java.lang.Short])
- .addObjectProxy(TestParserJavaBoxes.getClass, "parseCharacter", classOf[java.lang.Character])
- .addObjectProxy(TestParserJavaBoxes.getClass, "parseInteger", classOf[java.lang.Integer])
- .addObjectProxy(TestParserJavaBoxes.getClass, "parseLong", classOf[java.lang.Long])
- .addObjectProxy(TestParserJavaBoxes.getClass, "parseFloat", classOf[java.lang.Float])
- .addObjectProxy(TestParserJavaBoxes.getClass, "parseDouble", classOf[java.lang.Double])
- .addObjectProxy(TestParserJavaBoxes.getClass, "parseVoid", classOf[java.lang.Void])
-
- .addObjectProxy(TestParserClasses.getClass, "parseString", classOf[String])
- .addObjectProxy(TestParserClasses.getClass, "parseCharArray", classOf[Array[Char]])
- .addObjectProxy(TestParserClasses.getClass, "parseIntArray", classOf[Array[Int]])
- .addObjectProxy(TestParserClasses.getClass, "parseBigInt", classOf[BigInt])
- .addObjectProxy(TestParserClasses.getClass, "parseRegex", classOf[scala.util.matching.Regex])
- .addObjectProxy(TestParserClasses.getClass, "parseOption", classOf[Option[String]])
-
- feature("Overloading via return types on scala singletons") {
-
- scenario("Multiple singleton objects") {
- testParserProxyCompiler.export("target/classes")
-
- info(TestParser.parse("1234567"): Array[Char])
- info(TestParser.parse("1234567"): Array[Char])
- info(TestParser.parse("1234567"): Array[Int])
- }
- }
-}
+package hr.element.proxykwai
+package test
+
+import org.scalatest._
+
+import org.junit.runner.RunWith
+import org.scalatest.junit.JUnitRunner
+
+@RunWith(classOf[JUnitRunner])
+class ProxyKwaiFeatureSpec extends FeatureSpec {
+
+ object TestParserPrimitives {
+ def parseBoolean(s: String) = s.toBoolean
+ def parseByte(s: String) = s.toByte
+ def parseShort(s: String) = s.toShort
+ def parseChar(s: String) = s.head
+ def parseInt(s: String) = s.toInt
+ def parseFloat(s: String) = s.toFloat
+ def parseDouble(s: String) = s.toDouble
+ def parseLong(s: String) = s.toLong
+ def parseUnit(s: String) = ()
+ }
+
+ object TestParserJavaBoxes {
+ def parseBoolean(s: String) = java.lang.Boolean.valueOf(s)
+ def parseByte(s: String) = java.lang.Byte.valueOf(s)
+ def parseShort(s: String) = java.lang.Short.valueOf(s)
+ def parseCharacter(s: String) = java.lang.Character.valueOf(s.head)
+ def parseInteger(s: String) = java.lang.Integer.valueOf(s)
+ def parseFloat(s: String) = java.lang.Float.valueOf(s)
+ def parseDouble(s: String) = java.lang.Double.valueOf(s)
+ def parseLong(s: String) = java.lang.Long.valueOf(s)
+ def parseVoid(s: String) = null: java.lang.Void
+ }
+
+ object TestParserClasses {
+ def parseString(s: String) = s
+ def parseCharArray(s: String) = s.toCharArray
+ def parseIntArray(s: String) = s.toCharArray.map(_.getNumericValue)
+ def parseBigInt(s: String) = BigInt(s)
+ def parseRegex(s: String) = s.r
+ def parseOption(s: String) = Option(s)
+ def parseStringList(s: String) = s.split(",").toList
+ }
+
+ val testParserProxyCompiler =
+ new ProxyKwai(
+ "hr.element.proxykwai.test.TestParserProxy",
+ "parse",
+ Array[Class[_]](classOf[String]))
+
+ .addObjectProxy(TestParserPrimitives.getClass, "parseBoolean", classOf[Boolean])
+ .addObjectProxy(TestParserPrimitives.getClass, "parseByte", classOf[Byte])
+ .addObjectProxy(TestParserPrimitives.getClass, "parseShort", classOf[Short])
+ .addObjectProxy(TestParserPrimitives.getClass, "parseChar", classOf[Char])
+ .addObjectProxy(TestParserPrimitives.getClass, "parseInt", classOf[Int])
+ .addObjectProxy(TestParserPrimitives.getClass, "parseLong", classOf[Long])
+ .addObjectProxy(TestParserPrimitives.getClass, "parseFloat", classOf[Float])
+ .addObjectProxy(TestParserPrimitives.getClass, "parseDouble", classOf[Double])
+ .addObjectProxy(TestParserPrimitives.getClass, "parseUnit", classOf[Unit])
+
+ .addObjectProxy(TestParserJavaBoxes.getClass, "parseBoolean", classOf[java.lang.Boolean])
+ .addObjectProxy(TestParserJavaBoxes.getClass, "parseByte", classOf[java.lang.Byte])
+ .addObjectProxy(TestParserJavaBoxes.getClass, "parseShort", classOf[java.lang.Short])
+ .addObjectProxy(TestParserJavaBoxes.getClass, "parseCharacter", classOf[java.lang.Character])
+ .addObjectProxy(TestParserJavaBoxes.getClass, "parseInteger", classOf[java.lang.Integer])
+ .addObjectProxy(TestParserJavaBoxes.getClass, "parseLong", classOf[java.lang.Long])
+ .addObjectProxy(TestParserJavaBoxes.getClass, "parseFloat", classOf[java.lang.Float])
+ .addObjectProxy(TestParserJavaBoxes.getClass, "parseDouble", classOf[java.lang.Double])
+ .addObjectProxy(TestParserJavaBoxes.getClass, "parseVoid", classOf[java.lang.Void])
+
+ .addObjectProxy(TestParserClasses.getClass, "parseString", classOf[String])
+ .addObjectProxy(TestParserClasses.getClass, "parseCharArray", classOf[Array[Char]])
+ .addObjectProxy(TestParserClasses.getClass, "parseIntArray", classOf[Array[Int]])
+ .addObjectProxy(TestParserClasses.getClass, "parseBigInt", classOf[BigInt])
+ .addObjectProxy(TestParserClasses.getClass, "parseRegex", classOf[scala.util.matching.Regex])
+ .addObjectProxy(TestParserClasses.getClass, "parseOption", classOf[Option[String]])
+
+ feature("Overloading via return types on scala singletons") {
+
+ scenario("Multiple singleton objects") {
+ testParserProxyCompiler.export("target/test-classes")
+
+/*
+ info(TestParserProxy.parse("true"): Boolean)
+ info(TestParserProxy.parse("123456"): Array[Char])
+ info(TestParserProxy.parse("true"): Array[Int])
+*/
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.