This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Scala/Java interop

1 reply
Liam Clarke-Hut...
Joined: 2008-12-21,
User offline. Last seen 42 years 45 weeks ago.

Hi all,

I'm trialling Scala at the moment for a future personal project, but
also with a view towards a possible use at work - however, if this is
going to happen, Java code would have to be able to make use of any
work done in Scala, and so I'd like to demonstrate Java code
seamlessly using Scala.

I'm aware that chapter 29 of the Programming in Scala book covers
this, but at this early stage I'm not able to purchase the book, so I
was wondering if anyone had some basic hints on writing Scala that
Java can use.

So far I've noted two things, the first is probably an IDE problem -
when calling a Scala class from Java, my IDE (IDEA 8) does not
recognise the Scala class' constructor - that is, for class Foo (a:
Int), the IDE only recognises a constructor with nil arguments - but
the code compiles correctly, has anyone had this problem in other
IDEs?

The second thing I've noted is that for a class

class B(var a: Int)

B.a is a public field when being called within Scala, but attempting
to compile Java that accesses this field causes an error, with the
message that field a of class B is non-public. I'm not sure why it
happens, but I'm guessing that using the @BeanProperty annotation to
create getters/setters for the Java code to use would fix it, as all
my public methods are accessible.

Are there any other gotchas I should be aware of?

Regards,

Liam Clarke

James Iry
Joined: 2008-08-19,
User offline. Last seen 1 year 23 weeks ago.
Re: Scala/Java interop
I haven't read the chapter in the print edition either.  But in the meantime, here's a good way to explore what Scala generated code looks like to Java.   For instance, it shows that my constructor is an ordinary Java constructor and that my public field has turned into a private field plus an accessor/mutator pair of methods. 

Feel free to use a better disassembler. :-)

~/sctemp> cat Foo.scala
class Foo(bar : Int) {
  var blurb = "42"
}

~/sctemp> scalac Foo.scala

~/sctemp> javap -c -private Foo
Compiled from "Foo.scala"
public class Foo extends java.lang.Object implements scala.ScalaObject{
private java.lang.String blurb;

public Foo(int);
  Code:
   0:    aload_0
   1:    ldc    #10; //String 42
   3:    putfield    #14; //Field blurb:Ljava/lang/String;
   6:    aload_0
   7:    invokespecial    #19; //Method java/lang/Object."<init>":()V
   10:    return

public void blurb_$eq(java.lang.String);
  Code:
   0:    aload_0
   1:    aload_1
   2:    putfield    #14; //Field blurb:Ljava/lang/String;
   5:    return

public java.lang.String blurb();
  Code:
   0:    aload_0
   1:    getfield    #14; //Field blurb:Ljava/lang/String;
   4:    areturn

public int $tag()   throws java.rmi.RemoteException;
  Code:
   0:    aload_0
   1:    invokestatic    #35; //Method scala/ScalaObject$class.$tag:(Lscala/ScalaObject;)I
   4:    ireturn

}


On Sat, Dec 20, 2008 at 6:54 PM, Liam Clarke-Hutchinson <liam@steelsky.co.nz> wrote:
Hi all,

I'm trialling Scala at the moment for a future personal project, but
also with a view towards a possible use at work - however, if this is
going to happen, Java code would have to be able to make use of any
work done in Scala, and so I'd like to demonstrate Java code
seamlessly using Scala.

I'm aware that chapter 29 of the Programming in Scala book covers
this, but at this early stage I'm not able to purchase the book, so I
was wondering if anyone had some basic hints on writing Scala that
Java can use.

So far I've noted two things, the first is probably an IDE problem -
when calling a Scala class from Java, my IDE (IDEA 8) does not
recognise the Scala class' constructor - that is, for class Foo (a:
Int), the IDE only recognises a constructor with nil arguments - but
the code compiles correctly, has anyone had this problem in other
IDEs?

The second thing I've noted is that for a class

class B(var a: Int)

B.a is a public field when being called within Scala, but attempting
to compile Java that accesses this field causes an error, with the
message that field a of class B is non-public. I'm not sure why it
happens, but I'm guessing that using the @BeanProperty annotation to
create getters/setters for the Java code to use would fix it, as all
my public methods are accessible.

Are there any other gotchas I should be aware of?

Regards,

Liam Clarke

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland