Code Examples for

Programming in Scala

Return to chapter index

29 Combining Scala and Java

  • 29.1 Using Scala from Java
  • 29.2 Annotations
  • 29.3 Existential types
  • 29.4 Conclusion
  • 29.1 Using Scala from Java


    // In file java/App.scala object App { def main(args: Array[String]) { println("Hello, world!") } }
    $ javap App$ public final class App$ extends java.lang.Object implements scala.ScalaObject{ public static final App$ MODULE$; public static {}; public App$(); public void main(java.lang.String[]); public int $tag(); }
    $ javap App Compiled from "App.scala" public final class App extends java.lang.Object{ public static final int $tag(); public static final void main(java.lang.String[]); }

    29.2 Annotations


    // Java serial version marker private final static long SerialVersionUID = 1234L
    // In file java/Reader.scala import java.io._ class Reader(fname: String) { private val in = new BufferedReader(new FileReader(fname)) @throws(classOf[IOException]) def read() = in.read() }
    $ javap Reader Compiled from "Reader.scala" public class Reader extends java.lang.Object implements scala.ScalaObject{ public Reader(java.lang.String); public int read() throws java.io.IOException; public int $tag(); } $
    // In file java/SetTest.scala import org.junit.Test import org.junit.Assert.assertEquals class SetTest { @Test def testMultiAdd { val set = Set() + 1 + 2 + 3 + 1 + 2 + 3 assertEquals(3, set.size) } }
    $ scala -cp junit-4.3.1.jar:. org.junit.runner.JUnitCore SetTest JUnit version 4.3.1 . Time: 0.023 OK (1 test)
    // In file java/Ignore.java import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Ignore { }
    // In file java/Tests.scala object Tests { @Ignore def testData = List(0, 1, -1, 5, -5) def test1 { assert(testData == (testData.head :: testData.tail)) } def test2 { assert(testData.contains(testData.head)) } }
    // In file java/FindTests.scala for { method <- Tests.getClass.getMethods if method.getName.startsWith("test") if method.getAnnotation(classOf[Ignore]) == null } { println("found a test method: " + method) }
    $ javac Ignore.java $ scalac Tests.scala $ scalac FindTests.scala $ scala FindTests found a test method: public void Tests$.test2() found a test method: public void Tests$.test1()

    29.3 Existential types


    type forSome { declarations }
    Iterator[T] forSome { type T }
    Iterator[T] forSome { type T <: Component }
    Iterator[T] forSome { type T <: Component }
    // In file java/Wild.java // This is a Java class with wildcards public class Wild { Collection<?> contents() { Collection<String> stuff = new Vector<String>(); stuff.add("a"); stuff.add("b"); stuff.add("see"); return stuff; } }
    scala> val contents = (new Wild).contents contents: java.util.Collection[?0] forSome { type ?0 } = [a, b, see]
    scala> contents.size() res0: Int = 3
    import scala.collection.mutable.Set val iter = (new Wild).contents.iterator val set = Set.empty[???] // what type goes here? while (iter.hasMore) set += iter.next()
    // In file java/UseWild.scala import scala.collection.mutable.Set import java.util.Collection abstract class SetAndType { type Elem val set: Set[Elem] } def javaSet2ScalaSet[T](jset: Collection[T]): SetAndType = { val sset = Set.empty[T] // now T can be named! val iter = jset.iterator while (iter.hasNext) sset += iter.next() return new SetAndType { type Elem = T val set = sset } }

    29.4 Conclusion

    For more information about Programming in Scala (the "Stairway Book"), please visit:

    http://www.artima.com/shop/programming_in_scala

    and:

    http://booksites.artima.com/programming_in_scala

    Copyright © 2007-2008 Artima, Inc. All rights reserved.

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.