Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to pass a String* scala vararg to a java method using Scala 2.8
    text
    copied!<p>I would like to call the following java method from scala:</p> <pre><code>protected final FilterKeyBindingBuilder filter(String urlPattern, String... morePatterns) { return filtersModuleBuilder.filter(Lists.newArrayList(urlPattern, morePatterns)); } </code></pre> <p>my scala caller looks like this</p> <pre><code>def test(url: String, urls: String*) { filter(url, urls: _*).through(classOf[MyTestWhateverFilter]) } </code></pre> <p>this compiles, however, executing the code gives an exception:</p> <pre><code>java.lang.ClassCastException: scala.collection.mutable.WrappedArray$ofRef cannot be cast to [Ljava.lang.String; </code></pre> <p>I also tried this:</p> <pre><code>def test(url: String, urls: String*) { filter(url, urls.map(_.asInstanceOf[java.lang.String]) :_*).through(classOf[MyTestWhateverFilter]) } </code></pre> <p>in this case the exception was:</p> <pre><code>java.lang.ClassCastException: scala.collection.mutable.ArrayBuffer cannot be cast to [Ljava.lang.String; </code></pre> <p>I thought that in 2.8 Array[String] is passed to java as String[] array and no extra unboxing is necessary.</p> <p>Any ideas?</p> <p>Thanks in advance!</p> <p><strong>EDIT:</strong></p> <p>how to replicate it:</p> <pre><code>import com.google.inject.servlet.ServletModule trait ScalaServletModule extends ServletModule{ def test(s: String,strs: String*) = { println(strs.getClass) println(super.filter(s,strs:_*)) } } object Test { def main(args: Array[String]) { val module = new ServletModule with ScalaServletModule module.test("/rest") } } /opt/local/lib/scala28/bin/scala -cp /Users/p.user/Downloads/guice-2.0/guice-2.0.jar:/Users/p.user/Downloads/guice-2.0/guice-servlet-2.0.jar:/Users/p.user/Downloads/guice-2.0/aopalliance.jar:/Users/p.user/Downloads/javax.jar/javax.jar:. Test </code></pre> <p>result:</p> <pre><code>class scala.collection.mutable.WrappedArray$ofRef java.lang.ClassCastException: scala.collection.mutable.WrappedArray$ofRef cannot be cast to [Ljava.lang.String; at ScalaServletModule$class.test(test.scala:6) at Test$$anon$1.test(test.scala:11) at Test$.main(test.scala:12) at Test.main(test.scala) </code></pre>
 

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