scala-java-interop

How to convert a scala.List to a java.util.List?

﹥>﹥吖頭↗ 提交于 2020-01-18 19:36:25
问题 How to convert Scala's scala.List into Java's java.util.List ? 回答1: Scala List and Java List are two different beasts, because the former is immutable and the latter is mutable. So, to get from one to another, you first have to convert the Scala List into a mutable collection. On Scala 2.7: import scala.collection.jcl.Conversions.unconvertList import scala.collection.jcl.ArrayList unconvertList(new ArrayList ++ List(1,2,3)) From Scala 2.8 onwards: import scala.collection.JavaConversions._

How to convert a scala.List to a java.util.List?

我只是一个虾纸丫 提交于 2020-01-18 19:36:06
问题 How to convert Scala's scala.List into Java's java.util.List ? 回答1: Scala List and Java List are two different beasts, because the former is immutable and the latter is mutable. So, to get from one to another, you first have to convert the Scala List into a mutable collection. On Scala 2.7: import scala.collection.jcl.Conversions.unconvertList import scala.collection.jcl.ArrayList unconvertList(new ArrayList ++ List(1,2,3)) From Scala 2.8 onwards: import scala.collection.JavaConversions._

Use Scala constants in Java

与世无争的帅哥 提交于 2020-01-13 08:56:16
问题 I am currently evaluating Scala for future projects and came across something strange. I created the following constant for us in a JSP: val FORMATED_TIME = "formatedTime"; And it did not work. After some experimenting I decided to decompile to get to the bottom of it: private final java.lang.String FORMATED_TIME; public java.lang.String FORMATED_TIME(); Code: 0: aload_0 1: getfield #25; //Field FORMATED_TIME:Ljava/lang/String; 4: areturn Now that is interesting! Personally I have been

Use Scala constants in Java

你离开我真会死。 提交于 2020-01-13 08:56:04
问题 I am currently evaluating Scala for future projects and came across something strange. I created the following constant for us in a JSP: val FORMATED_TIME = "formatedTime"; And it did not work. After some experimenting I decided to decompile to get to the bottom of it: private final java.lang.String FORMATED_TIME; public java.lang.String FORMATED_TIME(); Code: 0: aload_0 1: getfield #25; //Field FORMATED_TIME:Ljava/lang/String; 4: areturn Now that is interesting! Personally I have been

Access inner class method with “fine” syntax in Java using Scala class & object

人盡茶涼 提交于 2020-01-07 02:55:05
问题 I have the following scala example: class Nested {} object Nested { class Inner {} object Inner { def x = 321 } } With a JUnit test to test specifically that I can do Nested.Inner.x() and call the method freely without the Nested.Inner$.MODULE$.x() : import static junit.framework.Assert.*; import org.junit.Test; public class TestFromJava { @Test public void accessTest() { assertEquals(321, Nested.Inner.x()); } } This compiles, but at the moment of running the test, I am getting ( sbt test ):

How to access a Java inherited field with the same name as a Scala method?

会有一股神秘感。 提交于 2020-01-06 07:25:11
问题 I'm having a Scala trait A with an abstract method trait A[T] { def self: T } Now it happened me that I want to extends a Java class not under my control with a field with the same name: public class B<T> { protected T self; } I'd like to have a class C defined like class C[T] extends B[T] with A[T] { override def self: T = /* the field `self` of `B` ??? */ } Is it possible to access the field B.self somehow so that it doesn't resolve to the method A.self instead? Is it even possible to have

How can I provide a scala companion object's class to Java?

橙三吉。 提交于 2020-01-04 16:57:29
问题 I have a Java code that looks for annotations in static methods of a class. processor.readStatics( MyClass.class ); // Takes Class<?> How can I provide the methods of a scala companion object to this function from within scala? class MyClass { } object MyClass { def hello() { println("Hello (object)") } } I seems that: MyClass$.MODULE$.getClass() should be the answer. However, MyClass$ seems to be missing from scala (in 2.10, at least) and only visible to Java. println( Class.forName("MyClass

Scala resolving to wrong override in Comparator.thenComparing

心不动则不痛 提交于 2020-01-03 09:05:10
问题 I'm trying to translate the following Java code: import java.util.Comparator; public class ComparatorTestJava { public static void test() { Comparator<String> cmp = (s1, s2) -> 0; cmp = cmp.thenComparing(s -> s); } } into Scala. I think this should work: import java.util.{Comparator, function} object ComparatorTest { var comparator: Comparator[String] = (t1, t2) ⇒ 0 comparator = comparator.thenComparing(new function.Function[String, String] { override def apply(t: String): String = t }) } But

What are the guarantees for scala access qualifiers?

邮差的信 提交于 2020-01-03 08:18:11
问题 I have a class with this code: package shop.orders.services.email private[services] class EmailService {...} Then in a different package, I use that class: package shop.ui import shop.orders.services.email.EmailService class PaymentConfirmation extends WithFacesContext { var emailService: EmailService = null Looking at the generated bytecode, there is no sign of any access modifier, which makes sense, as Java does not support such access restrictions. So what happens if I create a library

How to use Scala varargs from Java code

梦想的初衷 提交于 2019-12-28 16:37:44
问题 There are plenty of articles on calling Java varargs from Scala code, but the only thing I could find the opposite way round was this question: Using scala vararg methods in java, which doesn't have any concrete examples. I'm trying to use scala.Console from some Java code, for the reason that java.io.Console doesn't work in Eclipse, whereas the Scala one does. But I cannot get the method def readLine (text: String, args: Any*): String to work because it seems to be expecting a scala