Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If no-one else comes up with a working answer, I don't think it would be all that hard to write a code generator based on the method signatures.</p> <pre><code>val a = classOf[String].getMethods.map(_.toString).filter(s =&gt; s.contains("public") &amp;&amp; s.contains("static") ) </code></pre> <p>will, for instance, pull out all the method signatures (for <code>String</code> in this case) as strings. Then</p> <pre><code>val b = a.map(_.split(" ").reverse.take(2).reverse) </code></pre> <p>will grab just the function signature with the return type split out front. Now</p> <pre><code>val c = b.map(_(1).dropWhile(_ != '(').drop(1).takeWhile(_ != ')').split(",")) val d = b.map(_(1).takeWhile(_ != '(')) </code></pre> <p>will get the argument signatures and name of the function, respectively. We then need to convert the primitive types, which is pretty easy since we just need to capitalize the first letter (save for void which becomes unit):</p> <pre><code>def jprim2scala(s: String) = { val prim = List("boolean","byte","short","char","int","long","float","double") def arrayconvert(s: String): String = { if (s.endsWith("[]")) "Array["+arrayconvert(s.substring(0,s.length-2))+"]" else if (s=="void") "Unit" else if (prim contains s) { s.substring(0,1).toUpperCase + s.substring(1) } else s } arrayconvert(s) } def e = (b,c).zipped.map((bi,ci) =&gt; (jprim2scala(bi(0)), ci.map(jprim2scala))) </code></pre> <p>and finally you can put it all back together:</p> <pre><code>val f = (d,e).zipped.map((name,ei) =&gt; { val (ret,args) = ei val lastname = name.split('.').last "def "+lastname+"(" + (for ((a,i) &lt;- args.zipWithIndex) yield ("a"+i+": "+a)).mkString(", ") + "): "+ret+" = "+name+"("+(0 until args.length).map("a"+_).mkString(",")+")" }) </code></pre> <p>Now you have a bunch of Scala code that you can put (by hand) in an appropriate singleton.</p> <pre><code>object Stringleton { // The contents of this is cut-and-paste f.map(" "+_).foreach(println) def valueOf(a0: java.lang.Object): java.lang.String = java.lang.String.valueOf(a0) def valueOf(a0: Array[Char]): java.lang.String = java.lang.String.valueOf(a0) def valueOf(a0: Array[Char], a1: Int, a2: Int): java.lang.String = java.lang.String.valueOf(a0,a1,a2) def valueOf(a0: Boolean): java.lang.String = java.lang.String.valueOf(a0) def valueOf(a0: Char): java.lang.String = java.lang.String.valueOf(a0) def valueOf(a0: Int): java.lang.String = java.lang.String.valueOf(a0) def valueOf(a0: Long): java.lang.String = java.lang.String.valueOf(a0) def valueOf(a0: Float): java.lang.String = java.lang.String.valueOf(a0) def valueOf(a0: Double): java.lang.String = java.lang.String.valueOf(a0) def copyValueOf(a0: Array[Char], a1: Int, a2: Int): java.lang.String = java.lang.String.copyValueOf(a0,a1,a2) def copyValueOf(a0: Array[Char]): java.lang.String = java.lang.String.copyValueOf(a0) def format(a0: java.lang.String, a1: Array[java.lang.Object]): java.lang.String = java.lang.String.format(a0,a1) def format(a0: java.util.Locale, a1: java.lang.String, a2: Array[java.lang.Object]): java.lang.String = java.lang.String.format(a0,a1,a2) } </code></pre> <p>(Varargs aren't working here, incidentally--you'd need to fix those if they were present. You can do this by going back to the <code>.isVarArgs</code> method from the method itself (not its string representation) and converting the last Array[X] into X* and then calling it as <code>ai: _*</code>.)</p> <p>The nice thing about this approach is that if there are common methods, you don't even need to use structural typing in order to pull them out--you can define your own trait that your wrapped methods inherit from! (And if you're especially ambitious, you can shoehorn almost-but-not-quite conforming methods to actually conform, e.g. to all have the same method name.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload